#! /bin/bash

TCLVERS="8.4.19"
if echo "$1" | grep '^[0-9][0-9]*\.' >/dev/null || echo "$1" | grep '^cvs_' >/dev/null; then
	TCLVERS="$1"

	shift
fi
export TCLVERS

# Determine appropriate mode
mode="build"
if [ "$1" = "clean" ]; then
	shift

	mode="clean"
fi
if [ "$1" = "distclean" ]; then
	shift

	mode="distclean"
fi

# Define the list of all packages, for cleaning purposes
KITCREATOR_ALLPKGS="kitsh tcl tclvfs zlib tk itcl mk4tcl thread"
for pkg in ${KITCREATOR_ALLPKGS}; do
	rm -f "${pkg}/build.log"
	rm -rf "${pkg}/out" "${pkg}/inst" "${pkg}/build"

	rm -rf "${pkg}/src"/tmp-*

	if [ "${mode}" = "distclean" ]; then
		rm -rf "${pkg}/src"
	fi
done

# We're all done if we're in clean or distclean mode
if [ "${mode}" = "clean" -o "${mode}" = "distclean" ]; then
	rm -f tclkit-*
	rm -f libtclkit*.*

	exit 0
fi

# Add packages implied by the additional arguments
if [ -z "${KITCREATOR_PKGS}" ]; then
	KITCREATOR_PKGS="tk itcl mk4tcl"
fi

CONFIGUREEXTRA="$@"
export CONFIGUREEXTRA

if echo " ${CONFIGUREEXTRA} " | grep ' --enable-threads' >/dev/null 2>/dev/null; then
	KITCREATOR_PKGS="${KITCREATOR_PKGS} thread"
fi

# Set default target
KITTARGET="kitsh"
export KITTARGET

# Fix up package list
## If building KitDLL, define it as our target
if echo " ${KITCREATOR_PKGS} " | grep ' kitdll ' >/dev/null 2>/dev/null; then
	KITCREATOR_PKGS="$(echo " ${KITCREATOR_PKGS} " | sed 's@ kitdll @ @g;s@^  *@@;s@  *$@@;s@   *@ @g')"
	KITTARGET="kitdll"
fi

# If the user manually requested "--enable-kitdll", make it so
if echo "x $@ " | grep ' [-][-]enable-kitdll ' >/dev/null 2>/dev/null; then
	KITTARGET="kitdll"
fi

## If building KitDLL and Tk, must do Tk statically
### (Well, we don't HAVE to, but it would defeat much of the purpose)
if [ "${KITTARGET}" = "kitdll" ]; then
	if echo " ${KITCREATOR_PKGS} " | grep ' tk ' >/dev/null 2>/dev/null; then
		if [ "${STATICTK}" != "1" -a "${STATICTK}" != "-1" ]; then
			echo 'Warning: Linking Tk statically because you are building KitDLL' 2>&1
			echo '         Set STATICTK to -1 if you really want to link Tk dynamically.' >&2

			STATICTK="1"
			export STATICTK
		fi

		if [ -z "${STATICMK4}" ]; then
			echo 'Warning: Linking Mk4tcl dynamically because you are building KitDLL' 2>&1
			echo '         Set STATICMK4 to 1 if you really want to link Mk4tcl statically.' >&2

			STATICMK4="0"
			export STATICMK4
		fi
	fi
fi

# Find Windows resource-related files
if [ -f "kit.ico" ]; then
	KITCREATOR_ICON="$(pwd)/kit.ico"
else
	KITCREATOR_ICON="$(echo "$(pwd)/kitsh/buildsrc"/kitsh-*/kit.ico)"
fi
if [ -f "kit.rc" ]; then
	KITCREATOR_RC="$(pwd)/kit.rc"
else
	KITCREATOR_RC="$(echo "$(pwd)/kitsh/buildsrc"/kitsh-*/kit.rc)"
fi
export KITCREATOR_ICON KITCREATOR_RC

failedpkgs=""
buildfailed="0"
for pkg in tcl tclvfs zlib ${KITCREATOR_PKGS} kitsh; do
	echo -n "Building ${pkg} ..."

	failed="0"
	(
		cd "${pkg}" >/dev/null 2>/dev/null || exit 1

		./build.sh > build.log 2>&1 || exit 1
	) || failed="1"

	if [ "${failed}" = "1" ]; then
		echo " failed."
		failedpkgs="${failedpkgs} ${pkg}"
	else
		echo " done."
	fi

	case "${pkg}" in
		tcl)
			TCLCONFIGDIR=$(find "$(pwd)/tcl/build" -name tclConfig.sh | head -1 | sed 's@/[^/]*$@@')
			export TCLCONFIGDIR
			;;
	esac

	if [ "${failed}" = "1" ]; then
		buildfailed="1"
	fi
done

if [ -n "${failedpkgs}" ]; then
	echo "Failed to build:${failedpkgs}"
fi

if [ "${buildfailed}" != "0" ]; then
	echo 'WARNING: Build is likely incomplete or failed.' >&2
fi

cp 'kitsh/build'/kitsh-*/libtclkit* . >/dev/null 2>/dev/null
cp 'kitsh/build'/kitsh-*/kit "tclkit-${TCLVERS}" >/dev/null 2>/dev/null

exit "${buildfailed}"
