#!/bin/sh

NAME="[cegui-0-7]"
WORKING_DIR="cegui/CEGUI-0.7.9"

source ./build_common.sh

if [ $DO_CLEAN -eq 1 ]
then
	rm -rf "dependencies"
	check_result $? "remove dependencies"
	cd projects/premake
	make clean
	# the above command may fail if Makefiles were already removed
	find "." -name "Makefile" -exec rm -rf {} \;
	check_result $? "remove Makefiles"
	find "." -name "*.o" -exec rm -rf {} \;
	check_result $? "remove build output"
	find "." -name "*.d" -exec rm -rf {} \;
	check_result $? "remove build output"
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"
	
	# create subdirectory to fix an include path in premake script
	mkdir -p "cegui/include/ScriptingModules/LuaScriptModule/support/tolua++bin"

	cd projects/premake

	# prepare config.lua
	CONFIG_FILE="config.lua"
	rm -rf $CONFIG_FILE
	check_result $? "removing old config-file"
	echo "CEGUI_EXTRA_PATHS = {
			{ \"${HOME_DIR}/${WORKING_DIR}/dependencies\", \"include\", \"lib\" },
			{ \"${TARGET_BIN_DIR}\", \"\", \"\" },
			{ \"${TARGET_INC_DIR}\", \"lua/include\", \"\" },
			{ \"${TARGET_INC_DIR}\", \"ogre/include\", \"\", \"CEGUIOgreRenderer\" },
			{ \"${TARGET_INC_DIR}\", \"boost\", \"\", \"CEGUIOgreRenderer\" },
		}
		CEGUI_LUA_VER = 51
		CEGUI_USE_FREETYPE = true
		CEGUI_USE_PCRE_REGEX = true
		CEGUI_USE_DEFAULT_LOGGER = true
		OGRE_RENDERER = true
		FALAGARD_WR = true
		DEFAULT_WINDOW_RENDERER = \"falagard\"
		XERCES_PARSER = true
		DEFAULT_XML_PARSER = \"xerces\"
		LUA_SCRIPT_MODULE = true
	" > $CONFIG_FILE
	check_result $? "create new config-file"
	
	# change library names in several premake scripts
	sed -i "s/\"lua\"/\"lua51\"/" "ScriptingModules/LuaScriptModule/premake.lua"
	check_result $? "change lua library name"
	sed -i "s/\"lua\"/\"lua51\"/" "ScriptingModules/LuaScriptModule/support/tolua++/premake.lua"
	check_result $? "change lua library name"
	sed -i "s/\"lua\"/\"lua51\"/" "ScriptingModules/LuaScriptModule/support/tolua++bin/premake.lua"
	check_result $? "change lua library name"
	sed -i "s/\"xerces-c_3\"/\"xerces-c\"/" "XMLParserModules/XercesParser/premake.lua"
	check_result $? "change xerces library name"
	sed -i "s/library(\"OgreMain\", \"_d\")/library(\"OgreMain\"); library(\"libboost_system-mgw48-mt-1_60\")/" "RendererModules/Ogre/premake.lua"
	check_result $? "add boost system library"

	# run premake
	./premake --file cegui.lua --target gnu
	check_result $? "premake"

	# compile
	CC=gcc \
	make all -j8 CONFIG=Release
	check_result $? "make"

	# copy libraries
	find "../../bin" -name "*.dll" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copy libraries"

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

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