r13:
[php5-cn.git] / debian / postinst
1 #!/bin/sh
2
3 set -e
4
5 [ "$DEBIAN_SCRIPT_DEBUG" ] && set -vx
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 # Include CARNet functions
24 . /usr/share/carnet-tools/functions.sh
25
26 PKG="php5-cn"
27 A2MODEDIR="/etc/apache2/mods-enabled"
28 EXTENSIONS="mysql"
29 PHP5DIR="/etc/php5"
30 PHP5CONFD="/etc/php5/conf.d"
31
32 need_restart=0
33
34
35 # Disable PHP4 module and enable Apache2 PHP5 module.
36 #
37 if [ -e /etc/apache2/apache2.conf ]; then
38         if [ -e "$A2MODEDIR/php4.load" ]; then
39                 cp_echo "CN: Disabling PHP4 module for Apache2 web server"
40                 a2dismod php4 >/dev/null || true
41                 need_restart=1
42         fi
43         if [ ! -e "$A2MODEDIR/php5.load" ] || [ ! -e "$A2MODEDIR/php5.conf" ]; then
44                 cp_echo "CN: Enabling PHP5 module for Apache2 web server"
45                 a2enmod php5 >/dev/null || true
46                 need_restart=1
47         fi
48 fi
49
50
51 # Check if PHP5 extensions are enabled.
52 #
53 for php5ext in $EXTENSIONS; do
54
55         php5ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php5ext\.so"
56
57         # Remove extension entry from /etc/php5/(apache|apache2|cgi|cli).ini
58         # configuration files.
59         for SAPI in apache apache2 cgi cli; do
60
61                 if [ -d "$PHP5DIR/$SAPI" ]; then
62                 
63                     ini_file="$PHP5DIR/$SAPI/php.ini"
64
65                     if [ -f "$ini_file" ]; then
66
67                         cp_echo "CN: Removing $php5ext extension from file $ini_file"
68
69                         cp_check_and_sed "$php5ext_re" \
70                                 "/$php5ext_re/d" \
71                                 "$ini_file" && need_restart=1 || true
72                     fi
73                 fi
74         done
75
76         # Check $PHP5CONFD directory.
77         if [ ! -d "$PHP5CONFD" ]; then
78                 cp_echo "CN: Creating configuration directory $PHP5CONFD"
79                 mkdir $PHP5CONFD
80         fi
81
82         # Check extension configuration in /etc/php5/conf.d/ directory.
83         if [ ! -f "$PHP5CONFD/$php5ext.ini" ]; then
84
85                 cp_echo "CN: Creating configuration file $PHP5CONFD/$php5ext.ini"
86                 
87                 php5ext_up=`echo $php5ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
88                 INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
89
90                 cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
91                 
92                 echo -e "# configuration for php $php5ext_up module\nextension=$php5ext.so" >> "$INITMP"
93                 cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
94                 
95                 need_restart=1
96         else
97
98                 if ! grep -q "$php5ext_re" "$PHP5CONFD/$php5ext.ini"; then
99
100                         cp_echo "CN: Adding $php5ext extension to file $PHP5CONFD/$php5ext.ini"
101                         
102                         INITMP=`mktemp $PHP5CONFD/$php5ext.ini.tmp.XXXXXX`
103                         cat "$PHP5CONFD/$php5ext.ini" > "$INITMP"
104                         echo "extension=$php5ext.so" >> "$INITMP"
105                         cp_mv "$INITMP" "$PHP5CONFD/$php5ext.ini"
106                         
107                         need_restart=1
108                 fi
109         fi
110 done
111
112
113 # Enable some PHP5 tweaks for Apache2 web server (/etc/php5/apache2/php.ini).
114 #
115 #   * upload_max_filesize = 256M
116 #   * post_max_size = 256M
117 #   * memory_limit = 256M
118 #
119 for SAPI in apache apache2 cgi cli; do
120
121         if [ ! -d "$PHP5DIR/$SAPI" ]; then
122                 continue
123         fi
124
125         ini_file="$PHP5DIR/$SAPI/php.ini"
126
127         cp_echo "CN: Checking and enabling some CARNet specific parameters in file $ini_file"
128         
129         if [ -f "$ini_file" ]; then
130
131                 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=[[:space:]]*2M" \
132                         's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
133                         "$ini_file" && need_restart=1 || true
134
135                 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=[[:space:]]*8M" \
136                         's/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = 256M/' \
137                         "$ini_file" && need_restart=1 || true
138
139                 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=[[:space:]]*16M" \
140                         's/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = 256M/' \
141                         "$ini_file" && need_restart=1 || true
142         fi
143                 
144         ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
145         if [ -f "$ini_file" ]; then
146                 cat $ini_file > $ini_file_tmp
147         fi
148
149         if ! egrep -q "^[[:space:]]*(upload_max_filesize|post_max_size|memory_limit)[[:space:]]*=" $ini_file_tmp; then
150
151                 if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
152                         echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
153                 fi
154                 if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
155                         echo "post_max_size = 256MB" >> "$ini_file_tmp"
156                 fi
157                 if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
158                         echo "memory_limit = 256M" >> "$ini_file_tmp"
159                 fi
160                 cp_mv "$ini_file_tmp" "$ini_file"
161         
162                 need_restart=1
163         fi
164         
165         if [ -e "$ini_file_tmp" ]; then
166                 rm -f "$ini_file_tmp"
167         fi
168 done
169
170
171 # Restart Apache2 web server if needed.
172 #
173 if [ $need_restart -eq 1 ]; then
174         
175         # Check Apache2 web server configuration.
176         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
177
178                 # Restart Apache2 web server.
179                 if [ -x "/etc/init.d/apache2" ]; then
180                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
181                         invoke-rc.d apache2 force-reload || true
182                     else
183                         /etc/init.d/apache2 force-reload || true
184                     fi
185                 fi
186         else
187         
188                 # Something is broken.
189                 cp_echo "CN: Your Apache2 configuration is broken."
190                 cp_echo "CN: Please, check the service after the installation finishes!"
191         fi
192 fi
193
194
195 # Mail root
196 #
197 cp_mail "$PKG"
198
199 exit 0