zadnja verzija copyrighta
[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 # Load CARNet Tools
9 . /usr/share/carnet-tools/functions.sh
10
11 hostname=`hostname -f`
12 cron="/etc/cron.d/amavis-stats"
13 htpasswd="/usr/share/amavis-stats/.htpasswd"
14 aconf="/etc/amavis-stats/apache.conf"
15 # these two are used in mail
16 package="amavis-stats-cn"
17
18 if [ ! -f $htpasswd ]; then
19         passwd=`makepasswd`
20         htpasswd -bc $htpasswd admin $passwd 2> /dev/null
21         cp_echo "CN: amavis-stats URL is http://$hostname/amavis-stats/"
22         cp_echo "CN:         User: admin"
23         cp_echo "CN:         Password: $passwd"
24         cp_echo "CN: Use \"htpasswd $htpasswd admin\" to change it."
25         cp_echo ""
26 fi
27
28 # find out where the logs might lie
29 cant_figure_out() {
30   cp_echo "CN: Can't figure out log file, edit $cron yourself!" 1>&2
31 }
32
33 if [ -f /etc/rsyslog.conf ]; then
34   sl=/etc/rsyslog.conf 
35 else
36   sl=/etc/syslog.conf 
37 fi
38
39 if grep -q /var/log/mail.info $cron; then
40   if [ ! -e "$sl" ]; then
41     # no syslog.conf or rsyslog.conf
42     cant_figure_out
43   elif pgrep syslog-ng > /dev/null; then
44     # syslog-ng, I'm not parsing _that_.
45     cant_figure_out
46   elif ! grep -q '^mail.info[   ][      ]*-?/var/log/mail.info' $sl; then
47     # where oh where does mail.info go to?
48     log=$(awk '$1 == "mail.info" {print $2}' < $sl | sed 's/^-//' | head -1)
49     [ -z "$log" ] && \
50       log=$(awk '$1 == "mail.*" {print $2}' < $sl | sed 's/^-//' | head -1)
51     [ -z "$log" ] && \
52       log=$(awk '$1 ~ /mail\.info/ {print $2}' < $sl | sed 's/^-//' | head -1)
53     [ -z "$log" ] && \
54       log=$(awk '$1 ~ /mail\.\*/ {print $2}' < $sl | sed 's/^-//' | head -1)
55     if [ -z "$log" ]; then
56       cant_figure_out
57     else
58       sed "s,/var/log/mail.info,$log,g" < "$cron" > "${cron}.dpkg-tmp"
59       if ! cmp -s "${cron}.dpkg-tmp" "$cron"; then
60         cp "${cron}.dpkg-tmp" "$cron"
61         cp_echo "CN: ${log} set as log file in ${cron}."
62       fi
63       rm "${cron}.dpkg-tmp"
64     fi
65   fi
66 fi  
67
68 # ash's [ continues after -a, so we need && here
69 if [ -f "$log" ] && [ "$(dirname $log)" = "/var/log/mail" ]; then
70   logdirgroup=$(ls -ld /var/log/mail | awk '{print $4}')
71   if [ "$logdirgroup" != "adm" ]; then
72     # whyever was it not adm? on woody it's mail, on sarge smmsp...?
73     # and i'm not going to give amavis-stats privileges for those
74     cp_echo "CN: chgrp adm /var/log/mail (was \"$logdirgroup\")."
75     chgrp adm /var/log/mail 
76     #chgrp smmsp /var/log/mail
77     #addgroup amavis-stats smmsp > /dev/null
78   fi
79 fi
80
81 if ! grep -iq 'valid-user' $aconf; then
82   cat > ${aconf}.dpkg-tmp <<EOF
83 Alias /amavis-stats /usr/share/amavis-stats/
84
85 <Directory /usr/share/amavis-stats/>
86     Options +FollowSymLinks
87     AllowOverride None
88     order allow,deny
89     allow from all
90
91     AuthType Basic
92     AuthName amavis-stats
93     AuthUserFile $htpasswd
94
95     <Limit GET POST>
96         require valid-user
97     </Limit>
98 </Directory>
99 EOF
100   # If default debian file, don't bother to save it
101   if [ "$(md5sum $aconf|awk '{print $1}')" != "b6820ee83bc5b6386c72428f42950b6b" ]; then
102     bak="$aconf.dpkg-old.$(date '+%Y-%m-%d')"
103     if [ ! -f "$bak" ]; then
104       cp -p "${aconf}" "$bak" 
105       cp_echo "CN: Old apache config saved as \"$bak\"."
106     fi
107   fi
108   cp "${aconf}.dpkg-tmp" "$aconf"
109   rm "${aconf}.dpkg-tmp"
110   cp_echo "CN: Forced valid-user for amavis-stats web."
111   reload=1
112 fi
113
114 cp_mail $package
115
116 if [ ! -L /etc/apache2/conf.d/amavis-stats-cn.conf -a ! -f /etc/apache2/conf.d/amavis-stats-cn.conf ]; then
117   ln -sf /etc/amavis-stats/apache.conf /etc/apache2/conf.d/amavis-stats-cn.conf
118   reload_apache2="yes"
119 fi
120
121 if [ "$reload_apache2" = "yes" ]; then
122   if [ -x /usr/sbin/invoke-rc.d ]; then
123     [ -x /etc/init.d/apache2 ] && invoke-rc.d apache2 reload || true
124   else
125     [ -x /etc/init.d/apache2 ] && /etc/init.d/apache2 reload || true
126  fi
127 fi