#!/bin/sh
# vim:syntax=sh:sw=2:ts=2
. scripts/common

exec 3>Makefile.tmp
exec 4>all.tmp
exec 5>clean.tmp

rule() {
	local target="$1"
	local source="$2"
	shift 2
	echo "$target: $source" >&3
	local command
	for command in "$@"; do
		echo -ne "\t" >&3
		echo "$command" >&3
	done
	echo >&3
}

addAllRule() {
	echo -n " $@" >&4
}

addCleanRule() {
	echo -n " $@" >&5
}

rule all 'Makefile unpackphp alltargets'
begin "Generating rule for download"; {
	rule "$sourceDir/.stamp-download download" '' \
		"mkdir -p $sourceDir/" \
		"$scriptsDir/download \$(PHP_VERSION)" \
		"touch $sourceDir/.stamp-download"
	rule "Makefile" ".config $sourceDir/.stamp-download $(echo "$scriptsDir"/{common,utils,autobuild,generateMakefile})" \
		"$scriptsDir/generateMakefile"
}; end

rule "$develDir/.stamp-unpack unpackphp" "$(printf " $develDir/php-%s/.stamp-unpack" "${phpVersions[@]}")"

rulePhpVersion() {
	begin "Generating rule for devel/$phpVersion"; {
		cleararray binaryPackageStamp
		local binaryPackage
		for binaryPackage in "$sourceDir/php-$phpVersion"*Win32*.zip; do
			if [[ -s $binaryPackage ]]; then
				local binaryPackageDir="$develDir/$(basename "$binaryPackage" .zip)"
				local stampFile="$binaryPackageDir/.stamp-unpack"
				rule "$stampFile" "$binaryPackage" \
					"mkdir -p $binaryPackageDir" \
					"$scriptsDir/unpackphpbinary $phpVersion $binaryPackage $binaryPackageDir" \
					"touch $stampFile"
				pushv binaryPackageStamp "$stampFile"
			fi
		done

		rule "$develDirVersioned/.stamp-unpack" "$phpSourcePackage ${binaryPackageStamps[*]}" \
			"mkdir -p $develDirVersioned" \
			"$scriptsDir/unpackphpsource $phpVersion $phpSourcePackage $develDirVersioned" \
			"touch $develDirVersioned/.stamp-unpack"
	}; end
}

foreachPhpVersion rulePhpVersion

ruleBuild() {
	local extVersionedStamp
	extVersionedStamp="$extDir/.stamp.${extVersion}"
	local name
	local buildDirVersioned

	begin "Generating rule for $ext-$extVersion build for $phpReleaseVersion"; {
		name="$extNiceName-$extVersion-$phpReleaseVersion"
		buildDirVersioned="$buildDir/$name"

		local _TS=
		[[ ! -z $nts ]] && _TS=
		[[ -z $nts ]] && _TS=_TS

		rule "$buildDirVersioned/.stamp-update" "$develDirVersioned/.stamp-unpack $extVersionedStamp" \
			"rm -rf $buildDirVersioned/" \
			"mkdir -p $buildDirVersioned/php" \
			"rsync $VERBOSE -au $develDirVersioned/ $buildDirVersioned/php/" \
			"test -f $buildDirVersioned/php/win32/build/ || rsync $VERBOSE -au $develDir/win32build/build/ $buildDirVersioned/php/win32/build/" \
			"$scriptsDir/fixsource $buildDirVersioned/php/" \
			"mkdir -p $buildDirVersioned/php/Release${_TS}/" \
			"cp $VERBOSE -au $phpReleaseDir/php*.lib $buildDirVersioned/php/Release${_TS}/" \
			"mkdir -p $buildDirVersioned/$ext" \
			"rsync $VERBOSE -Cau $extDirVersioned/ $buildDirVersioned/$ext/" \
			"touch $buildDirVersioned/.stamp-update"

		rule "$buildDirVersioned/php/configure.js" "$buildDirVersioned/.stamp-update" \
			"cd $buildDirVersioned/php && ${compiler}${arch}env.cmd cscript /nologo win32/build/buildconf.js --add-modules-dir=../" \
			"mv -f $buildDirVersioned/php/configure.js $buildDirVersioned/php/configure.js.tmp" \
			"$scriptsDir/fixconfigure $buildDirVersioned/php/configure.js.tmp" \
			"mv -f $buildDirVersioned/php/configure.js.tmp $buildDirVersioned/php/configure.js"

		local args=("${configureFlags[@]}")
		local phpMajorVersion=$(getVersionPart 0 "$phpVersion")
		[[ $phpMajorVersion -ge 6 ]] && pushv arg --with-static-icu
		[[ ! -z $nts ]] && pushv arg --disable-zts
		[[ -z $nts ]] && pushv arg --enable-zts
		rule "$buildDirVersioned/php/Makefile" "$buildDirVersioned/php/configure.js" \
			"cd $buildDirVersioned/php && ${compiler}${arch}env.cmd cscript.exe /nologo configure.js ${args[*]} \\
				--with-php-build=\"\$\$(cygpath -wa $topdir/win32build)\" \\
				--disable-all --enable-object-out-dir=. --enable-debug-pack \\
				--enable-$ext=yes,shared" \
			"mv -f $buildDirVersioned/php/Makefile $buildDirVersioned/php/Makefile.tmp" \
			"$scriptsDir/fixmakefile $buildDirVersioned/php/Makefile.tmp" \
			"mv -f $buildDirVersioned/php/Makefile.tmp $buildDirVersioned/php/Makefile"

		xcachetmp=$TMP/$name
		local extDll="$buildDirVersioned/php/Release${_TS}/php_${ext}.dll"
		rule "$extDll" "$buildDirVersioned/php/Makefile" \
			"mkdir -p $xcachetmp" \
			"cd $buildDirVersioned/php && MAKEFLAGS= TMP=$xcachetmp TEMP=$xcachetmp ${compiler}${arch}env.cmd nmake.exe /nologo php_${ext}.dll" \
			"rmdir $xcachetmp"

		addAllRule "$extDll"
	}; end
}

forExtVersion() {
	local extVersionedStamp
	extVersionedStamp="$extDir/.stamp.${extVersion}"

	rule "update-$ext" "update-$ext-$extVersion"
	rule "update-$ext-$extVersion $extVersionedStamp" "" \
		"$scriptsDir/extupdate $ext $extVersion"
}

forExt() {
	begin "Generating svn update rule for $ext-$extVersion"; {
		rule update update-$ext
		$@ forExtVersion
	}; end
	$@ foreachPhpVersion foreachPhpReleaseVersion ruleBuild
}

if [[ $# -gt 0 ]]; then
	loadExt $1
	shift
	if [[ $# -gt 0 ]]; then
		loadExtVersion $1
		forExt
	else
		forExt foreachExtVersion
	fi
else
	foreachExt forExt foreachExtVersion
fi

rule "package" "all" "scripts/updatepackage"

exec 4>&-
rule alltargets "$(cat all.tmp)"
rm -f all.tmp

exec 5>&-
rule clean "$(cat clean.tmp)"
rm -f clean.tmp

exec 3>&-
{
	echo "topdir=$topdir"
	echo "scriptsDir=$scriptsDir"
	sed Makefile.tmp \
		-e "s#$scriptsDir#\$(scriptsDir)#g" \
		-e "s#$topdir#\$(topdir)#g"
	rm -f Makefile.tmp
} > Makefile
