]> git.donarmstrong.com Git - roundcube.git/blob - debian/roundcube-core.postinst
Fix symlink mess
[roundcube.git] / debian / roundcube-core.postinst
1 #!/bin/sh
2 # postinst script for roundcube
3 #
4 # see: dh_installdeb(1)
5
6 set -e
7
8 . /usr/share/debconf/confmodule
9 . /usr/share/dbconfig-common/dpkg/postinst 
10 dbc_generate_include=php:/etc/roundcube/debian-db.php
11 dbc_generate_include_owner="root:www-data"
12 dbc_generate_include_perms="640"
13 dbc_dbfile_owner="www-data:www-data"
14 dbc_dbfile_perms="0660"
15 dbc_dbuser=roundcube
16 dbc_dbname=roundcube
17
18 dbc_go roundcube $@
19
20 lighttpd_install() {
21         if [ ! -f /etc/lighttpd/conf-available/50-roundcube.conf ] ; then
22                 if [ ! -x /usr/sbin/lighty-enable-mod ] ; then
23                         echo "Lighttpd not installed, skipping"
24                 else
25                         ln -s /etc/roundcube/lighttpd.conf /etc/lighttpd/conf-available/50-roundcube.conf
26                         lighty-enable-mod roundcube
27                 fi
28         fi
29 }
30
31 apache_install() {
32         webserver=$1
33         if [ -d /etc/$webserver/conf.d ] && [ ! -e /etc/$webserver/conf.d/roundcube ]; then
34                ln -s /etc/roundcube/apache.conf /etc/$webserver/conf.d/roundcube
35         fi
36 }
37
38 fix_symlinks() {
39     # In 0.5.1-1, symlinks have been mangled. Before this version,
40     # /var/lib/roundcube and /usr/share/roundcube were using symlinks
41     # to each other:
42     #  /usr/share/roundcube/config   -> /var/lib/roundcube/config
43     #  /usr/share/roundcube/logs     -> /var/lib/roundcube/logs
44     #  /usr/share/roundcube/plugins  -> /var/lib/roundcube/plugins
45     #  /usr/share/roundcube/skins    -> /var/lib/roundcube/skins
46     #  /usr/share/roundcube/temp     -> /var/lib/roundcube/temp
47     #  /var/lib/roundcube/logs       -> ../../log/roundcube
48     #  /var/lib/roundcube/.htaccess  -> /usr/share/roundcube/.htaccess
49     #  /var/lib/roundcube/bin        -> /usr/share/roundcube/bin
50     #  /var/lib/roundcube/index.php  -> /usr/share/roundcube/bin
51     #  /var/lib/roundcube/program    -> /usr/share/roundcube/bin
52     #  /var/lib/roundcube/robots.txt -> /usr/share/roundcube/bin
53
54     # As of 0.7.2-6, the user is expected to use /var/lib/roundcube
55     # since /usr/share/roundcube does not contain the appropriate
56     # symlinks anymore.
57     #  /var/lib/roundcube/logs       -> ../../log/roundcube
58     #  /var/lib/roundcube/.htaccess  -> /usr/share/roundcube/.htaccess
59     #  /var/lib/roundcube/index.php  -> /usr/share/roundcube/bin
60     #  /var/lib/roundcube/program    -> /usr/share/roundcube/bin
61     #  /var/lib/roundcube/robots.txt -> /usr/share/roundcube/bin
62     #  /var/lib/roundcube/plugins/*  -> /usr/share/roundcube/plugins/*
63     #  /var/lib/roundcube/skins/*    -> /usr/share/roundcube/skins/*
64
65     # Because dpkg will not overwrite a symlink but follow them, an
66     # upgrade gives a confuse situation. We have the following
67     # symlinks in /usr/share/roundcube:
68     #  /usr/share/roundcube/plugins  -> /var/lib/roundcube/plugins
69     #  /usr/share/roundcube/skins    -> /var/lib/roundcube/skins
70     # And the following are not symlinks as they should be:
71     #  /var/lib/roundcube/plugins/*  -> /usr/share/roundcube/plugins/*
72     #  /var/lib/roundcube/skins/*    -> /usr/share/roundcube/skins/*
73
74     # We detect and fix that.
75     for dir in plugins skins; do
76         [ -L /usr/share/roundcube/${dir} ] || continue
77         echo "Fixing symbolic link /usr/share/roundcube/${dir}"
78         [ ! -L /var/lib/roundcube/${dir} ] || {
79             echo "/var/lib/roundcube/${dir} is not expected to be a symbolic link. Abort."
80             exit 1
81         }
82         # Remove the symbolic link and make a real directory.
83         rm /usr/share/roundcube/${dir}
84         mkdir /usr/share/roundcube/${dir}
85
86         # We move each subdirectory regardless we own them or not. The
87         # ideal solution would be to leave files installed by the user
88         # in /var/lib/roundcube/${dir} but since other packages (like
89         # roundcube-plugins and roundcube-plugins-extra) may have
90         # installed files, it is something difficult to track. We do
91         # not want each package to fix this mess. What's important is
92         # that user installation will keep working as expected,
93         # despite the move.
94         for d in /var/lib/roundcube/${dir}/*; do
95             mv ${d} /usr/share/roundcube/${dir}
96             ln -s /usr/share/roundcube/${dir}/${d##*/} /var/lib/roundcube/${dir}/${d##*/}
97         done
98     done
99 }
100
101 case "$1" in
102     configure)
103
104         # We try to fix an incomplete upgrade (see #610725 and #613586)
105         if [ -n "$2" ] && dpkg --compare-versions "$2" le 0.5.1+dfsg-5; then
106             db_get roundcube/dbconfig-upgrade || true
107             if [ x"$RET" = x"true" ]; then
108                 if [ x"$dbc_dbtype" = x"mysql" ]; then
109                     # Ok, we may need to do some upgrade steps.
110                     echo "Fixing MySQL installation..."
111                     if dpkg --compare-versions "$2" ge 0.5; then
112                         # Hack (ugly) to replace "mysql" by "mysql -f"
113                         real_mysql="$(which mysql)"
114                         workdir="$(mktemp -d)"
115                         oldpath="$PATH"
116                         cat <<EOF > "$workdir/mysql"
117 #!/bin/sh
118 "$real_mysql" "\$@" -f
119 EOF
120                         chmod +x "$workdir/mysql"
121                         PATH="$workdir:$PATH"
122                         dbc_mysql_exec_file \
123                             /usr/share/dbconfig-common/data/roundcube/upgrade/mysql/0.5-1 || true
124                         PATH="$oldpath"
125                         rm -rf "$workdir"
126                     fi
127                     cat <<EOF | while read command; do dbc_mysql_exec_command "$command" || true ; done
128 ALTER TABLE \`messages\` ADD INDEX \`index_index\` (\`user_id\`, \`cache_key\`, \`idx\`);
129 ALTER TABLE \`contacts\`ADD INDEX \`user_contacts_index\` (\`user_id\`,\`email\`);
130 ALTER TABLE \`identities\` ADD \`changed\` datetime NOT NULL DEFAULT '1000-01-01 00:00:00' AFTER \`user_id\`;
131 ALTER TABLE \`identities\` ADD INDEX \`user_identities_index\` (\`user_id\`, \`del\`);
132 EOF
133                 fi
134             fi
135         fi
136
137         # Fix bug 680917
138         if [ -n "$2" ] && dpkg --compare-versions "$2" lt 0.7.2-6; then
139             fix_symlinks
140         fi
141
142         CONFFILE=/etc/roundcube/main.inc.php
143         touch $CONFFILE.ucftmp
144         chmod 640 $CONFFILE.ucftmp
145         chown root:www-data $CONFFILE.ucftmp
146
147         db_get roundcube/hosts || true
148         hosts="$RET"
149         if [ "$hosts" != "" ]; then
150             hosts="array(\"$(echo $hosts | sed 's/ /\",\"/g')\")"
151         else
152             hosts="''"
153         fi
154
155         db_get roundcube/language || true
156         language="$RET"
157
158         # Get current 3DES key from /etc/roundcube/main.inc.php
159         [ -f /etc/roundcube/main.inc.php ] && {
160             deskey=$(sed -n "s+^\$rcmail_config\['des_key'\] = '\(.*\)';\$+\1+p" \
161                 /etc/roundcube/main.inc.php)
162         }
163         # If this is the default key, forget it !
164         [ "$deskey" = "rcmail-!24ByteDESkey*Str" ] && unset deskey
165         # Generate a new one
166         while [ -z "$deskey" ]; do
167             deskey=$(dd if=/dev/urandom bs=1 count=200 2> /dev/null | \
168                 tr -c -d '[A-Za-z0-9]' | sed -n 's/\(.\{24\}\).*/\1/p')
169         done
170
171         # Put hosts, language and key in main.inc.php
172         cat /usr/share/roundcube/main.inc.php.dist | while read line; do
173             case "$line" in
174                 "\$rcmail_config['default_host'] = "*)
175                     printf "\$rcmail_config['default_host'] = %s;\n" "${hosts}"
176                     ;;
177                 "\$rcmail_config['des_key'] = "*)
178                     printf "\$rcmail_config['des_key'] = '%s';\n" "${deskey}"
179                     ;;
180                 "\$rcmail_config['language'] = "*)
181                     printf "\$rcmail_config['language'] = '%s';\n" "${language}"
182                     ;;
183                 *)
184                     printf "%s\n" "$line"
185                     ;;
186             esac
187         done >> $CONFFILE.ucftmp
188
189         ucf --debconf-ok $CONFFILE.ucftmp $CONFFILE
190         chown root:www-data $CONFFILE
191         [ ! -f $CONFFILE.dpkg-dist ] || chown root:www-data $CONFFILE.dpkg-dist
192         rm -f $CONFFILE.ucftmp
193         
194         # Handle webserver reconfiguration/restart ; stolen from zabbix package
195         db_get roundcube/reconfigure-webserver || true
196         webservers="$RET"
197         restart=""
198
199         for webserver in $webservers; do
200             webserver=${webserver%,}
201             if [ "$webserver" = "lighttpd" ] ; then
202                 lighttpd_install
203             else
204                 apache_install $webserver
205             fi
206             test -x /usr/sbin/$webserver || continue
207             restart="$restart $webserver"
208         done
209
210         db_get roundcube/restart-webserver || true
211         res="$RET"
212         db_stop || true
213         if [ "$res" = "true" ]; then
214             for webserver in $restart; do
215                 webserver=${webserver%,}
216                 # Redirection of 3 is needed because Debconf uses it and it might
217                 # be inherited by webserver. See bug #446324.
218                 if [ -x /usr/sbin/invoke-rc.d ]; then
219                     invoke-rc.d $webserver reload 3>/dev/null || true
220                 else
221                     /etc/init.d/$webserver reload 3>/dev/null || true
222                 fi
223             done
224         fi
225
226         chown -R www-data:adm /var/log/roundcube
227         chmod -R 750 /var/log/roundcube
228         chown -R www-data:www-data /var/lib/roundcube/temp
229         chmod -R 750 /var/lib/roundcube/temp
230
231     ;;
232
233     abort-upgrade|abort-remove|abort-deconfigure)
234     ;;
235
236     *)
237         echo "postinst called with unknown argument \`$1'" >&2
238         exit 1
239     ;;
240 esac
241
242
243 #DEBHELPER#
244
245 exit 0
246
247