#!/bin/sh
#
ME=$(readlink -f "$0")
MEDIR=${ME%/*}
test "$(uname -m)" = "x86_64" && export KBITS=64 || export KBITS=32
BASE=${BASE:-/mnt/sda1/lamp}

EXT=php7
PGVER=96

. $MEDIR/phase-default-init.sh

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 libnet libnet-dev
 libvpx libvpx-dev gmp gmp-dev icu-dev aspell-dev
 cyrus-sasl cyrus-sasl-dev libxslt libxslt-dev
 libzip libzip-dev libsodium libsodium-dev
 fontconfig-dev libltdl libtool-dev libtidy libtidy-dev
 postgresql-9.6 postgresql-9.6-dev oracle-12.2-client
 perl5 unixODBC unixODBC-dev tzdata"

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

. $MEDIR/phase-default-deps.sh
. $MEDIR/phase-cc-opts-no-flto-excp.sh

echo $PATH | grep -q pgsql || export PATH=$PATH:/usr/local/mysql/bin:/usr/local/pgsql$PGVER/bin:/usr/local/oracle

# 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
sed -i -e 's/"XBM Support", "enabled"/"XBM Support", "disabled"/' \
	-e 's/"XBM Support", 1/"XBM Support", 0/' ext/gd/gd.c

# 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

CONFCMD=$(cat $MEDIR/phase-php-config-head.cmd)"
	--disable-cgi
	--disable-cli
	--disable-fpm
	--disable-phpdbg
	--disable-phar             
	--without-pear
	--with-pgsql=shared,/usr/local/pgsql$PGVER
	--with-pdo-pgsql=shared,/usr/local/pgsql$PGVER
"
$CONFCMD || exit

. $MEDIR/phase-default-make.sh

CONFCMD=$(cat $MEDIR/phase-php-config-head.cmd)"
	--enable-cgi
	--enable-cli
	--enable-fpm
	--enable-phpdbg
	--enable-phpdbg-webhelper=shared 
	--enable-phar=shared             
	--with-pear=shared,/usr/local/lib/php/pear
	--with-pgsql=shared,/usr/local/pgsql$PGVER
	--with-pdo-pgsql=shared,/usr/local/pgsql$PGVER
	--with-oci8=shared,instantclient,/usr/local/oracle/lib
	--with-pdo-oci=shared,/usr/local/oracle
"
$CONFCMD || exit

. $MEDIR/phase-default-make.sh

# 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 $BASE/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

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 $BASE/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 $BASE/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 $BASE/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

. $MEDIR/phase-default-strip.sh
. $MEDIR/phase-default-set-perms.sh
. $MEDIR/phase-default-squash-tcz.sh
