#!/bin/sh

NAME="[boost]"
WORKING_DIR="boost/boost_1_60_0"

BUILD_DIR=build
STAGE_DIR=stage
TEMP_INC_DIR="stripped_orxonox"

source ./build_common.sh

if [ $DO_CLEAN -eq 1 ]
then
	rm -rf $BUILD_DIR
	check_result $? "cleaning build dir"
	rm -rf $STAGE_DIR
	check_result $? "cleaning stage dir"
	rm -rf "dist"
	check_result $? "cleaning dist dir"
	rm -rf $TEMP_INC_DIR
	check_result $? "cleaning temp include dir"
fi

if [ $DO_BUILD -eq 1 ]
then
	# build bjam
	./bootstrap.bat
	check_result $? "bootstrap"

	# compile libraries
	./b2 --build-dir=$BUILD_DIR --stagedir=$STAGE_DIR toolset=gcc variant=release link=shared threading=multi --build-type=complete stage -j8 \
			--with-thread --with-filesystem --with-system --with-date_time --with-atomic
	check_result $? "compiling"

	# copy libraries into target directory
	find $STAGE_DIR -name "*.dll" -exec cp -a {} ${TARGET_BIN_DIR} \;
	check_result $? "copying libraries"

	# build bcp
	cd tools/bcp
	../../bjam toolset=gcc -j8
	check_result $? "building bcp"
	cd ../..

	# prepare includes
	mkdir -p ${TEMP_INC_DIR}
	check_result $? "creating temp include dir"

	./dist/bin/bcp atomic bimap date_time filesystem preprocessor thread ${TEMP_INC_DIR}
	check_result $? "creating stripped includes"

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

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