#!/bin/sh

NAME="[lua]"
WORKING_DIR="lua/lua-5.1.5"

source ./build_common.sh

if [ $DO_CLEAN -eq 1 ]
then
	make clean
	check_result $? "cleaning build"
#	# only necessary in versions 5.2 and above
#	rm -rf "install"
#	check_result $? "delete install directory"
fi

if [ $DO_BUILD -eq 1 ]
then
	# compile
	make mingw -j8
	check_result $? "make"
	make local
	check_result $? "make local"

	LIB_DIR=src
	HEADER_DIR=include # version 5.1
#	HEADER_DIR=install/include # version 5.2 and above
	
	# copy lib
	find $LIB_DIR -name "*.dll" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copying library"
	
	# copy headers
	mkdir -p "${TARGET_INC_DIR}/lua"
	check_result $? "create include dir"
	cp -aT $HEADER_DIR "${TARGET_INC_DIR}/lua"
	check_result $? "copying includes"

	# add to ceguideps
	mkdir -p "${TEMP_DIR}/ceguideps/lib"
	check_result $? "create lib dir"
	mkdir -p "${TEMP_DIR}/ceguideps/include"
	check_result $? "create include dir"
	
	find $LIB_DIR -name "*.dll" -exec cp -a {} "${TEMP_DIR}/ceguideps/lib" \;
	check_result $? "copy lib"
	cp -aT $HEADER_DIR "${TEMP_DIR}/ceguideps/include/lua"
	check_result $? "copy includes"

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