9c07c04cdc8b62cc46a6c57ed39c413693997bdd
[webalizer-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
6
7 case "$1" in
8     configure)
9     # continue below
10     ;;
11
12     abort-upgrade|abort-remove|abort-deconfigure)
13         exit 0
14     ;;
15
16     *)
17         echo "postinst called with unknown argument \`$1'" >&2
18         exit 0
19     ;;
20 esac
21
22
23 # Load debconf
24 . /usr/share/debconf/confmodule
25 db_version 2.0
26
27 # Include CARNet functions
28 . /usr/share/carnet-tools/functions.sh
29
30 PKG="webalizer-cn"
31 WDIR="/var/www/webalizer"
32 WHTPASSWD="$WDIR/.htpasswd"
33 WCONF="/etc/webalizer/webalizer.conf"
34 WCONFBAK=`basename $WCONF`
35
36 oldreporttitle="usage statistics for"
37 reporttitle="Statistika pristupa za"
38 customized=0
39
40
41 if [ -e /usr/share/apache2/apache2-maintscript-helper ]; then
42     . /usr/share/apache2/apache2-maintscript-helper
43
44     webalizer_enable() {
45         cp_echo "CN: Enabling $PKG configuration for Apache2."
46         return 0
47     }
48 else
49     cp_echo "CN: Could not load Apache 2.4 maintainer script helper."
50
51     webalizer_enable() {
52         return 1
53     }
54 fi
55
56 webalizer_error() {
57     cp_echo "CN: Error while trying to enable $PKG configuration for Apache2."
58 }
59
60 mv_conffile()
61 {
62     local oldconffile="$1"
63     local newconffile="$2"
64
65     rm -f $oldconffile.dpkg-remove
66     [ -e "$oldconffile" ] || return 0
67
68     cp_echo "CN: Preserving user changes to $newconffile (renamed from $oldconffile)."
69     mv -f "$newconffile" "$newconffile.dpkg-new"
70     mv -f "$oldconffile" "$newconffile"
71 }
72
73
74 mv_conffile /etc/apache2/conf.d/$PKG.conf \
75     /etc/apache2/conf-available/$PKG.conf
76
77 if [ -f "$WCONF" ]; then
78
79     # Backup first.
80     if cp_check_and_backup "$WCONF"; then
81         cp_echo "CN: Old $WCONF saved as /var/backups/$WCONFBAK.bak."
82     fi
83
84     # Check for OutputDir
85     temp=`awk 'tolower($1) == "outputdir" { print $2; }' $WCONF | tail -n 1`
86     if [ "$temp" != "$WDIR" ]; then
87         cp_echo "CN: Found custom OutputDir value: $temp"
88         db_set webalizer/directory "$temp" || true
89         customized=1
90     fi
91
92     # Check for ReportTitle
93     temp=`awk 'tolower($1) == "reporttitle" { for(i=2; i<NF; i++) \
94           printf("%s ", tolower($i)); print tolower($(NF)) }' $WCONF | tail -n 1`
95     if [ "$temp" = "$oldreporttitle" ]; then
96         temp_re="^[[:space:]]*ReportTitle[[:space:]]*${oldreporttitle}$"
97         cp_check_and_sed "." \
98             "s/$temp_re/ReportTitle     ${reporttitle}/I" \
99             "$WCONF" || true
100         db_set webalizer/doc_title "$reporttitle" || true
101     fi
102 else
103     cp_echo "CN: Configuration file $WCONF is missing."
104     customized=1
105 fi
106
107 db_stop || true
108
109
110 # Generate e-mail message with informations (username and password).
111 #
112 if [ $customized -eq 0 ]; then
113     if [ ! -f "$WHTPASSWD" ]; then
114         passwd="$(makepasswd --chars 8)"
115         htpasswd -bcm $WHTPASSWD admin $passwd 2> /dev/null
116         cp_echo ""
117         cp_echo "CN: Webalizer URL is http://`hostname -f`/stats/"
118         cp_echo "CN:            User: admin"
119         cp_echo "CN:        Password: $passwd"
120         cp_echo ""
121         cp_echo "CN: Use \"htpasswd -m $WHTPASSWD admin\" to change it."
122     else
123         cp_echo "CN: File $WHTPASSWD already exists."
124     fi
125 else
126     cp_echo "CN: Skipping file $WHTPASSWD"
127 fi
128
129 #DEBHELPER#
130
131 if ! apache2ctl configtest >/dev/null 2>&1; then
132     cp_echo "CN: Your Apache2 configuration seems to be broken."
133     cp_echo "CN: Please, check the service after the installation finishes!"
134 fi
135
136 # Mail root.
137 #
138 cp_mail "$PKG"
139
140 exit 0