Prva inacica paketa za Debian wheezy distribuciju.
[webalizer-cn.git] / debian / postinst
index a3b3604..2203c52 100755 (executable)
 #!/bin/sh
 
-[ "$1" = "configure" ] || exit 0
-[ "$DEBIAN_SCRIPT_DEBUG" ] && set -x
+set -e
 
-# Load CARNet Tools
-. /usr/share/carnet-tools/functions.sh
+[ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
+
+case "$1" in
+    configure)
+    # continue below
+    ;;
+
+    abort-upgrade|abort-remove|abort-deconfigure)
+    exit 0
+    ;;
+
+    *)
+    echo "postinst called with unknown argument \`$1'" >&2
+    exit 0
+    ;;
+esac
 
-# Debconf
+
+# Load debconf
 . /usr/share/debconf/confmodule
+db_version 2.0
 
-# Get some useful functions (apache-common)
-. /usr/share/apache/postinst.common
+# Include CARNet functions
+. /usr/share/carnet-tools/functions.sh
 
-dir=/var/www/webalizer
-htpasswd=$dir/.htpasswd
-CONF=/etc/webalizer/webalizer.conf
-BACKUPDIR=/var/backups
-CONFBAK=$BACKUPDIR/`basename $CONF`.bak
-oldreporttitle="Usage Statistics for"
+PKG="webalizer-cn"
+WDIR="/var/www/webalizer"
+WHTPASSWD="$WDIR/.htpasswd"
+WCONF="/etc/webalizer/webalizer.conf"
+WCONFBAK=`basename $WCONF`
+oldreporttitle="usage statistics for"
 reporttitle="Statistika pristupa za"
 
+customized=0
+need_restart=0
 
-# Check if there is some old webalizer-cn.conf file in Apache 1.x conf.d directory  -ddosen
-if [ -f "/etc/apache/conf.d/webalizer-cn.conf" ]; then
-       if ! cmp -s "/etc/apache/conf.d/webalizer-cn.conf" "/etc/apache2/conf.d/webalizer-cn.conf"; then
-               mv /etc/apache/conf.d/webalizer-cn.conf /etc/apache2/conf.d/webalizer-cn.conf
-       fi
-fi
+if [ -f "$WCONF" ]; then
 
-temp=`pget $CONF OutputDir |tail -n 1|awk '{print $NF}'`
-if [ "$temp" != "$dir" ]; then
-       #cp_echo "CN: Found custom OutputDir value: $temp, not touching anything!"
-       db_set webalizer/directory "$temp" || true
-fi
+    # Backup first.
+    if cp_check_and_backup "$WCONF"; then
+        cp_echo "CN: Old $WCONF saved as \"/var/backups/$WCONFBAK.bak\"."
+    fi
 
-# Customise ReportTitle
-temp=`pget $CONF ReportTitle |tail -n 1|perl -pe 's/(ReportTitle\s+?)(.*$)/$2/'`
-if [ "$temp" = "$oldreporttitle" ]; then
-       pset $CONF ReportTitle "$reporttitle"
-       db_set webalizer/doc_title "$reporttitle" || true
+    # Check for OutputDir
+    temp=`awk 'tolower($1) == "outputdir" { print $2; }' $WCONF | tail -n 1`
+    if [ "$temp" != "$WDIR" ]; then
+        cp_echo "CN: Found custom OutputDir value: $temp"
+        db_set webalizer/directory "$temp" || true
+        customized=1
+    fi
+
+    # Check for ReportTitle
+    temp=`awk 'tolower($1) == "reporttitle" { for(i=2; i<NF; i++) \
+          printf("%s ", tolower($i)); print tolower($(NF)) }' $WCONF | tail -n 1`
+    if [ "$temp" = "$oldreporttitle" ]; then
+        temp_re="^[[:space:]]*ReportTitle[[:space:]]*${oldreporttitle}$"
+        cp_check_and_sed "." \
+            "s/$temp_re/ReportTitle    ${reporttitle}/I" \
+            "$WCONF" || true
+        db_set webalizer/doc_title "$reporttitle" || true
+        need_restart=1
+    fi
+else
+    cp_echo "CN: Configuration file $WCONF is missing."
+    customized=1
 fi
 
-db_stop
+db_stop || true
+
 
-# Reload Apache2 web server
-if [ -x /etc/init.d/apache2 ]; then
-       /etc/init.d/apache2 reload || true
+# Check Apache2 web server configuration and reload Apache2 web server.
+#
+if [ $need_restart -eq 1 ]; then
+    if apache2ctl configtest 2>/dev/null; then
+        invoke-rc.d apache2 force-reload || true
+    else
+        # Something is broken.
+        cp_echo "CN: Your Apache2 configuration is broken."
+        cp_echo "CN: Please, check the service after the installation finishes!"
+    fi
 fi
 
-# Send mail with informations
-if [ ! -f $htpasswd ]; then
-       passwd=`makepasswd`
-       htpasswd -bc $htpasswd admin $passwd 2> /dev/null
-       cp_echo ""
-       cp_echo "CN: Webalizer URL is http://`hostname -f`/stats/"
-       cp_echo "CN:            User: admin"
-       cp_echo "CN:        Password: $passwd"
+
+# Generate e-mail message with informations (username and password).
+#
+if [ $customized -eq 0 ]; then
+    if [ ! -f "$WHTPASSWD" ]; then
+        passwd="$(makepasswd --chars 8)"
+        htpasswd -bcm $WHTPASSWD admin $passwd 2> /dev/null
+        cp_echo ""
+        cp_echo "CN: Webalizer URL is http://`hostname -f`/stats/"
+        cp_echo "CN:            User: admin"
+        cp_echo "CN:        Password: $passwd"
         cp_echo ""
-        cp_echo "CN: Use \"htpasswd $htpasswd admin\" to change it."
-       cp_mail "webalizer-cn"
+        cp_echo "CN: Use \"htpasswd -m $WHTPASSWD admin\" to change it."
+    else
+        cp_echo "CN: File $WHTPASSWD already exists."
+    fi
+else
+    cp_echo "CN: Skipping file $WHTPASSWD"
 fi
+
+# Mail root.
+#
+cp_mail "$PKG"
+
+# dh_installdeb will replace this with shell code automatically
+# generated by other debhelper scripts.
+
+#DEBHELPER#
+
+exit 0