#!/bin/sh
#

EXT=php7
TCZ=/tmp/$EXT/TCZ
PGVER=96

sudo rm -rf /tmp/$EXT

DEPS="apache2.4 apache2.4-dev apr apr-dev apr-util apr-util-dev
 openldap openldap-dev libxml2 libxml2-dev recode recode-dev
 net-snmp net-snmp-dev libgd libgd-dev curl curl-dev
 enchant-dev libwebp-nox libwebp-nox-dev
 libvpx libvpx-dev gmp gmp-dev icu-dev aspell-dev
 cyrus-sasl cyrus-sasl-dev libxslt libxslt-dev
 fontconfig-dev libltdl libtool-dev libtidy libtidy-dev
 postgresql-9.6 postgresql-9.6-dev perl5 unixODBC unixODBC-dev
 tzdata"

test "$KBITS" = "64" && DEPS="$DEPS mariadb-10.1-dev" || DEPS="$DEPS mariadb-dev"

NOTFOUND=""
for a in compiletc bash file squashfs-tools $DEPS
         do tce-load -i $a || tce-load -iwl $a || NOTFOUND=x
done
test -z "$NOTFOUND" || exit

# built-in extensions are configured in main/internal_functions.c
#   date, libxml, pcre, filter, reflection, spl, standard

# configure will fail if Apache httpd throws an error when trying to start
# make sure that /usr/local/etc/httpc/httpd.conf is correct

# apply fixes:

# busybox expr lack of support for -- meaning no more options
sed -i 's/expr -- /expr /g' configure

# phpdbg libraries are not added and passed to the linker
grep -q 'PHPDBG_EXTRA_LIBS -lreadline -lncurses -ltinfo' configure || \
sed -i '\#BUILD_BINARY="sapi/phpdbg/phpdbg"#i \
 PHPDBG_EXTRA_LIBS="$PHPDBG_EXTRA_LIBS -lreadline -lncurses -ltinfo"\
' configure

# PHP will require X11 libraries even if you tell it --with-xpm-dir=no
# break xpm detection so it isn't required
sed -i 's/gdImageCreateFromXpm/gdImageCreateFromXXXpm/g' configure

export CC="gcc -mtune=generic -Os -pipe"
export CXX="g++ -mtune=generic -Os -pipe -fno-exceptions"
echo $PATH | grep -q mysql || export PATH=$PATH:/usr/local/mysql/bin:/usr/local/pgsql$PGVER/bin:/usr/local/oracle

# Oracle and LDAP have name conflicts in their header files
# Compile with LDAP first, then make a second pass adding OCI
# don't bother compiling executables on first pass

