r2:
[amavis-stats-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$1" = "configure" ] || exit 0
6 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
7
8 hostname=`hostname -f`
9 cron="/etc/cron.d/amavis-stats"
10 htpasswd="/usr/share/amavis-stats/.htpasswd"
11 aconf="/etc/amavis-stats/apache.conf"
12 # these two are used in mail
13 package="amavis-stats-cn"
14 version="0.1.12-2"
15
16 notify () {
17   if [ "x$1" = "x-mailonly" ]; then
18     shift
19   else
20     echo "$1"
21   fi
22   NOTICE="$NOTICE$1"
23   if [ `echo "$1" | wc -l` -eq 1 ]; then
24     NOTICE="$NOTICE
25 "
26   fi
27 }
28
29 if [ ! -f $htpasswd ]; then
30         passwd=`makepasswd`
31         htpasswd -bc $htpasswd admin $passwd 2> /dev/null
32         notify "CN: amavis-stats URL is http://$hostname/amavis-stats/"
33         notify "CN:         User: admin"
34         notify "CN:         Password: $passwd"
35         notify "CN: Use \"htpasswd $htpasswd admin\" to change it."
36         notify ""
37 fi
38
39 # find out where the logs might lie
40 cant_figure_out() {
41   notify "CN: Can't figure out log file, edit $cron yourself!" 1>&2
42 }
43
44 sl=/etc/syslog.conf 
45 if grep -q /var/log/mail.info $cron; then
46   if [ ! -e "$sl" ]; then
47     # no syslog.conf
48     cant_figure_out
49   elif pgrep syslog-ng > /dev/null; then
50     # syslog-ng, I'm not parsing _that_.
51     cant_figure_out
52   elif ! grep -q '^mail.info[   ][      ]*-?/var/log/mail.info' $sl; then
53     # where oh where does mail.info go to?
54     log=$(awk '$1 == "mail.info" {print $2}' < $sl | sed 's/^-//' | head -1)
55     [ -z "$log" ] && \
56       log=$(awk '$1 == "mail.*" {print $2}' < $sl | sed 's/^-//' | head -1)
57     [ -z "$log" ] && \
58       log=$(awk '$1 ~ /mail\.info/ {print $2}' < $sl | sed 's/^-//' | head -1)
59     [ -z "$log" ] && \
60       log=$(awk '$1 ~ /mail\.\*/ {print $2}' < $sl | sed 's/^-//' | head -1)
61     if [ -z "$log" ]; then
62       cant_figure_out
63     else
64       sed "s,/var/log/mail.info,$log,g" < "$cron" > "${cron}.dpkg-tmp"
65       if ! cmp -s "${cron}.dpkg-tmp" "$cron"; then
66         cp "${cron}.dpkg-tmp" "$cron"
67         notify "CN: ${log} set as log file in ${cron}."
68       fi
69       rm "${cron}.dpkg-tmp"
70     fi
71   fi
72 fi  
73
74 # ash's [ continues after -a, so we need && here
75 if [ -f "$log" ] && [ "$(dirname $log)" = "/var/log/mail" ]; then
76   logdirgroup=$(ls -ld /var/log/mail | awk '{print $4}')
77   if [ "$logdirgroup" != "adm" ]; then
78     # whyever was it not adm? on woody it's mail, on sarge smmsp...?
79     # and i'm not going to give amavis-stats privileges for those
80     notify "CN: chgrp adm /var/log/mail (was \"$logdirgroup\")."
81     chgrp adm /var/log/mail 
82     #chgrp smmsp /var/log/mail
83     #addgroup amavis-stats smmsp > /dev/null
84   fi
85 fi
86
87 if ! grep -iq 'valid-user' $aconf; then
88   cat > ${aconf}.dpkg-tmp <<EOF
89 Alias /amavis-stats /usr/share/amavis-stats/
90
91 <Directory /usr/share/amavis-stats/>
92     Options +FollowSymLinks
93     AllowOverride None
94     order allow,deny
95     allow from all
96
97     AuthType Basic
98     AuthName amavis-stats
99     AuthUserFile $htpasswd
100
101     <Limit GET POST>
102         require valid-user
103     </Limit>
104 </Directory>
105 EOF
106   # If default debian file, don't bother to save it
107   if [ "$(md5sum $aconf|awk '{print $1}')" != "b6820ee83bc5b6386c72428f42950b6b" ]; then
108     bak="$aconf.dpkg-old.$(date '+%Y-%m-%d')"
109     if [ ! -f "$bak" ]; then
110       cp -p "${aconf}" "$bak" 
111       notify "CN: Old apache config saved as \"$bak\"."
112     fi
113   fi
114   cp "${aconf}.dpkg-tmp" "$aconf"
115   rm "${aconf}.dpkg-tmp"
116   notify "CN: Forced valid-user for amavis-stats web."
117   reload=1
118 fi
119
120 if [ "$(echo $NOTICE|wc -w)" -gt 0 ]; then
121   echo "Mailing upgrade output to root."
122   NOTICE="From: $package postinst script <root>
123 To: root
124 Subject: $package $version package install log
125 Date: $(/bin/date +'%a, %d %b %Y %H:%M:%S %z')
126  
127 Hello!
128
129 The $package package has been successfully installed on your computer.
130 For your convenience, a partial output of the last $package installation
131 is included below.
132
133 ----------
134 $NOTICE
135 ----------"
136
137   echo "$NOTICE" | /usr/sbin/sendmail -t &
138   sleep 1
139 fi
140
141
142 if [ ! -L /etc/apache2/conf.d/amavis-stats-cn.conf -a ! -f /etc/apache2/conf.d/amavis-stats-cn.conf ]; then
143   ln -sf /etc/amavis-stats/apache.conf /etc/apache2/conf.d/amavis-stats-cn.conf
144   reload_apache2="yes"
145 fi
146
147 if [ "$reload_apache2" = "yes" ]; then
148   if [ -x /usr/sbin/invoke-rc.d ]; then
149     [ -x /etc/init.d/apache2 ] && invoke-rc.d apache2 reload || true
150   else
151     [ -x /etc/init.d/apache2 ] && /etc/init.d/apache2 reload || true
152  fi
153 fi