#!/bin/sh
#
# Copyright (c) 2019 David Demelier <markand@malikania.fr>
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

PKGNAME=boost
PKGVERSION=1.70.0
_VERSION=$(echo $PKGVERSION | tr "." "_")
PKGREVISION=1
PKGLICENSE="BOOST"
PKGSUMMARY="peer-reviewed C++ libraries"
PKGDOWNLOAD="https://dl.bintray.com/boostorg/release/$PKGVERSION/source/${PKGNAME}_$_VERSION.tar.gz"
PKGDEPENDS="bzip2 xz zlib zstd"
PKGOPTIONS="ICU PYTHON"

: ${CXX:=clang++}
: ${CXXFLAGS:=-O2}
: ${LDFLAGS:=}
: ${ICU:=yes}
: ${PYTHON:=yes}

if [ -n "$CXXFLAGS" ]; then
	with_cxxflags="cxxflags=\"$CXXFLAGS\""
fi

if [ -n "$LDFLAGS" ]; then
	with_linkflags="linkflags=\"$LDFLAGS\""
fi

if [ "$ICU" = "yes" ]; then
	PKGDEPENDS="icu $PKGDEPENDS"
	with_bootstrap_icu="--with-icu"
else
	with_bootstrap_icu="--without-icu"
	with_icu="--without-icu"
fi

if [ "$PYTHON" = "yes" ]; then
	PKGDEPENDS="python $PKGDEPENDS"
	with_bootstrap_python="--with-python=/bin/python"
else
	with_python="--without-python"
fi

case "$CXX" in
"clang++")
	with_toolset="clang"
	;;
"g++")
	with_toolset="g++"
	;;
esac

build()
{
	rm -rf ${PKGNAME}_$_VERSION
	tar xvf ${PKGNAME}_$_VERSION.tar.gz
	cd ${PKGNAME}_$_VERSION

	touch user-config.jam

	if [ "$PYTHON" = "yes" ]; then
		echo "using python : 3.7 : /bin/python : /include/python3.7m : : : : ;" >> user-config.jam
	fi

	./bootstrap.sh \
		--with-toolset=$with_toolset \
		$with_bootstrap_icu \
		$with_bootstrap_python

	# --layout=system: generate simple libraries names (no suffixes).
	./b2 \
		--user-config=user-config.jam \
		--layout=system \
		--prefix=$DESTDIR \
		link=static,shared \
		debug-symbols=off \
		runtime-link=shared \
		threading=multi \
		variant=release \
		toolset=$with_toolset \
		$with_cxxflags \
		$with_linkflags \
		$with_python \
		$with_icu \
		install

	cd ..
	rm -rf ${PKGNAME}_$_VERSION
}