./configure \
	--prefix=/usr/local \
	--sysconfdir=/usr/local/etc \
	--localstatedir=/var \
	--datadir=/usr/local/share \
	--mandir=/usr/local/share/man \
	--with-config-file-path=/usr/local/etc/php7/ \
	--with-config-file-scan-dir=/usr/local/etc/php7/extensions/ \
	--enable-shared \
	--with-apxs2=/usr/local/bin/apxs \
	--enable-dmalloc=shared \
	--enable-libgcc \
	--disable-cgi \
	--disable-cli \
	--disable-phpdbg \
	--disable-fpm \
	--without-pear \
	--with-system-ciphers \
	--enable-pdo=shared \
	--disable-embed \
	--disable-inline-optimization \
	--disable-mysqlnd \
	--disable-rpath \
	--disable-static \
	--enable-bcmath=shared \
	--with-bz2=shared,/usr/local \
	--enable-calendar=shared \
	--enable-ctype=shared \
	--with-curl=shared,/usr/local \
	--enable-dba=shared \
	--enable-dom=shared \
	--with-enchant=shared,/usr/local \
	--enable-exif=shared \
	--enable-fileinfo=shared \
	--enable-filter \
	--with-freetype-dir=/usr/local \
	--enable-ftp=shared \
	--with-gd=shared,/usr/local \
	--enable-gd-jis-conv \
	--enable-gd-native-ttf \
	--with-gdbm=shared,/usr/local \
	--with-gettext=shared,/usr/local \
	--with-gmp=shared,/usr/local \
	--enable-hash=shared \
	--with-iconv=shared \
	--with-icu-dir=/usr/local \
	--enable-intl=shared \
	--with-jpeg-dir=/usr/local \
	--enable-json=shared \
	--with-ldap-sasl=/usr/local \
	--with-ldap=shared,/usr/local \
	--with-libxml-dir=/usr/local \
	--enable-libxml \
	--enable-mbstring=shared \
	--with-mhash=shared,/usr/local \
	--with-mysqli=shared,/usr/local/mysql/bin/mysql_config \
	--with-pdo-mysql=shared,/usr/local/mysql \
	--enable-opcache=shared \
	--disable-opcache-file \
	--with-openssl-dir=/usr/local \
	--with-openssl=shared,/usr/local \
	--enable-pcntl=shared \
	--with-pcre-dir=/usr/local \
	--with-pcre-regex=/usr/local \
	--with-unixODBC=shared,/usr/local \
	--with-pdo-odbc=shared,unixODBC \
	--with-pgsql=shared,/usr/local/pgsql$PGVER \
	--with-pdo-pgsql=shared,/usr/local/pgsql$PGVER \
	--enable-phar=shared \
	--enable-phpdbg-webhelper=shared \
	--with-png-dir=/usr/local \
	--enable-posix=shared \
	--with-pspell=shared,/usr/local \
	--with-readline=shared,/usr/local \
	--with-recode=shared,/usr/local \
	--enable-session=shared \
	--enable-shmop=shared \
	--enable-simplexml=shared \
	--with-snmp=shared,/usr/local \
	--enable-soap=shared \
	--enable-sockets=shared \
	--with-sqlite3=shared,/usr/local \
	--with-pdo-sqlite=shared,/usr/local \
	--enable-sysvmsg=shared \
	--enable-sysvsem=shared \
	--enable-sysvshm=shared \
	--with-tidy=shared,/usr/local \
	--enable-tokenizer=shared \
	--with-tsrm-pthreads \
	--enable-wddx=shared \
	--with-webp-dir=/usr/local \
	--enable-xml=shared \
	--enable-xmlreader=shared \
	--with-xmlrpc=shared \
	--enable-xmlwriter=shared \
	--with-xsl=shared,/usr/local \
	--enable-zend-signals \
	--enable-zip=shared \
	--with-zlib-dir=/usr \
	--with-zlib=shared,/usr \
	--with-xpm-dir=no \
	|| exit

make || exit

