#!/bin/sh

NAME="[main]"

if [ $# -eq 1 ] && [ $1 = "list" ]
then
	DO_LIST=1
	DO_CLEAN=0
	DO_BUILD=0

	echo "${NAME} Listing all build targets..."
	echo "${NAME}    Special targets:"
	echo "${NAME}        all"
	echo "${NAME}"
	echo "${NAME}    Build targets:"
elif [ $# -eq 2 ] && [ $1 = "clean" ]
then
	DO_LIST=0
	DO_CLEAN=1
	DO_BUILD=0
	TARGET=$2

	if [ $TARGET = "all" ]
	then
		echo "${NAME} Cleaning all..."
		DO_ALL=1
	else
		echo "${NAME} Cleaning '${TARGET}'..."
		DO_ALL=0
	fi
elif [ $# -eq 4 ]
then
	DO_LIST=0
	DO_CLEAN=0
	DO_BUILD=1
	TARGET=$2
	ARCHITECTURE=$3 # 32 or 64 (bit)
	TARGET_DIR=$4 # where to put the resulting headers and binaries?

	if [ $TARGET = "all" ]
	then
		echo "${NAME} Build all dependencies into '${TARGET_DIR}'"
		DO_ALL=1
	else
		echo "${NAME} Build '${TARGET}' into '${TARGET_DIR}'"
		DO_ALL=0
	fi
else
	echo "${NAME} Usage:"
	echo "${NAME}   List targets: $0 list"
	echo "${NAME}   Clean targets: $0 clean <TARGET>"
	echo "${NAME}   Build targets: $0 build <TARGET> <[32|64]> <TARGET_DIR>"
	exit 1
fi

check_result() {
	if [ $1 -ne 0 ]
	then
		echo "${NAME} $2 failed: $1"
		echo "${NAME} Exiting..."
		exit 1
	fi
}

if [ $DO_CLEAN -eq 1 ] || [ $DO_BUILD -eq 1 ]
then
	SLEEP=0 # sleep between builds?
	TEMP_DIR="${TARGET_DIR}/_temp"
fi

# ---------- Clean ----------
#if [ $DO_CLEAN -eq 1 ] && [ $DO_ALL -eq 1 ]
#then
#	rm -rf $TEMP_DIR
#	check_result $? "clean temp directory"
#fi

# ---------- Build target directory ----------
if [ $DO_BUILD -eq 1 ]
then
	if [ $ARCHITECTURE = "32" ]
	then
		echo "${NAME} Building for 32bit architecture"
		TOOLCHAIN_NAME="mingw-x86"
	elif [ $ARCHITECTURE = "64" ]
	then
		echo "${NAME} Building for 64bit architecture"
		TOOLCHAIN_NAME="mingw-x64"
	else
		echo "${NAME} Unsupported architecture $ARCHITECTURE (must be either 32 or 64)"
		exit 1
	fi

	echo "${NAME} Preparing target directory..."

	TARGET_BIN_DIR="${TARGET_DIR}/dependencies/bin/${TOOLCHAIN_NAME}"
	TARGET_INC_DIR="${TARGET_DIR}/dependencies/include"
	TARGET_LIB_DIR="${TARGET_DIR}/dependencies/lib/${TOOLCHAIN_NAME}"

	mkdir -p $TARGET_BIN_DIR
	check_result $? "creating target bin directory"
	mkdir -p $TARGET_INC_DIR
	check_result $? "creating target include directory"
	mkdir -p $TARGET_LIB_DIR
	check_result $? "creating target lib directory"

	if [ $ARCHITECTURE -eq 32 ]
	then
		export DXSDK_DIR="/mingw32/i686-w64-mingw32"

		cp -a /mingw32/bin/libgcc_s_dw2-1.dll $TARGET_BIN_DIR
		check_result $? "copying libgcc"
		cp -a /mingw32/bin/libstdc++-6.dll $TARGET_BIN_DIR
		check_result $? "copying libstdc++"
		cp -a /mingw32/bin/libwinpthread-1.dll $TARGET_BIN_DIR
		check_result $? "copying libwinpthread"
	else
		export DXSDK_DIR="/mingw64/x86_64-w64-mingw32"

		cp -a /mingw64/bin/libgcc_s_seh-1.dll $TARGET_BIN_DIR
		check_result $? "copying libgcc"
		cp -a /mingw64/bin/libstdc++-6.dll $TARGET_BIN_DIR
		check_result $? "copying libstdc++"
		cp -a /mingw64/bin/libwinpthread-1.dll $TARGET_BIN_DIR
		check_result $? "copying libwinpthread"
	fi

	mkdir -p $TEMP_DIR
	check_result $? "create _temp"

	echo "${NAME} Building libraries..."
fi

# ---------- Build dependencies ----------
run_build() {
	if [ $# -ne 2 ]
	then
		exit 1
	fi
	
	BUILD_NUM=$1
	BUILD_NAME=$2
	BUILD_LABEL="${NAME} ${BUILD_NUM}/${NUMBER_OF_BUILDS}"
	
	if [ $DO_LIST -eq 1 ]
	then
		echo "${NAME}        $2"
	elif [ $DO_CLEAN -eq 1 ]
	then
		if [ $DO_ALL -eq 1 ] || [ $TARGET = $BUILD_NAME ]
		then
			./build_${BUILD_NAME}.sh "clean"
			check_result $? "cleaning $BUILD_NAME"
			echo "${BUILD_LABEL} $BUILD_NAME done"
		else
			echo "${BUILD_LABEL} skipping $BUILD_NAME"
		fi
	elif [ $DO_BUILD -eq 1 ]
	then
		if [ $DO_ALL -eq 1 ] || [ $TARGET = $BUILD_NAME ]
		then
			sleep $SLEEP
			./build_${BUILD_NAME}.sh $TARGET_BIN_DIR $TARGET_INC_DIR $TARGET_LIB_DIR $TEMP_DIR
			check_result $? "building $BUILD_NAME"
			echo "${BUILD_LABEL} $BUILD_NAME done"
		else
			echo "${BUILD_LABEL} skipping $BUILD_NAME"
		fi
	fi
}

NUMBER_OF_BUILDS=12

# note: enable either cegui+toluapp OR cegui-0-7
run_build  1 "boost"
run_build  2 "ogredeps"
run_build  3 "ogre"
run_build  4 "xerces"
run_build  5 "pcre"
run_build  6 "lua"
#run_build  7 "toluapp"
#run_build  8 "cegui"
run_build  8 "cegui-0-7"
run_build  9 "ogg"
run_build 10 "vorbis"
run_build 11 "freealut"
run_build 12 "tcl"

if [ $DO_BUILD -eq 1 ] || [ $DO_CLEAN -eq 1 ]
then
	echo "${NAME} done."
fi
