r4:
[squirrelmail-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -x 
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 PKG=squirrelmail-cn
24 domainname=`dnsdomainname`
25 FQDN=`hostname --fqdn`
26
27 # Include CARNet functions
28 . /usr/share/carnet-tools/functions.sh
29 unset CP_ECHO_RETURN
30
31 mv_old_backups() {
32
33         # Clean up /etc, nove old jun^H^H^Hbackups where they belong
34         glob="$1.old.*-*-*.*:*:*"
35         
36         # ${glob%/*} (dirname $glob) does not work for files in root
37         if [ -n "$(find ${glob%/*} -name ${glob##*/})" ]; then
38                 for i in $(find ${glob%/*} -name ${glob##*/})
39                 do
40                         cp_backup_conffile "$i" "$2"
41                         rm -f "$i"
42                 done
43         fi
44 }
45
46
47 reload=""
48 DATE=`date +%Y-%m-%d.%H:%M:%S`
49 CONFDIR=/etc/squirrelmail
50 CONF=$CONFDIR/apache.conf
51
52 backup_name=squirrelmail_apache.conf
53 mv_old_backups $CONF $backup_name
54
55 # Check Apache2 web server configuration.
56 #
57 if [ -f "$CONF" ]; then
58
59         # Backup first.
60         if cp_check_and_backup "$CONF" "$backup_name"; then
61                 cp_echo "CN: Old $CONF saved as \"/var/backups/$backup_name.bak\"."
62         fi
63
64         # Check if we already modified apache.conf
65         if ! egrep -q "^[       ]+RewriteRule \. https" "$CONF"; then
66                 
67                 echo -n "CN: Updating Apache2 configuration for Squirrelmail"
68                 perl -n -e 'print if ! m|^Alias\s*/webmail|' "$CONF" > $CONF.tmp
69                 
70                 cp-update -t squirrelmail-cn $CONF.tmp <<-EOF
71         # Force SSL for /webmail -> you can still use /squirrelmail
72         Alias /webmail /usr/share/squirrelmail
73         
74         <IfModule mod_rewrite.c>
75           <IfModule mod_ssl.c>
76             <Location /webmail>
77               RewriteEngine on
78               RewriteCond %{HTTPS} !=on
79               RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}  [L]
80             </Location>
81           </IfModule>
82         </IfModule>
83         
84         #
85         # WARNING: This file is automatically included in each VirtualHost
86         # entry you might have.  Do not enable the VirtualHost example provided
87         # in this file, it WILL break your Apache configuration. Copy the
88         # VirtualHost section to the standard webserver configuration file
89         # instead.
90         #
91         EOF
92                 cp_mv $CONF.tmp $CONF
93
94                 # This will break if cp_backup_conffile ever changes destination path
95                 if ! cmp -s "$CONF" "/var/backups/$backup_name.bak"; then
96                         echo "."
97                         cp_echo "CN: Updated Apache2 configuration for Squirrelmail."
98                         reload="reload"
99                 else
100                         echo "... no changes."
101                 fi
102         fi
103 fi
104
105
106 hconf=/etc/apache2/apache2.conf
107 sconf=/etc/apache2/sites-available/ssl
108
109 # Include Squirrelmail apache.conf in Apache2 web server configuration
110 # (directory /etc/apache2/conf.d/), but only if we are not present in 
111 # Apache2 configuration at all.
112 #
113 # We're using listconffiles from apache-common Debian package. Yes, 
114 # it works with Apache2 configurations too.
115 #
116 apache2listconf=`/usr/share/apache/listconffiles -V $hconf | tr '\a' ' ' | grep "$CONF"` || true
117 apache2squirrel=`ls -l /etc/apache2/conf.d/ | grep "$CONF"` || true
118
119 if [ -z "$apache2listconf" -a -z "$apache2squirrel" ]; then
120         ln -s "$CONF" /etc/apache2/conf.d/squirrelmail-cn.conf
121         reload=reload
122 fi
123
124
125 # Initial CARNet config tweaks.
126 #
127 CONF="$CONFDIR/config.php"
128 TMPL="$CONFDIR/config_default.php"
129 mv_old_backups "$CONF" "`basename $CONF`"
130 if cp_check_and_backup "$CONF"; then
131         cp_echo "CN: Old $CONF saved as \"/var/backups/${CONF##*/}.bak\"."
132 fi
133
134 # We use sed to replace old defaults with ours.
135 # Must escape \$ because of shell expansion.
136 #
137 seddef="/^[     ]*\\\$org_logo[         ]*=/s/sm_logo\.jpg/sm_carnet_logo.png/;
138         /^[     ]*\\\$org_logo[         ]*=/s/sm_logo\.png/sm_carnet_logo.png/;
139         /^[     ]*\\\$org_logo[         ]*=/s/sm_carnet_logo\.jpg/sm_carnet_logo.png/;
140         /^[     ]*\\\$domain[   ]*=/s/mydomain\.com/$domainname/;
141         /^[     ]*\\\$default_charset[  ]*=/s/iso-8859-1/iso-8859-2/;
142         /^[     ]*\\\$org_logo_width[   ]*=/s/308/0/;
143         /^[     ]*\\\$org_logo_height[  ]*=/s/111/0/;
144         /^[     ]*\\\$squirrelmail_default_language[    ]*=/s/en_US/hr_HR/;
145         /^[     ]*\\\$data_dir[         ]*=/s/\.\.\/data\//\/var\/lib\/squirrelmail\/data\//;
146         "
147
148 # Check for our uw-imap package; if it is installed, assume it's used.
149 #
150 if dpkg -l uw-imap-ssl-cn 2>/dev/null | grep -q '^ii'; then
151   seddef="$seddef/^[    ]*\\\$imap_server_type[         ]*=/ {s/cyrus/uw/;s/other/uw/;}
152         "
153 fi
154
155 # Check for our dovecot package; if it is installed, assume it's used.
156 #
157 if dpkg -l dovecot-cn 2>/dev/null | grep -q '^ii'; then
158   seddef="$seddef/^[    ]*\\\$imap_server_type[         ]*=/ {s/cyrus/dovecot/;s/other/dovecot/;s/uw/dovecot/}
159         "
160 fi
161
162 # Would there be any changes, perhaps?
163 #
164 if cp_check_and_sed '.' "$seddef" "$CONF"; then
165         changed=1
166 fi
167
168 if grep -q x62 "$CONF"; then
169         really_old_config=1
170 fi
171
172
173 #
174 # MORE FIXES...
175 #
176
177 # Global $config_version;
178 #
179 if ! egrep -q '^[       ]*global[       ]+\$config_version' "$CONF"; then
180         really_old_config=1
181         #echo "CN: Adding \"global \$config_version;\" to config.php."
182         perl -p -e 'print "global \$config_version;\n" if (/^\s*\$config_version/);' \
183                 "$CONF" > "$CONF.tmp"
184         cp_mv "$CONF.tmp" "$CONF"
185 fi
186
187 # This was missing on some machines
188 #
189 if ! egrep -q '^[       ]*\$allow_thread_sort' $CONF; then
190         to_add='\$allow_thread_sort = false;\n'
191         cp_echo "CN: Adding \"\$allow_thread_sort\"."
192         perl -p -e 'if (! $a && /^\s*\$edit_name/) {
193                  $_ .= "'"$to_add"'"; $a = 1; }
194               elsif (! $a && /^\?>/) {
195                  $_ = "'"$to_add"'" . $_; $a = 1; }' \
196         $CONF > "$CONF.tmp"
197         cp_mv "$CONF.tmp" "$CONF"
198 fi
199
200 # Shouldn't use /etc/hostname, especially if it contains only the host part
201 # replace
202 #   $domain = implode('', file('/etc/hostname'));
203 # with
204 #   $domain = trim(implode('',file('/etc/'.(file_exists('/etc/mailname')?'mail':'host').'name')));
205 #
206 if egrep -q "^[[:blank:]]*\\\$domain[[:blank:]]*=[[:blank:]]*implode\\('',[[:blank:]]*file\\('/etc/hostname'\\)\\);" $CONF; then
207         nodots=
208         nomailname=
209         grep -q '\.' /etc/hostname || nodots=1
210         [ -e /etc/mailname ] || nomailname=1
211         if cp_check_and_sed "^[[:blank:]]*\\\$domain[[:blank:]]*=[[:blank:]]*implode\\('',[[:blank:]]*file\\('/etc/hostname'\\)\\);" \
212                    "s|^[ \t]*\\\$domain[ \t]*=[ \t]*implode('',[ \t]*file('/etc/hostname'));|\$domain = trim(implode('',file('/etc/'.(file_exists('/etc/mailname')?'mail':'host').'name')));|" \
213                    $CONF; then
214                 cp_echo "CN: config.php updated to use either /etc/mailname or /etc/hostname"
215         fi
216 fi
217
218
219 # Generate hr_HR locale if needed.
220 #
221 if ! egrep -q '^hr_HR ISO-8859-2\b' /etc/locale.gen; then
222         if [ -f /etc/locale.gen ]; then
223                 echo "hr_HR ISO-8859-2" >> /etc/locale.gen
224                 cp_echo "CN: Added hr_HR locale to /etc/locale.gen."
225         else
226                 echo "hr_HR ISO-8859-2" > /etc/locale.gen
227                 cp_echo "CN: /etc/locale.gen created."
228         fi
229         dpkg-reconfigure -f noninteractive locales
230 fi
231
232
233 # Check Apache2 web server for restart/reload.
234 #
235 if [ -n "$reload" ]; then
236
237         # Restart Apache2 web server.
238         if [ -x "/etc/init.d/apache2" ]; then
239                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
240                     invoke-rc.d apache2 "$reload" || exit $?
241                 else
242                     /etc/init.d/apache2 "$reload" || exit $?
243                 fi
244         fi
245 fi
246
247
248 # Warn user of really old config file
249 #
250 if [ "$really_old_config" ]; then
251         echo
252         echo "CN: You have a really old config file."
253         cp_echo -m "CN: Old \"$CONF\" detected during installation."
254         cp_echo "CN  The known bad options were fixed, but it is _strongly_ recommended that"
255         cp_echo "CN  you either copy over the new template, and reconfigure this package"
256         cp_echo
257         cp_echo "    cp $TMPL $CONF"
258         cp_echo "    dpkg-reconfigure squirrelmail-cn"
259         cp_echo
260         cp_echo "CN  or use squirrelmail-configure (conf.pl) to generate a fresh"
261         cp_echo "    ${CONF} file."
262         echo
263 fi
264
265
266 # Mailname info...
267 #
268 if [ "$nodots" -o "$nomailname" ]; then
269         cp_echo
270         cp_echo "CN: Make sure that /etc/mailname contains a fully qualified domain name"
271         cp_echo "CN  of this machine!  Sending mail via squirrelmail might fail if the FQDN"
272         cp_echo "CN  cannot be found in either /etc/mailname or /etc/hostname."
273         cp_echo "CN  Read mailname(5) man page for details."
274         cp_echo -mailonly "CN  If this machine receives mail for both user@`hostname -f` and "
275         cp_echo -mailonly "CN  user@`hostname -d`, you can also put just the domain in there."
276 fi
277
278 # Mail root
279 #
280 cp_mail "$PKG"
281
282 exit 0