./configure \
	--prefix=/usr/local \
	--sysconfdir=/usr/local/etc \
	--localstatedir=/var \
	--datadir=/usr/local/share \
	--mandir=/usr/local/share/man \
	--with-config-file-path=/usr/local/etc/php7/ \
	--with-config-file-scan-dir=/usr/local/etc/php7/extensions/ \
	--enable-shared \
	--with-apxs2=/usr/local/bin/apxs \
	--enable-dmalloc=shared \
	--enable-libgcc \
	--enable-cgi \
	--enable-cli \
	--enable-phpdbg \
	--enable-fpm \
	--with-pear=shared,/usr/local/lib/php/pear \
	--with-system-ciphers \
	--enable-pdo=shared \
	--disable-embed \
	--disable-inline-optimization \
	--disable-mysqlnd \
	--disable-rpath \
	--disable-static \
	--enable-bcmath=shared \
	--with-bz2=shared,/usr/local \
	--enable-calendar=shared \
	--enable-ctype=shared \
	--with-curl=shared,/usr/local \
	--enable-dba=shared \
	--enable-dom=shared \
	--with-enchant=shared,/usr/local \
	--enable-exif=shared \
	--enable-fileinfo=shared \
	--enable-filter \
	--with-freetype-dir=/usr/local \
	--enable-ftp=shared \
	--with-gd=shared,/usr/local \
	--enable-gd-jis-conv \
	--enable-gd-native-ttf \
	--with-gdbm=shared,/usr/local \
	--with-gettext=shared,/usr/local \
	--with-gmp=shared,/usr/local \
	--enable-hash=shared \
	--with-iconv=shared \
	--with-icu-dir=/usr/local \
	--enable-intl=shared \
	--with-jpeg-dir=/usr/local \
	--enable-json=shared \
	--with-ldap-sasl=/usr/local \
	--with-ldap=shared,/usr/local \
	--with-libxml-dir=/usr/local \
	--enable-libxml \
	--enable-mbstring=shared \
	--with-mhash=shared,/usr/local \
	--with-mysqli=shared,/usr/local/mysql/bin/mysql_config \
	--with-pdo-mysql=shared,/usr/local/mysql \
	--enable-opcache=shared \
	--disable-opcache-file \
	--with-openssl-dir=/usr/local \
	--with-openssl=shared,/usr/local \
	--enable-pcntl=shared \
	--with-pcre-dir=/usr/local \
	--with-pcre-regex=/usr/local \
	--with-oci8=shared,instantclient,/usr/local/oracle \
	--with-pdo-oci=shared,/usr/local/oracle \
	--with-unixODBC=shared,/usr/local \
	--with-pdo-odbc=shared,unixODBC \
	--with-pgsql=shared,/usr/local/pgsql$PGVER \
	--with-pdo-pgsql=shared,/usr/local/pgsql$PGVER \
	--enable-phar=shared \
	--enable-phpdbg-webhelper=shared \
	--with-png-dir=/usr/local \
	--enable-posix=shared \
	--with-pspell=shared,/usr/local \
	--with-readline=shared,/usr/local \
	--with-recode=shared,/usr/local \
	--enable-session=shared \
	--enable-shmop=shared \
	--enable-simplexml=shared \
	--with-snmp=shared,/usr/local \
	--enable-soap=shared \
	--enable-sockets=shared \
	--with-sqlite3=shared,/usr/local \
	--with-pdo-sqlite=shared,/usr/local \
	--enable-sysvmsg=shared \
	--enable-sysvsem=shared \
	--enable-sysvshm=shared \
	--with-tidy=shared,/usr/local \
	--enable-tokenizer=shared \
	--with-tsrm-pthreads \
	--enable-wddx=shared \
	--with-webp-dir=/usr/local \
	--enable-xml=shared \
	--enable-xmlreader=shared \
	--with-xmlrpc=shared \
	--enable-xmlwriter=shared \
	--with-xsl=shared,/usr/local \
	--enable-zend-signals \
	--enable-zip=shared \
	--with-zlib-dir=/usr \
	--with-zlib=shared,/usr \
	--with-xpm-dir=no \
	|| exit

make || exit

# make install will complain and die if a copy of the current httpd.conf
# file isn't in the install tree in the right place
mkdir -p $TCZ-dev/usr/local/etc/httpd
cp /mnt/sda1/lamp/contrib/httpd.conf $TCZ-dev/usr/local/etc/httpd

# pear requires xml to install so make sure module gets loaded
sed -i '/^PEAR_INSTALL_FLAGS/s#$# -d extension_dir=$(top_builddir)/modules/ -d extension=xml.so#' Makefile

make install INSTALL_ROOT=$TCZ-dev

for a in $(find $TCZ-dev -type f); do file -b $a | grep -q '^ELF .*not stripped$' && strip --strip-unneeded $a; done
rm -rf $TCZ-dev/var
rm -rf $TCZ-dev/.[a-z]*
rm $TCZ-dev/usr/local/etc/httpd/httpd.conf.bak

mkdir -p $TCZ-cli/usr/local/bin
mv $TCZ-dev/usr/local/bin/php $TCZ-cli/usr/local/bin

