#set -e

#variables
SRC_DIR=/data/src
SVN_DIR=/data/svn
PHP_VER=php-5.6.39
EISFAIR_VER=1
export CFLAGS='-O2 -march=i486'
export CXXFLAGS="${CFLAGS} -std=c++11"

if [ "`cat /etc/eisfair-system`" = "eisfair-2" ]
then
    EISFAIR_VER=2
fi

# goto src directory
cd $SRC_DIR

#--------------
# php
#--------------
#wget 
#tar xjvf php-5.4.34.tar.bz2

cd $PHP_VER

# download patches
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/004-ldap_fix.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/017-pread_pwrite_disable.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/113-php.ini_securitynotes.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/034-apache2_umask_fix.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/036-fd_setsize_fix.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/043-recode_size_t.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/libdb_is_-ldb
#wget http://download.suhosin.org/suhosin-patch-5.3.9-0.9.10.patch.gz
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/zend_int_overflow.patch

#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/fix_broken_upstream_tests.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/fix_broken_5.3_tests.patch
#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/fix_broken_sha2_test.patch

#wget http://patch-tracker.debian.org/patch/series/dl/php5/5.4.15-1/gdIOCtx.patch

#gunzip suhosin-patch-5.3.9-0.9.10.patch.gz
# apply patches
#patch -p 1 -i 004-ldap_fix.patch
#patch -p 1 -i 017-pread_pwrite_disable.patch  
#patch -p 1 -i 113-php.ini_securitynotes.patch    
#patch -p 1 -i 034-apache2_umask_fix.patch
#patch -p 1 -i 036-fd_setsize_fix.patch
#patch -p 1 -i 043-recode_size_t.patch
#patch -p 1 -i libdb_is_-ldb
##patch -p 1 -i suhosin-patch-5.3.9-0.9.10.patch
#patch -p 1 -i zend_int_overflow.patch

#patch -p 1 -i fix_broken_upstream_tests.patch
#patch -p 1 -i fix_broken_5.3_tests.patch
#patch -p 1 -i fix_broken_sha2_test.patch
#patch -p 1 -i fix_gd_210.patch
#patch -p 1 -i libdb.patch

#patch -p 1 -i gdIOCtx.patch

# rebuild configure script
mv configure configure.orig
./buildconf --force 

#sed für maintainer mode = off

# configure
./configure  --prefix=/usr \
--build=i486-pc-linux-gnu  \
--host=i486-pc-linux-gnu \
--exec-prefix=/usr \
--with-apxs2=/usr/bin/apxs \
--with-config-file-path=/etc/php5/apache2 \
--program-prefix= \
--bindir=/usr/bin \
--sysconfdir=/etc/php5 \
--libdir=/usr/lib/php5 \
--datadir=/usr/lib/php5 \
--includedir=/usr/include/php5 \
--with-pear=/usr/share/php5 \
--disable-debug \
--enable-bcmath \
--enable-calendar \
--enable-dba \
--enable-exif \
--enable-ftp \
--enable-gd-native-ttf \
--enable-gd-jis-conv \
--enable-inline-optimization \
--enable-ipv6 \
--enable-mbstring \
--enable-mbregex \
--enable-session \
--enable-shmop \
--enable-sigchild \
--enable-sockets \
--enable-sysvmsg \
--enable-sysvsem \
--enable-sysvshm \
--enable-wddx \
--enable-zip \
--with-bz2=shared,/usr \
--with-curl=shared,/usr \
--with-db4=shared,/usr \
--with-gettext=shared,/usr \
--with-gd=shared,/usr \
--with-freetype-dir \
--with-jpeg-dir \
--with-layout=GNU \
--with-ldap=shared,/usr \
--with-ldap-sasl \
--with-libmbfl \
--with-mcrypt=shared,/usr \
--with-mssql=shared,/usr \
--with-pdo-dblib=shared,/usr \
--with-mysql=shared,/usr \
--with-pdo-mysql=shared,/usr \
--with-pgsql=shared,/usr \
--with-pdo-pgsql=shared,/usr \
--with-interbase=shared,/usr \
--with-openssl=shared,/usr \
--with-png-dir \
--with-t1lib=shared,/usr \
--with-zlib=shared,/usr \
--with-imap=/usr \
--with-imap-ssl=/usr \
--with-config-file-scan-dir=/etc/php5/conf.d \
--with-config-file-path=/etc/php5/apache2 \
--enable-soap=shared \
--enable-sigchild \
--enable-pdo=shared \
--with-sqlite3=shared,/usr \
--with-pdo-sqlite=shared,/usr \
--enable-libxml \
--with-iconv=shared \
--with-mysqli=mysqlnd \
--with-pdo-mysql=shared,mysqlnd \
--with-xsl \
--with-xpm-dir=/usr \
--with-vpx-dir=/usr \
--enable-fpm \
--with-fpm-user=wwwrun \
--with-fpm-group=nogroup \
--enable-intl=shared 

# compile php5
make
make install

#
strip -R .note -R .comment /usr/lib/php5/20131226/*
strip -R .note -R .comment /usr/lib/apache2/modules/libphp5.so

#--------------
# apcu
#--------------
cd $SRC_DIR
#wget http://pecl.php.net/get/apcu-4.0.11.tgz
#tar xzvf apcu-4.0.11.tgz
cd apcu-4.0.11
make distclean
phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu

make
make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# pdf
#--------------
cd $SRC_DIR
#wget https://pecl.php.net/get/pdflib-4.1.2.tgz
#tar xzvf pdflib-4.1.2.tgz
# see https://bugs.php.net/bug.php?id=60385 for hotfix
cd pdflib-4.1.2
make distclean
phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu
           
make
make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# redis
#--------------
cd $SRC_DIR
#wget https://pecl.php.net/get/redis-3.1.4.tgz
#tar xzvf redis-3.1.4.tgz
cd redis-3.1.4/
make distclean
phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu
           
make
make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# php-xz
#--------------
cd $SRC_DIR
cd $PHP_VER
cd ext
git clone https://github.com/payden/php-xz xz
cd xz && phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu
make && make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# pcntl
#--------------
cd $SRC_DIR
cd $PHP_VER
cd ext
cd pcntl
phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu
make && make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# intl
#--------------
cd $SRC_DIR
cd $PHP_VER
cd ext
cd intl
phpize
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu
make && make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# libsmbclient
#--------------
cd $SRC_DIR
cd $PHP_VER
cd ext
git clone git://github.com/eduardok/libsmbclient-php.git
cd libsmbclient-php && phpize && 
./configure --build=i486-pc-linux-gnu --host=i486-pc-linux-gnu --includedir=/usr/include/samba-4.0/ 
make && make install
strip -R .note -R .comment /usr/lib/php5/20131226/*

#--------------
# copy to svn
#--------------
cd $SVN_DIR/trunk/web/apache2_php5/eisfair-${EISFAIR_VER}-bin
cp /usr/lib/php5/20131226/* usr/lib/php5/extensions/
cp /usr/lib/apache2/modules/libphp5.so usr/lib/apache2/modules/libphp5.so