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