#!/bin/sh

NAME="[freealut]"
WORKING_DIR="freealut/freealut-freealut_1_1_0"
AL_SDK_DIR="C:/Program Files (x86)/OpenAL 1.1 SDK"

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
	# prepare OpenAL dependencies
	AL_INCLUDES="${TARGET_INC_DIR}/openal/include"
	mkdir -p ${AL_INCLUDES}
	check_result $? "create OpenAL include dir"
	cp -aT "${AL_SDK_DIR}/include" "${AL_INCLUDES}/AL"
	check_result $? "copy OpenAL includes"
		
	# prepare build
	mkdir -p $BUILD_DIR
	check_result $? "creating build dir"
	cd $BUILD_DIR

	# run cmake
	cmake .. -G "MSYS Makefiles" -DOPENAL_INCLUDE_DIR:PATH=${AL_INCLUDES}
	check_result $? "cmake"

	# compile
	make -j8
	check_result $? "make"

	# copy lib
	cp -a "libalut.dll" ${TARGET_BIN_DIR}
	check_result $? "copying library"
	
	# copy headers
	HEADER_DIR="${TARGET_INC_DIR}/freealut/include/AL"
	mkdir -p ${HEADER_DIR}
	check_result $? "create header dir"
	find "../include/AL" -name "*.h" -exec cp -a {} ${HEADER_DIR} \;
	check_result $? "copying headers"

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