6979c4eea7d25e9ba4bed187ad623b483b0be69e
[php4-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 # Load debconf
24 . /usr/share/debconf/confmodule
25
26 # Include CARNet functions
27 . /usr/share/carnet-tools/functions.sh
28
29 PKG="php4-cn"
30 A2MODEDIR="/etc/apache2/mods-enabled"
31 EXTENSIONS="mysql"
32 PHP4DIR="/etc/php4"
33 PHP4CONFD="/etc/php4/conf.d"
34
35 need_restart=0
36
37
38 # phpmemlimit ()
39 #
40 #   Calculate memory size for PHP memory_limit option.
41 #
42 phpmemlimit () {
43
44     local memtotal memlimit
45     memtotal=`awk 'tolower($1) == "memtotal:" { printf("%i", $2/1024); }' /proc/meminfo`
46
47     if [ $memtotal -lt 512 ]; then memlimit=16; fi
48     if [ $memtotal -ge 512 ]; then memlimit=32; fi
49     if [ $memtotal -gt 1024 ]; then memlimit=64; fi
50
51     echo $memlimit
52 }
53
54
55 # Disable PHP5 module and enable Apache2 PHP4 module.
56 #
57 if [ -e /etc/apache2/apache2.conf ]; then
58         if [ -e "$A2MODEDIR/php5.load" ]; then
59                 cp_echo "CN: Disabling PHP5 module for Apache2 web server"
60                 a2dismod php5 >/dev/null || true
61                 need_restart=1
62         fi
63         if [ ! -e "$A2MODEDIR/php4.load" ] || [ ! -e "$A2MODEDIR/php4.conf" ]; then
64                 cp_echo "CN: Enabling PHP4 module for Apache2 web server"
65                 a2enmod php4 >/dev/null || true
66                 need_restart=1
67         fi
68 fi
69
70
71 # Check for PHP4 SAPI directories and configuration files.
72 #
73 for SAPI in apache2 cli; do
74
75         if [ ! -d "$PHP4DIR/$SAPI" ]; then
76                 cp_echo "CN: Creating configuration directory $PHP4DIR/$SAPI/"
77                 mkdir -p $PHP4DIR/$SAPI/
78         fi
79
80         ini_file="$PHP4DIR/$SAPI/php.ini"
81
82         if [ ! -f "$ini_file" ]; then
83         
84                 cp_echo "CN: Generating configuration file $ini_file"
85         
86                 ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
87                 if [ "$SAPI" = "cli" ]; then
88                     if [ -f "/usr/share/php4/php.ini-dist.cli" ]; then
89                         cat /usr/share/php4/php.ini-dist.cli > $ini_file_tmp
90                     fi
91                 else
92                     if [ -f "/usr/share/php4/php.ini-dist" ]; then
93                         cat /usr/share/php4/php.ini-dist > $ini_file_tmp
94                     fi
95                 fi
96                 cp_mv $ini_file_tmp $ini_file
97                 need_restart=1
98         fi
99 done
100
101
102 # Check for /etc/php4/conf.d/ directory.
103 #
104 if [ ! -d "$PHP4CONFD" ]; then
105         cp_echo "CN: Creating configuration directory $PHP4CONFD"
106         mkdir -p $PHP4CONFD/
107 fi
108
109
110 # Check if PHP4 extensions are enabled.
111 #
112 for php4ext in $EXTENSIONS; do
113
114         php4ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php4ext\.so"
115
116         # Remove extension entry from /etc/php4/(apache2|cli).ini
117         # configuration files.
118         for SAPI in apache2 cli; do
119
120                 ini_file="$PHP4DIR/$SAPI/php.ini"
121
122                 if [ -f "$ini_file" ]; then
123
124                         cp_echo "CN: Removing $php4ext extension from file $ini_file"
125
126                         cp_check_and_sed "$php4ext_re" \
127                                 "/$php4ext_re/d" \
128                                 "$ini_file" && need_restart=1 || true
129                 fi
130         done
131
132         # Check extension configuration in /etc/php4/conf.d/ directory.
133         if [ ! -f "$PHP4CONFD/$php4ext.ini" ]; then
134
135                 cp_echo "CN: Creating configuration file $PHP4CONFD/$php4ext.ini"
136                 
137                 php4ext_up=`echo $php4ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
138                 INITMP=`mktemp $PHP4CONFD/$php4ext.ini.tmp.XXXXXX`
139
140                 cp_echo "CN: Adding $php4ext extension to file $PHP4CONFD/$php4ext.ini"
141                 
142                 printf "# configuration for php %s module\nextension=%s\n" "${php4ext_up}" "${php4ext}" >> "$INITMP"
143                 cp_mv "$INITMP" "$PHP4CONFD/$php4ext.ini"
144                 
145                 need_restart=1
146         else
147
148                 if ! grep -q "$php4ext_re" "$PHP4CONFD/$php4ext.ini"; then
149
150                         cp_echo "CN: Adding $php4ext extension to file $PHP4CONFD/$php4ext.ini"
151                         
152                         INITMP=`mktemp $PHP4CONFD/$php4ext.ini.tmp.XXXXXX`
153                         cat "$PHP4CONFD/$php4ext.ini" > "$INITMP"
154                         echo "extension=$php4ext.so" >> "$INITMP"
155                         cp_mv "$INITMP" "$PHP4CONFD/$php4ext.ini"
156                         
157                         need_restart=1
158                 fi
159         fi
160 done
161
162
163 # Enable some PHP4 tweaks for Apache2 web server (/etc/php4/apache2/php.ini).
164 #
165 #   * upload_max_filesize = 256M
166 #   * post_max_size, memory_limit = depends on system memory, we are using
167 #     phpmemlimit() function.
168 #
169 for SAPI in apache2 cli; do
170
171         if [ ! -d "$PHP4DIR/$SAPI" ]; then
172                 continue
173         fi
174
175         db_get php4-cn/${SAPI} || true
176         if [ "$RET" = "true" ]; then
177         
178             ini_file="$PHP4DIR/$SAPI/php.ini"
179
180             cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
181         
182             phplimit="$(phpmemlimit)M"
183         
184             if [ -f "$ini_file" ]; then
185
186                 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
187                         's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
188                         "$ini_file" && need_restart=1 || true
189
190                 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
191                         "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
192                         "$ini_file" && need_restart=1 || true
193
194                 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
195                         "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
196                         "$ini_file" && need_restart=1 || true
197             fi
198
199             ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
200             if [ -f "$ini_file" ]; then
201                 cat $ini_file > $ini_file_tmp
202             fi
203
204             if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
205                 echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
206                 need_restart=1
207             fi
208             if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
209                 echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
210                 need_restart=1
211             fi
212             if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
213                 echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
214                 need_restart=1
215             fi
216             cp_mv "$ini_file_tmp" "$ini_file"
217
218             if [ -e "$ini_file_tmp" ]; then
219                 rm -f "$ini_file_tmp"
220             fi
221         fi
222 done
223
224
225 db_stop || true
226
227
228 # Restart Apache2 web server if needed.
229 #
230 if [ $need_restart -eq 1 ]; then
231         
232         # Check Apache2 web server configuration.
233         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
234
235                 # Restart Apache2 web server.
236                 if [ -x "/etc/init.d/apache2" ]; then
237                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
238                         invoke-rc.d apache2 force-reload || true
239                     else
240                         /etc/init.d/apache2 force-reload || true
241                     fi
242                 fi
243         else
244         
245                 # Something is broken.
246                 cp_echo "CN: Your Apache2 configuration is broken."
247                 cp_echo "CN: Please, check the service after the installation finishes!"
248         fi
249 fi
250
251
252 # Mail root
253 #
254 cp_mail "$PKG"
255
256 exit 0