#! /bin/sh
# config script for webalizer
#
# see: dh_installdeb(1)

set -e

# bogus - though necessary - rule
. /usr/share/debconf/confmodule
db_version 2.0

case "$1" in
    configure|reconfigure)
      if [ "$2" ] && dpkg --compare-versions $2 lt 2.01.6-1; then
        db_input high webalizer/upgrading || true
        db_go
      fi
      
      if [ "$2" ] && dpkg --compare-versions $2 lt 2.01.10-30; then
        db_input high webalizer/upgrade2011030 || true
        db_go
      fi

      if [ -f "/etc/webalizer.conf" ] && [ ! -f "/etc/webalizer/webalizer.conf" ]; then
        [ -d /etc/webalizer/ ] || mkdir /etc/webalizer/;
        mv /etc/webalizer.conf /etc/webalizer/webalizer.conf;
      fi

      conffile="/etc/webalizer/webalizer.conf"
      OUTPUTDIR=""
      REPORTTITLE=""
      LOGFILE=""
      if [ -f $conffile ]; then
	# parse existing configuration.  This code happily
	# concatenates multiple occurences of a configuration item,
	# but that should never occur <frank@debian.org>.
	OUTPUTDIR=`sed -ne '/^OutputDir/ {s@OutputDir[[:space:]]\([^[:space:]]*\)@\1@;p}' $conffile`
	REPORTTITLE=`sed -ne '/^ReportTitle/ {s@ReportTitle[[:space:]]\([^[:space:]]*\)@\1@;p}' $conffile`
	LOGFILE=`sed -ne '/^LogFile/ {s@LogFile[[:space:]]\([^[:space:]]*\)@\1@;p}' $conffile`
      fi
      # assign default values
      [ -n "$OUTPUTDIR" ] || OUTPUTDIR="/var/www/webalizer"
      [ -n "$REPORTTITLE" ] || REPORTTITLE="Usage statistics for"
      if [ -z "$LOGFILE" ]; then
        # apache2 found?
	if [ -f /var/log/apache2/access.log.1 ] || [ -f /var/log/apache2/access.log ]; then
          LOGFILE="/var/log/apache2/access.log.1";
	else
          LOGFILE="/var/log/apache/access.log.1";
	fi
      fi

      # now preseed the debconf questions with what we found
      db_set webalizer/directory "$OUTPUTDIR" || true
      db_set webalizer/doc_title "$REPORTTITLE" || true
      db_set webalizer/logfile "$LOGFILE" || true

      # Ask for the directory the output should be put in
      db_input medium webalizer/directory || true
      db_go

      # At this point, ask the user what the title of webalizer's reports should be
      db_input medium webalizer/doc_title || true
      db_go

      # Ask for the rotated logfile
      # by default  is access log file of apache, but if I found apache2 log file,
      # I changed default to this one.
      db_input medium webalizer/logfile || true
      db_go

      # Ask for enable DNSCache option
      db_input medium webalizer/dnscache || true
      db_go
    ;;
    *)
        echo "config called with unknown argument \`$1'" >&2;
        exit 1;
    ;;
esac

# dh_installdeb will replace this with shell code automatically
# generated by other debhelper scripts.

exit 0

