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