#! /bin/bash

TCLVERS="8.4.19"
export TCLVERS

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

for pkg in tcl tk itcl mk4tcl tclvfs memchan kitsh; do
	if [ "${mode}" = "distclean" ]; then
		rm -rf "${pkg}/src"
	fi

	if [ "${mode}" = "clean" -o "${mode}" = "distclean" ]; then
		rm -f "${pkg}/build.log"
		rm -rf "${pkg}/out" "${pkg}/inst" "${pkg}/build"

		continue
	fi

	(
		cd "${pkg}" || exit 1

		./build.sh > build.log 2>&1 || exit 1
	) || failed="${failed} ${pkg}"

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

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

exit 0
