#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin;

mkdir -p /usr/local/php-5.2/download_tar;
cd /usr/local/php-5.2/download_tar;

# install autoconf-2.13
wget http://code.amysql.com/files/autoconf-2.13.tar.gz;
tar zxvf autoconf-2.13.tar.gz;
cd autoconf-2.13/;
./configure --prefix=/usr/local/autoconf-2.13;
make && make install;
export PHP_AUTOCONF=/usr/local/autoconf-2.13/bin/autoconf;
export PHP_AUTOHEADER=/usr/local/autoconf-2.13/bin/autoheader;
if [ -f /usr/lib64/libjpeg.so ]; then
	cp -frp /usr/lib64/libpng* /usr/lib/;
	cp -frp /usr/lib64/libldap* /usr/lib;
fi;

# install php-5.2.17
cd /usr/local/php-5.2/download_tar;
wget http://code.amysql.com/files/php-5.2.17.tar.gz;
wget http://code.amysql.com/files/php-5.2.17-fpm-0.5.14.diff.gz;
tar -zxvf php-5.2.17.tar.gz;
gzip -cd php-5.2.17-fpm-0.5.14.diff.gz | patch -d php-5.2.17 -p1;
cd php-5.2.17;
wget http://code.amysql.com/files/php-5.2.17-max-input-vars.patch;
patch -p1 < php-5.2.17-max-input-vars.patch;
./buildconf --force;
./configure --prefix=/usr/local/php-5.2 --enable-fastcgi --enable-fpm --with-config-file-path=/usr/local/php-5.2/etc  --with-openssl --with-zlib --with-curl --enable-ftp --with-gd --with-jpeg-dir --with-png-dir --with-freetype-dir --enable-gd-native-ttf --enable-mbstring --enable-zip --with-iconv=/usr/local/libiconv --with-mysql=/usr/local/mysql;
make && make install;
cp php.ini-dist /usr/local/php-5.2/etc/php.ini;


cd /usr/local/php-5.2/download_tar;
wget http://code.amysql.com/files/php-5.2.17-conf.tar.gz;
tar -zxvf php-5.2.17-conf.tar.gz;
mkdir -p /usr/local/php-5.2/logs;
\cp conf/php-fpm.conf /usr/local/php-5.2/etc;
\cp conf/php52 /usr/local/php-5.2/php52;
chmod 775 /usr/local/php-5.2/php52;

if /usr/local/php-5.2/bin/php-cgi --fpm --fpm-config /usr/local/php-5.2/etc/php-fpm.conf; then
	echo "/usr/local/php-5.2/bin/php-cgi --fpm --fpm-config /usr/local/php-5.2/etc/php-fpm.conf" >>/etc/init.d/amh-start;
	echo "[OK] php-5.2.17 install completed.";
else
	echo "[Error] php-5.2.17 install failed.";
fi;