#!/bin/bash
# this script is for developers only
set -e
MAKE=/usr/bin/make

. devel/run.cfg
if [[ ! -z $1 ]]; then
	type=$1
fi

case "$type" in
prep*)
	shift
	exec $MAKE -f devel/prepare.mak "$@"
	;;
tags)
	rm -f tags
	shift
	exec $MAKE -f devel/prepare.mak tags "$@"
	;;
dep*)
	pwd=$(readlink -f "$(pwd)")
	builddir=$(readlink -f ../trunk-php5-debug)
	if [[ -z $builddir ]]; then
		echo required ../trunk-php5-debug not found
		return 1
	fi
	find . -iname \*.c | sort | while read -r sourceFile; do
		sourceFile=${sourceFile#./}
		case "$sourceFile" in
		includes.c)
			echo -n "\$(XCACHE_INCLUDES_I) "
			;;
		esac
		echo -n "\$(builddir)/${sourceFile%.c}.lo:"
		for dependency in $(gcc -M $sourceFile -I$HOME/test/php5-debug/include/php/{,main,Zend,TSRM} -I. -I${builddir} -MG | sed 's#.*:##g' | sed 's#\\##g'); do
			dependency=$(readlink -f "$dependency")
			case "$dependency" in
			$pwd/*)
				dependency=${dependency#$pwd/}
				;;
			$builddir/*)
				dependency="\$(builddir)/"${dependency#$builddir/}
				;;
			esac

			case "$dependency" in
			/*) ;;
			$sourceFile) ;;
			\$\(builddir\)/xc_processor.h)
				echo -n " \$(XCACHE_PROC_H)"
				;;
			\$\(builddir\)/xc_processor.c.h)
				echo -n " \$(XCACHE_PROC_C)"
				;;
			*)
				if [[ -r $dependency ]]; then
					echo -n " \$(srcdir)/$dependency"
				else
					echo "$dependency not found" >&2
				fi
			esac
		done
		echo
	done > Makefile.frag.deps
	exit
	;;
xtest)
	exec $MAKE -f devel/test.mak
	;;
esac

basename=$(basename $(pwd))
if echo $basename | grep -- - >/dev/null; then :; else
	svn propget svn:ignore . > .svnignore

	dirs=${dirs:-php5-debug}
	for dir in $dirs; do
		mkdir -p ../${basename}-${dir}
		cd ../${basename}-${dir} || exit
		lndir ../${basename} > /dev/null

		pwd
		$0 "$@"
	done
	exit
fi

phpbasename=${basename#*-}
xcachebasename=${basename%%-*}
xcachesrcdir=../$xcachebasename

if [[ $# -gt 0 ]]; then
	type=$1
	shift
	if [[ $# -gt 0 ]]; then
		args=("$@")
	fi
fi

if [[ -z ${args[0]} ]]; then
	args=(devel.php)
fi

case "$type" in
phpize)
	if [[ -r Makefile ]]; then
		$MAKE xcachesvnclean || true
	fi
	;;
esac

rm -f php-src
find -L . -type l | xargs rm -f
lndir "$xcachesrcdir" >/dev/null
find . -iname .\*.swp | xargs rm -f
ln -sf ~/src/php/$phpbasename php-src
for i in ~/src/php/$phpbasename/sapi/cgi/php{,-cgi}; do
	if [[ -r $i ]]; then
		ln -sf "$i" php-cgi
	fi
done
ln -sf ~/src/php/$phpbasename/sapi/cli/php php-cli
ln -sf ~/src/php/$phpbasename/sapi/fpm/php-fpm php-fpm

case "$type" in
phpize)
	PHPDIRS=${PHPDIRS:-$HOME/test}
	if [[ ! -x $PHPDIRS/$phpbasename/bin/phpize ]]; then
		echo $PHPDIRS/$phpbasename/bin/phpize not found
		exit
	fi
	export PATH=$PHPDIRS/$phpbasename/bin:$PATH
	phpize --clean \
	&& phpize \
	&& CFLAGS="-g -O0 -Wall -Wno-unused -W -Wshadow -Werror=implicit-function-declaration -std=c89 -D_GNU_SOURCE -D_POSIX_SOURCE -Dinline=" ./configure \
		--enable-xcache-cacher \
		--enable-xcache-optimizer \
		--enable-xcache-encoder \
		--enable-xcache-decoder \
		--enable-xcache-disassembler \
		--enable-xcache-coverager \
		--enable-xcache-test \
		--enable-xcache-constant
	exit
	;;
make)
	MAKEARGS=("$@")
	;;
esac

LANG=C $MAKE $MAKEOPTS "${MAKEARGS[@]}" 2>&1 \
| sed -ur \
	-e 's#Werror=implicit-function-declaration#We/rror=i/mplicit-function-declaration#' \
	-e 's#\./xc_processor\.h#'$PWD'/xc_processor.h#' \
	-e 's#\./xc_processor\.c\.h#'$PWD'/xc_processor.c.h#' \
| hi error implicit warn FAIL
ret=${PIPESTATUS[0]}
if [[ $ret -ne 0 || $type = make ]]; then
	exit $ret
fi

cmd=()
tracer=()
case "$basename" in
*-apache1*)
	cmd=($HOME/apache1/bin/httpd -X)
	;;
*-apache*)
	exit 1
	;;
*)
	case "${args[0]}" in
	fcgi)
		cmd=(./php-cgi -q -c devel.ini)
		args=(-b 1026)
		;;
	fpm)
		cmd=(./php-fpm -c devel.ini)
		args=(-y devel.fpm)
		;;
	*)
		cmd=(./php-cgi -q -c devel.ini)
		;;
	esac

	"${cmd[@]}" -v || true
esac

case "$type" in
ltr*)
	export USE_ZEND_ALLOC=0
	tracer=(ltrace -s1024 -e malloc,realloc,free,write)
	;;
str*)
	tracer=(strace -s1024 -T)
	;;
gdb)
	#USE_ZEND_ALLOC=0
	tracer=(gdb --args)
	;;
val*)
	export USE_ZEND_ALLOC=0
	tracer=(valgrind --gen-suppressions=all)
	;;
dc)
	exec ./php-cli -c devel.ini ./bin/phpdc.phpr "${args[@]}" | tee decompiled.php
	;;
dop)
	exec ./php-cli -c devel.ini ./bin/phpdop.phpr "${args[@]}"
	;;
retest)
	exec $MAKE xcachetest "$@" TESTS="`grep '^/.*\.phpt$' php_test_results_*.txt | uniq | xargs`"
	;;
test)
	case "${args[0]}" in
	*.phpt)
		exec $MAKE xcachetest TEST_ARGS=-v TESTS="${args[*]}"
		;;
	*/)
		exec $MAKE xcachetest TESTS="${args[@]}"
		;;
	*)
		exec $MAKE xcachetest
		;;
	esac
	;;
esac

export XCACHE_SKIP_FCGI_WARNING=1
echo "${tracer[@]}" "${cmd[@]}" "${args[@]}"
exec "${tracer[@]}" "${cmd[@]}" "${args[@]}"
