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