#!/bin/sh

NAME="[cegui]"
WORKING_DIR="cegui/cegui-0.8.4"

BUILD_DIR=build

source ./build_common.sh

if [ $DO_CLEAN -eq 1 ]
then
	rm -rf $BUILD_DIR
	check_result $? "cleaning build dir"
	rm -rf "dependencies"
	check_result $? "remove dependencies"
fi

if [ $DO_BUILD -eq 1 ]
then
	# copy cegui dependencies
	cp -aT "${TEMP_DIR}/ceguideps" "dependencies"
	check_result $? "copy ceguideps dependencies"
	
	# merge ogre dependencies into cegui dependencies
	cp -aT "${TEMP_DIR}/ogredeps/include" "dependencies/include"
	check_result $? "copy includes"
	cp -aT "${TEMP_DIR}/ogredeps/bin/Release" "dependencies/lib"
	check_result $? "copy bins"
	cp -aT "${TEMP_DIR}/ogredeps/lib/Release" "dependencies/lib"
	check_result $? "copy libs"
	
	# prepare build directory
	mkdir -p $BUILD_DIR
	check_result $? "creating build dir"
	cd $BUILD_DIR
	
	# run cmake
	# add flag for FreeImage to be compatible with the ogre-dependencies
	CXXFLAGS="-DFREEIMAGE_LIB" \
	cmake .. -G "MSYS Makefiles" \
			-DOGRE_H_PATH=${TARGET_INC_DIR}/ogre/include \
			-DOGRE_H_BUILD_SETTINGS_PATH=${TARGET_INC_DIR}/ogre/include \
			-DOGRE_LIB=${TARGET_BIN_DIR}/OgreMain.dll \
			-DBOOST_LIBRARYDIR=${TARGET_BIN_DIR} \
			-DBOOST_INCLUDEDIR=${TARGET_INC_DIR}/boost
	check_result $? "cmake"

	# compile
	make -j8
	check_result $? "make"

	# copy libraries into target directory
	find "bin" -name "libCEGUI*.dll" -not -name "*Demo*" -not -name "*Minesweeper*" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copying CEGUI-libraries"

	# copy includes
	mkdir -p ${TARGET_INC_DIR}/cegui
	check_result $? "creating include dir"
	cp -aT "../cegui/include/CEGUI" ${TARGET_INC_DIR}/cegui/include
	check_result $? "copying includes"
	cp -aT "cegui/include/CEGUI" ${TARGET_INC_DIR}/cegui/include
	check_result $? "copying generated includes"

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