mkdir -p $TCZ-cgi/usr/local/bin
mv $TCZ-dev/usr/local/bin/php-cgi $TCZ-cgi/usr/local/bin

mkdir -p $TCZ-fpm/usr/local/etc/httpd/original/conf.d
cp /mnt/sda1/lamp/contrib/httpd-php7-fpm.conf $TCZ-fpm/usr/local/etc/httpd/original/conf.d
mv $TCZ-dev/usr/local/etc/php-fpm* $TCZ-fpm/usr/local/etc
mv $TCZ-dev/usr/local/sbin $TCZ-fpm/usr/local
mkdir -p $TCZ-fpm/usr/local/share
mv $TCZ-dev/usr/local/share/fpm $TCZ-fpm/usr/local/share
mkdir -p $TCZ-fpm/usr/local/etc/init.d
cat >$TCZ-fpm/usr/local/etc/init.d/php-fpm <<'EOF'
#! /bin/sh

php_fpm_BIN=$(which php-fpm)
php_fpm_CONF=/usr/local/etc/php-fpm.conf
php_fpm_PID=/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


wait_for_pid () {
	try=0

	while test $try -lt 35 ; do

		case "$1" in
			'created')
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;

			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN --daemonize $php_opts

		if [ "$?" != 0 ] ; then
			echo " failed"
			exit 1
		fi

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if [ ! -r $php_fpm_PID ] ; then
			echo "php-fpm is stopped"
			exit 0
		fi

		PID=`cat $php_fpm_PID`
		if ps -p $PID | grep -q $PID; then
			echo "php-fpm (pid $PID) is running..."
		else
			echo "php-fpm dead but pid file exists"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	*)
		echo "Usage: $0 {start|stop|force-quit|restart|reload|status}"
		exit 1
	;;

esac
EOF
chmod 775 $TCZ-fpm/usr/local/etc/init.d/php-fpm

mkdir -p $TCZ-mod/usr/local/etc/httpd/original/conf.d
mv $TCZ-dev/usr/local/etc/httpd/httpd.conf $TCZ-mod/usr/local/etc/httpd/original
cp /mnt/sda1/lamp/contrib/httpd-php7-mod.conf $TCZ-mod/usr/local/etc/httpd/original/conf.d
mv $TCZ-dev/usr/local/apache2 $TCZ-mod/usr/local
mv $TCZ-mod/usr/local/apache2/modules/libphp7.so $TCZ-mod/usr/local/apache2/modules/mod_php7.so

mkdir -p $TCZ-ext/usr/local/lib/php
mv $TCZ-dev/usr/local/lib/php/extensions $TCZ-ext/usr/local/lib/php
mv $TCZ-dev/usr/local/etc $TCZ-ext/usr/local
mkdir -p $TCZ-ext/usr/local/etc/php7/extensions
cp /mnt/sda1/lamp/contrib/php.ini-sample $TCZ-ext/usr/local/etc/php7

mkdir -p $TCZ-dev/usr/local/etc
mv $TCZ-ext/usr/local/etc/pear.conf $TCZ-dev/usr/local/etc/pear.conf-sample
rm -rf $TCZ-dev/usr/local/etc/httpd

sudo chown -R root.root $TCZ*

mksquashfs $TCZ-cli /tmp/$EXT/$EXT-cli.tcz -noappend
mksquashfs $TCZ-cgi /tmp/$EXT/$EXT-cgi.tcz -noappend
mksquashfs $TCZ-fpm /tmp/$EXT/$EXT-fpm.tcz -noappend
mksquashfs $TCZ-mod /tmp/$EXT/$EXT-mod.tcz -noappend
mksquashfs $TCZ-ext /tmp/$EXT/$EXT-ext.tcz -noappend
mksquashfs $TCZ-dev /tmp/$EXT/$EXT-dev.tcz -noappend

