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