#!/bin/sh

NAME="[ogredeps]"
WORKING_DIR="ogre/cabalistic-ogredeps-7168c50f9d04"

BUILD_DIR=build

source ./build_common.sh

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

if [ $DO_BUILD -eq 1 ]
then
	# modify build script: make zlib a SHARED library (instead of a STATIC one)
	CMAKE_FILE="src/zlib/CMakeLists.txt"
	sed -i "s/zlib STATIC /zlib SHARED /g" $CMAKE_FILE
	check_result $? "modifying build script"

	# prepare build
	mkdir -p $BUILD_DIR
	check_result $? "creating build dir"
	cd $BUILD_DIR

	# run cmake
	cmake .. -G "MSYS Makefiles" -DOGREDEPS_BUILD_SDL2=false
	check_result $? "cmake"

	# compile
	make -j8
	check_result $? "make"
	make -j8 install
	check_result $? "make install"

	# copy zlib and cg into target directory
	cp -a "ogredeps/bin/Release/libzlib.dll" ${TARGET_BIN_DIR}
	check_result $? "copying library"
	cp -a "ogredeps/bin/Release/cg.dll" ${TARGET_BIN_DIR}
	check_result $? "copying cg"

	# copy zlib headers into target directory
	ZLIB_INC_DIR="${TARGET_INC_DIR}/zlib/include"
	mkdir -p $ZLIB_INC_DIR
	check_result $? "creating include dir"
	cp -a "ogredeps/include/zconf.h" ${ZLIB_INC_DIR}
	check_result $? "copying zconf.h"
	cp -a "ogredeps/include/zlib.h" ${ZLIB_INC_DIR}
	check_result $? "copying zlib.h"

	# copy whole dependency folder to TEMP_DIR for future build steps (i.e. ogre)
	cp -aT "ogredeps" "${TEMP_DIR}/ogredeps"
	check_result $? "copying dependencies to TEMP_DIR"

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