r1: [svn-inject] Installing original source of squirrelmail-cn
[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.jpg/;
138         /^[     ]*\\\$org_logo[         ]*=/s/sm_logo\.png/sm_carnet_logo.jpg/;
139         /^[     ]*\\\$domain[   ]*=/s/mydomain\.com/$domainname/;
140         /^[     ]*\\\$default_charset[  ]*=/s/iso-8859-1/iso-8859-2/;
141         /^[     ]*\\\$org_logo_width[   ]*=/s/308/0/;
142         /^[     ]*\\\$org_logo_height[  ]*=/s/111/0/;
143         /^[     ]*\\\$squirrelmail_default_language[    ]*=/s/en_US/hr_HR/;
144         /^[     ]*\\\$data_dir[         ]*=/s/\.\.\/data\//\/var\/lib\/squirrelmail\/data\//;
145         "
146
147 # Check for our uw-imap package; if it is installed, assume it's used.
148 #
149 if dpkg -l uw-imap-ssl-cn 2>/dev/null | grep -q '^ii'; then
150   seddef="$seddef/^[    ]*\\\$imap_server_type[         ]*=/ {s/cyrus/uw/;s/other/uw/;}
151         "
152 fi
153
154 # Check for our dovecot package; if it is installed, assume it's used.
155 #
156 if dpkg -l dovecot-cn 2>/dev/null | grep -q '^ii'; then
157   seddef="$seddef/^[    ]*\\\$imap_server_type[         ]*=/ {s/cyrus/dovecot/;s/other/dovecot/;s/uw/dovecot/}
158         "
159 fi
160
161 # Would there be any changes, perhaps?
162 #
163 if cp_check_and_sed '.' "$seddef" "$CONF"; then
164         changed=1
165 fi
166
167 if grep -q x62 "$CONF"; then
168         really_old_config=1
169 fi
170
171
172 #
173 # MORE FIXES...
174 #
175
176 # Global $config_version;
177 #
178 if ! egrep -q '^[       ]*global[       ]+\$config_version' "$CONF"; then
179         really_old_config=1
180         #echo "CN: Adding \"global \$config_version;\" to config.php."
181         perl -p -e 'print "global \$config_version;\n" if (/^\s*\$config_version/);' \
182                 "$CONF" > "$CONF.tmp"
183         cp_mv "$CONF.tmp" "$CONF"
184 fi
185
186 # This was missing on some machines
187 #
188 if ! egrep -q '^[       ]*\$allow_thread_sort' $CONF; then
189         to_add='\$allow_thread_sort = false;\n'
190         cp_echo "CN: Adding \"\$allow_thread_sort\"."
191         perl -p -e 'if (! $a && /^\s*\$edit_name/) {
192                  $_ .= "'"$to_add"'"; $a = 1; }
193               elsif (! $a && /^\?>/) {
194                  $_ = "'"$to_add"'" . $_; $a = 1; }' \
195         $CONF > "$CONF.tmp"
196         cp_mv "$CONF.tmp" "$CONF"
197 fi
198
199 # Shouldn't use /etc/hostname, especially if it contains only the host part
200 # replace
201 #   $domain = implode('', file('/etc/hostname'));
202 # with
203 #   $domain = trim(implode('',file('/etc/'.(file_exists('/etc/mailname')?'mail':'host').'name')));
204 #
205 if egrep -q "^[[:blank:]]*\\\$domain[[:blank:]]*=[[:blank:]]*implode\\('',[[:blank:]]*file\\('/etc/hostname'\\)\\);" $CONF; then
206         nodots=
207         nomailname=
208         grep -q '\.' /etc/hostname || nodots=1
209         [ -e /etc/mailname ] || nomailname=1
210         if cp_check_and_sed "^[[:blank:]]*\\\$domain[[:blank:]]*=[[:blank:]]*implode\\('',[[:blank:]]*file\\('/etc/hostname'\\)\\);" \
211                    "s|^[ \t]*\\\$domain[ \t]*=[ \t]*implode('',[ \t]*file('/etc/hostname'));|\$domain = trim(implode('',file('/etc/'.(file_exists('/etc/mailname')?'mail':'host').'name')));|" \
212                    $CONF; then
213                 cp_echo "CN: config.php updated to use either /etc/mailname or /etc/hostname"
214         fi
215 fi
216
217
218 # Generate hr_HR locale if needed.
219 #
220 if ! egrep -q '^hr_HR ISO-8859-2\b' /etc/locale.gen; then
221         if [ -f /etc/locale.gen ]; then
222                 echo "hr_HR ISO-8859-2" >> /etc/locale.gen
223                 cp_echo "CN: Added hr_HR locale to /etc/locale.gen."
224         else
225                 echo "hr_HR ISO-8859-2" > /etc/locale.gen
226                 cp_echo "CN: /etc/locale.gen created."
227         fi
228         dpkg-reconfigure -f noninteractive locales
229 fi
230
231
232 # Check Apache2 web server for restart/reload.
233 #
234 if [ -n "$reload" ]; then
235
236         # Restart Apache2 web server.
237         if [ -x "/etc/init.d/apache2" ]; then
238                 if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
239                     invoke-rc.d apache2 "$reload" || exit $?
240                 else
241                     /etc/init.d/apache2 "$reload" || exit $?
242                 fi
243         fi
244 fi
245
246
247 # Warn user of really old config file
248 #
249 if [ "$really_old_config" ]; then
250         echo
251         echo "CN: You have a really old config file."
252         cp_echo -m "CN: Old \"$CONF\" detected during installation."
253         cp_echo "CN  The known bad options were fixed, but it is _strongly_ recommended that"
254         cp_echo "CN  you either copy over the new template, and reconfigure this package"
255         cp_echo
256         cp_echo "    cp $TMPL $CONF"
257         cp_echo "    dpkg-reconfigure squirrelmail-cn"
258         cp_echo
259         cp_echo "CN  or use squirrelmail-configure (conf.pl) to generate a fresh"
260         cp_echo "    ${CONF} file."
261         echo
262 fi
263
264
265 # Mailname info...
266 #
267 if [ "$nodots" -o "$nomailname" ]; then
268         cp_echo
269         cp_echo "CN: Make sure that /etc/mailname contains a fully qualified domain name"
270         cp_echo "CN  of this machine!  Sending mail via squirrelmail might fail if the FQDN"
271         cp_echo "CN  cannot be found in either /etc/mailname or /etc/hostname."
272         cp_echo "CN  Read mailname(5) man page for details."
273         cp_echo -mailonly "CN  If this machine receives mail for both user@`hostname -f` and "
274         cp_echo -mailonly "CN  user@`hostname -d`, you can also put just the domain in there."
275 fi
276
277 # Mail root
278 #
279 cp_mail "$PKG"
280
281 exit 0