#!/bin/sh

NAME="[tcl]"
WORKING_DIR="tcl/tcl8.6.4"

source ./build_common.sh

# remove the 'pkgs' directory to avoid building unnecessary packages
rm -rf "pkgs"

# go to 'win' directory
cd "win"
check_result $? "cd win"

if [ $DO_CLEAN -eq 1 ]
then
	make clean
	check_result $? "cleaning build"
fi

if [ $DO_BUILD -eq 1 ]
then
	# configure
	./configure
	check_result $? "configure"

	# compile
	make -j8
	if [ $? -ne 0 ]
	then
		# sometimes the first make-run fails to build the *.exe target -> we call make again and only check the result of the second call
		echo "${NAME} trying again to build target..."
		make
		check_result $? "make"
	fi

	# copy lib
	find "." -name "tcl??.dll" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copy tcl-lib"
	find "." -name "zlib*.dll" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copy zlib-lib"

	# copy headers
	HEADERS_DIR=${TARGET_INC_DIR}/tcl/include
	mkdir -p $HEADERS_DIR
	check_result $? "create headers directory"
	find "../generic/" -name "*.h" -exec cp -a {} ${HEADERS_DIR} \;
	check_result $? "copy headers"
	
	# copy tcl-script library
	cp -aT "../library" "${TARGET_LIB_DIR}/tcl"
	check_result $? "copy script library"

	echo "${NAME} Finished building ${NAME}"
fi
