debian/postinst:
[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 # phpinivalidate ()
55 #
56 #   Validate php.ini values.
57 #
58 phpinivalidate () {
59
60     local ini_file
61     ini_file="$1"
62
63     awk -F'[[:space:]]*=[[:space:]]*' \
64         '/^[[:space:]]*[[:alpha:]_]+[[:space:]]*=[[:space:]]*[[:digit:]]+[MGK]B/ {print $1" = "$2}' \
65         "$ini_file"
66 }
67
68
69 # Disable PHP5 module and enable Apache2 PHP4 module.
70 #
71 if [ -e /etc/apache2/apache2.conf ]; then
72         if [ -e "$A2MODEDIR/php5.load" ]; then
73                 cp_echo "CN: Disabling PHP5 module for Apache2 web server"
74                 a2dismod php5 >/dev/null || true
75                 need_restart=1
76         fi
77         if [ ! -e "$A2MODEDIR/php4.load" ] || [ ! -e "$A2MODEDIR/php4.conf" ]; then
78                 cp_echo "CN: Enabling PHP4 module for Apache2 web server"
79                 a2enmod php4 >/dev/null || true
80                 need_restart=1
81         fi
82 fi
83
84
85 # Check for PHP4 SAPI directories and configuration files.
86 #
87 for SAPI in apache2 cli; do
88
89         if [ ! -d "$PHP4DIR/$SAPI" ]; then
90                 cp_echo "CN: Creating configuration directory $PHP4DIR/$SAPI/"
91                 mkdir -p $PHP4DIR/$SAPI/
92         fi
93
94         ini_file="$PHP4DIR/$SAPI/php.ini"
95
96         if [ ! -f "$ini_file" ]; then
97         
98                 cp_echo "CN: Generating configuration file $ini_file"
99         
100                 ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
101                 if [ "$SAPI" = "cli" ]; then
102                     if [ -f "/usr/share/php4/php.ini-dist.cli" ]; then
103                         cat /usr/share/php4/php.ini-dist.cli > $ini_file_tmp
104                     fi
105                 else
106                     if [ -f "/usr/share/php4/php.ini-dist" ]; then
107                         cat /usr/share/php4/php.ini-dist > $ini_file_tmp
108                     fi
109                 fi
110                 cp_mv $ini_file_tmp $ini_file
111                 need_restart=1
112         fi
113         chmod 644 $ini_file
114 done
115
116
117 # Check for /etc/php4/conf.d/ directory.
118 #
119 if [ ! -d "$PHP4CONFD" ]; then
120         cp_echo "CN: Creating configuration directory $PHP4CONFD"
121         mkdir -p $PHP4CONFD/
122 fi
123
124
125 # Check if PHP4 extensions are enabled.
126 #
127 for php4ext in $EXTENSIONS; do
128
129         php4ext_re="^[[:space:]]*extension[[:space:]]*=[[:space:]]*$php4ext\.so"
130
131         # Remove extension entry from /etc/php4/(apache2|cli).ini
132         # configuration files.
133         for SAPI in apache2 cli; do
134
135                 ini_file="$PHP4DIR/$SAPI/php.ini"
136
137                 if [ -f "$ini_file" ]; then
138
139                         cp_echo "CN: Removing $php4ext extension from file $ini_file"
140
141                         cp_check_and_sed "$php4ext_re" \
142                                 "/$php4ext_re/d" \
143                                 "$ini_file" && need_restart=1 || true
144                 fi
145         done
146
147         # Check extension configuration in /etc/php4/conf.d/ directory.
148         if [ ! -f "$PHP4CONFD/$php4ext.ini" ]; then
149
150                 cp_echo "CN: Creating configuration file $PHP4CONFD/$php4ext.ini"
151                 
152                 php4ext_up=`echo $php4ext | tr [:lower:] [:upper:] | sed 's/Y/y/'`
153                 INITMP=`mktemp $PHP4CONFD/$php4ext.ini.tmp.XXXXXX`
154
155                 cp_echo "CN: Adding $php4ext extension to file $PHP4CONFD/$php4ext.ini"
156                 
157                 printf "# configuration for php %s module\nextension=%s.so\n" "${php4ext_up}" "${php4ext}" >> "$INITMP"
158                 cp_mv "$INITMP" "$PHP4CONFD/$php4ext.ini"
159                 
160                 need_restart=1
161         else
162
163                 if ! grep -q "$php4ext_re" "$PHP4CONFD/$php4ext.ini"; then
164
165                         cp_echo "CN: Adding $php4ext extension to file $PHP4CONFD/$php4ext.ini"
166                         
167                         INITMP=`mktemp $PHP4CONFD/$php4ext.ini.tmp.XXXXXX`
168                         cat "$PHP4CONFD/$php4ext.ini" > "$INITMP"
169                         echo "extension=$php4ext.so" >> "$INITMP"
170                         cp_mv "$INITMP" "$PHP4CONFD/$php4ext.ini"
171                         
172                         need_restart=1
173                 fi
174         fi
175         chmod 644 $PHP4CONFD/$php4ext.ini
176 done
177
178
179 # Enable some PHP4 tweaks for Apache2 web server (/etc/php4/apache2/php.ini).
180 #
181 #   * upload_max_filesize = 256M
182 #   * post_max_size, memory_limit = depends on system memory, we are using
183 #     phpmemlimit() function.
184 #
185 for SAPI in apache2 cli; do
186
187         if [ ! -d "$PHP4DIR/$SAPI" ]; then
188                 continue
189         fi
190
191         ini_file="$PHP4DIR/$SAPI/php.ini"
192         db_get php4-cn/${SAPI} || true
193         if [ "$RET" = "true" ]; then
194         
195             cp_echo "CN: Checking and enabling some specific parameters in file $ini_file"
196         
197             phplimit="$(phpmemlimit)M"
198         
199             if [ -f "$ini_file" ]; then
200
201                 cp_check_and_sed "^[[:space:]]*upload_max_filesize[[:space:]]*=" \
202                         's/^[[:space:]]*upload_max_filesize[[:space:]]*=.*/upload_max_filesize = 256M/' \
203                         "$ini_file" && need_restart=1 || true
204
205                 cp_check_and_sed "^[[:space:]]*post_max_size[[:space:]]*=" \
206                         "s/^[[:space:]]*post_max_size[[:space:]]*=.*/post_max_size = ${phplimit}/" \
207                         "$ini_file" && need_restart=1 || true
208
209                 cp_check_and_sed "^[[:space:]]*memory_limit[[:space:]]*=" \
210                         "s/^[[:space:]]*memory_limit[[:space:]]*=.*/memory_limit = ${phplimit}/" \
211                         "$ini_file" && need_restart=1 || true
212             fi
213
214             ini_file_tmp=`mktemp $ini_file.tmp.XXXXXX`
215             if [ -f "$ini_file" ]; then
216                 cat $ini_file > $ini_file_tmp
217             fi
218
219             if ! egrep -q "^[[:space:]]*upload_max_filesize[[:space:]]*=" $ini_file_tmp; then
220                 echo "upload_max_filesize = 256M" >> "$ini_file_tmp"
221                 need_restart=1
222             fi
223             if ! egrep -q "^[[:space:]]*post_max_size[[:space:]]*=" $ini_file_tmp; then
224                 echo "post_max_size = ${phplimit}" >> "$ini_file_tmp"
225                 need_restart=1
226             fi
227             if ! egrep -q "^[[:space:]]*memory_limit[[:space:]]*=" $ini_file_tmp; then
228                 echo "memory_limit = ${phplimit}" >> "$ini_file_tmp"
229                 need_restart=1
230             fi
231             cp_mv "$ini_file_tmp" "$ini_file"
232             chmod 644 "$ini_file"
233
234             if [ -e "$ini_file_tmp" ]; then
235                 rm -f "$ini_file_tmp"
236             fi
237         fi
238
239         # Validate php.ini values.
240         if [ -f "$ini_file" ]; then
241                 php4_inivalues="$(phpinivalidate "$ini_file")"
242                 if [ -n "$php4_inivalues" ]; then
243                         db_fset php4-cn/inivalues seen false
244                         db_title php4-cn - konfiguracija za $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
245                         db_subst php4-cn/inivalues php4_sapi $(echo ${SAPI} | sed 's/a/A/;s/cli/CLI/')
246                         db_subst php4-cn/inivalues ini_file "$ini_file"
247                         db_capb escape
248                         db_subst php4-cn/inivalues php4_inivalues "$(echo -n "$php4_inivalues" | debconf-escape -e)"
249                         db_input critical php4-cn/inivalues || true
250                         db_go || true
251                 fi
252         fi
253 done
254
255
256 db_stop || true
257
258
259 # Restart Apache2 web server if needed.
260 #
261 if [ $need_restart -eq 1 ]; then
262         
263         # Check Apache2 web server configuration.
264         if /usr/sbin/apache2ctl configtest 2>/dev/null; then
265
266                 # Restart Apache2 web server.
267                 if [ -x "/etc/init.d/apache2" ]; then
268                     if [ -x "`which invoke-rc.d 2>/dev/null`" ]; then
269                         invoke-rc.d apache2 force-reload || true
270                     else
271                         /etc/init.d/apache2 force-reload || true
272                     fi
273                 fi
274         else
275         
276                 # Something is broken.
277                 cp_echo "CN: Your Apache2 configuration is broken."
278                 cp_echo "CN: Please, check the service after the installation finishes!"
279         fi
280 fi
281
282
283 # Mail root
284 #
285 cp_mail "$PKG"
286
287 exit 0