ac161dcce98a331a5e73c74e94adeac4cf5708f4
[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     # Clean up /etc, nove old jun^H^H^Hbackups where they belong
36     glob="$1.old.*-*-*.*:*:*"
37
38     # ${glob%/*} (dirname $glob) does not work for files in root
39     if [ -n "$(find ${glob%/*} -name ${glob##*/})" ]; then
40         for i in $(find ${glob%/*} -name ${glob##*/})
41         do
42             cp_backup_conffile "$i" "$2"
43             rm -f "$i"
44         done
45     fi
46 }
47
48 # listconffiles ()
49 #
50 #   Recursively walks /etc/apache2/apache2.conf for Include directives.
51 #   Returns all configfiles so defined.
52 #
53 listconffiles () {
54     local i incs
55
56     incs=`awk 'tolower($1) == "include" { sub("/$","/*",$2); print $2; }' $1`
57     if [ -n "$incs" ]; then
58         for i in $incs; do
59             if [ -e "$i" ]; then echo "$i"; listconffiles "$i"; fi
60         done
61     fi
62 }
63
64 # genlocales ()
65 #
66 #   Update/generate localisation files from templates if locales-all
67 #   is not installed.
68 #
69 genlocales () {
70     if [ -f /usr/lib/locales-all/supported.tar.lzma ] ; then
71         cp_echo "CN: locales-all installed, skipping locales generation."
72     else
73         locale-gen
74     fi
75 }
76
77
78 need_restart=0
79 DATE=`date +%Y-%m-%d.%H:%M:%S`
80 CONFDIR=/etc/squirrelmail
81 CONF=$CONFDIR/apache.conf
82
83 backup_name=squirrelmail_apache.conf
84 mv_old_backups $CONF $backup_name
85
86 # Check Apache2 web server configuration.
87 #
88 if [ -f "$CONF" ]; then
89
90     # Backup first.
91     if cp_check_and_backup "$CONF" "$backup_name"; then
92         cp_echo "CN: Old $CONF saved as \"/var/backups/$backup_name.bak\"."
93     fi
94
95     # Check if we already modified apache.conf
96     if ! egrep -q "^[   ]+RewriteRule \. https" "$CONF"; then
97         echo -n "CN: Updating Apache2 configuration for Squirrelmail"
98         perl -n -e 'print if ! m|^Alias\s*/webmail|' "$CONF" > $CONF.tmp
99
100         cp-update -t squirrelmail-cn $CONF.tmp <<-EOF
101 # Force SSL for /webmail -> you can still use /squirrelmail
102 Alias /webmail /usr/share/squirrelmail
103
104 <IfModule mod_rewrite.c>
105   <IfModule mod_ssl.c>
106     <Location /webmail>
107       RewriteEngine on
108       RewriteCond %{HTTPS} !=on
109       RewriteRule . https://%{HTTP_HOST}%{REQUEST_URI}  [L]
110     </Location>
111   </IfModule>
112 </IfModule>
113
114 #
115 # WARNING: This file is automatically included in each VirtualHost
116 # entry you might have.  Do not enable the VirtualHost example provided
117 # in this file, it WILL break your Apache configuration. Copy the
118 # VirtualHost section to the standard webserver configuration file
119 # instead.
120 #
121 EOF
122         cp_mv $CONF.tmp $CONF
123
124         # This will break if cp_backup_conffile ever changes destination path
125         if ! cmp -s "$CONF" "/var/backups/$backup_name.bak"; then
126             echo "."
127             cp_echo "CN: Updated Apache2 configuration for Squirrelmail."
128             need_restart=1
129         else
130             echo "... no changes."
131         fi
132     fi
133 fi
134
135
136 hconf=/etc/apache2/apache2.conf
137 sconf=/etc/apache2/sites-available/ssl
138
139 # Include Squirrelmail apache.conf in Apache2 web server configuration
140 # (directory /etc/apache2/conf.d/), but only if we are not present in 
141 # Apache2 configuration at all.
142 #
143 # We're not using listconffiles from apache-common Debian package.
144 # apache-common is no longer available in Debian Lenny. We're using
145 # listconffiles() function instead.
146 #
147 apache2listconf=`listconffiles "$hconf" | grep "$CONF"` || true
148 apache2squirrel=`ls -l /etc/apache2/conf.d/ | grep "$CONF"` || true
149
150 if [ -z "$apache2listconf" -a -z "$apache2squirrel" ]; then
151     ln -s "$CONF" /etc/apache2/conf.d/squirrelmail-cn.conf
152     need_restart=1
153 fi
154
155
156 # Initial CARNet config tweaks.
157 #
158 CONF="$CONFDIR/config.php"
159 TMPL="$CONFDIR/config_default.php"
160 mv_old_backups "$CONF" "`basename $CONF`"
161 if cp_check_and_backup "$CONF"; then
162     cp_echo "CN: Old $CONF saved as \"/var/backups/${CONF##*/}.bak\"."
163 fi
164
165 # We use sed to replace old defaults with ours.
166 # Must escape \$ because of shell expansion.
167 #
168 seddef="/^[     ]*\\\$org_logo[         ]*=/s/sm_logo\.jpg/sm_carnet_logo.png/;
169         /^[     ]*\\\$org_logo[         ]*=/s/sm_logo\.png/sm_carnet_logo.png/;
170         /^[     ]*\\\$org_logo[         ]*=/s/sm_carnet_logo\.jpg/sm_carnet_logo.png/;
171         /^[     ]*\\\$domain[   ]*=/s/mydomain\.com/$domainname/;
172         /^[     ]*\\\$default_charset[  ]*=/s/iso-8859-1/iso-8859-2/;
173         /^[     ]*\\\$org_logo_width[   ]*=/s/308/0/;
174         /^[     ]*\\\$org_logo_height[  ]*=/s/111/0/;
175         /^[     ]*\\\$squirrelmail_default_language[    ]*=/s/en_US/hr_HR/;
176         /^[     ]*\\\$data_dir[         ]*=/s/\.\.\/data\//\/var\/lib\/squirrelmail\/data\//;
177         "
178
179 # Left frame size to 220px.
180 # $default_left_size = '220';
181 #
182 db_get squirrelmail-cn/leftwidth || true
183 if [ "$RET" = "true" ]; then
184     seddef="$seddef/^[  ]*\\\$default_left_size[        ]*=/s/'[0-9]*'/'220'/;
185         "
186 fi
187
188 # Change CSS file for Squirrelmail.
189 # $theme_css = SM_PATH . 'themes/css/squirrelmail-cn.css';
190 #
191 db_get squirrelmail-cn/themecss || true
192 if [ "$RET" = "true" ]; then
193     seddef="$seddef/^[  ]*\\\$theme_css[        ]*=/s/=.*/= SM_PATH . 'themes\/css\/squirrelmail-cn.css';/;
194         "
195 fi
196
197 db_stop || true
198
199 # Check for our dovecot package; if it is installed, assume it's used.
200 #
201 if dpkg -l dovecot-cn 2>/dev/null | grep -q '^ii'; then
202     seddef="$seddef/^[  ]*\\\$imap_server_type[         ]*=/ {s/cyrus/dovecot/;s/other/dovecot/;s/uw/dovecot/}
203         "
204 fi
205
206 # Would there be any changes, perhaps?
207 #
208 if cp_check_and_sed '.' "$seddef" "$CONF"; then
209     need_restart=1
210 fi
211
212
213 # Check /etc/hostname and /etc/mailname, because
214 #   $domain = trim(implode('', file('/etc/'.(file_exists('/etc/mailname')?'mail':'host').'name')));
215 #
216 nodots=
217 nomailname=
218 grep -q '\.' /etc/hostname || nodots=1
219 [ -e /etc/mailname ] || nomailname=1
220
221
222 # Check for hr_HR locale.
223 #
224 if [ -f /etc/locale.gen ]; then
225     if ! egrep -q '^hr_HR ISO-8859-2\b' /etc/locale.gen; then
226         echo "hr_HR ISO-8859-2" >> /etc/locale.gen
227         cp_echo "CN: Added hr_HR locale to /etc/locale.gen."
228         genlocales
229         need_restart=1
230     fi
231 else
232     echo "hr_HR ISO-8859-2" > /etc/locale.gen
233     cp_echo "CN: /etc/locale.gen created."
234     genlocales
235     need_restart=1
236 fi
237
238
239 # Check Apache2 web server for restart.
240 #
241 if [ $need_restart -eq 1 ]; then
242     if apache2ctl configtest 2>/dev/null; then
243         invoke-rc.d apache2 force-reload || true
244     else
245         cp_echo "CN: Your Apache2 configuration is either broken or nonexistant."
246     fi
247 fi
248
249
250 # Mailname info...
251 #
252 if [ "$nodots" -o "$nomailname" ]; then
253     cp_echo
254     cp_echo "CN: Make sure that /etc/mailname contains a fully qualified domain name"
255     cp_echo "CN  of this machine!  Sending mail via squirrelmail might fail if the FQDN"
256     cp_echo "CN  cannot be found in either /etc/mailname or /etc/hostname."
257     cp_echo "CN  Read mailname(5) man page for details."
258     cp_echo -mailonly "CN  If this machine receives mail for both user@`hostname -f` and "
259     cp_echo -mailonly "CN  user@`hostname -d`, you can also put just the domain in there."
260 fi
261
262 # Mail root
263 #
264 cp_mail "$PKG"
265
266 # dh_installdeb will replace this with shell code automatically
267 # generated by other debhelper scripts.
268
269 #DEBHELPER#
270
271 exit 0