#!/usr/bin/env modbuild

SOURCE_URL="http://github.com/xianyi/$P/archive/v$V.tar.gz"

pbuild::configure() {
	case ${COMPILER} in
	gcc )
		CC='gcc'
		;;
	intel )
		CC='icc'
		;;
	clang-macos )
		CC='gcc'
		;;
	* )
		die 3 "Oops: unknown compiler: ${COMPILER}"
		;;
	esac
	cat <<EOF > "${MODULE_SRCDIR}/make.inc"
SHELL = /bin/sh
PLAT = 
DRVOPTS  = \$(NOOPT)
ARCHFLAGS= -ru
EOF
	echo "USE_SIMPLE_THREADED_LEVEL3 = 1" >> "${MODULE_SRCDIR}/Makefile.rule"
	echo "NO_AVX = 1" >> "${MODULE_SRCDIR}/Makefile.rule"
	echo "NO_AVX2 = 1" >> "${MODULE_SRCDIR}/Makefile.rule"
	echo "USE_THREAD = 0" >> "${MODULE_SRCDIR}/Makefile.rule"
	sed -i.bak "s/MACOSX_DEPLOYMENT_TARGET=.*/MACOSX_DEPLOYMENT_TARGET=$MACOSX_DEPLOYMENT_TARGET/" "${MODULE_SRCDIR}/Makefile.system"
}

pbuild::build() {
	make -j3
}

pbuild::install() {
	make PREFIX="${PREFIX}" install
	# We have to build shared libs and remove them here.
	# Building with NOSHARED gives an error during install -
	# due to a bug in the Makefile(s).
	rm -f "${PREFIX}/lib/"*.so
	rm -f "${PREFIX}/lib/"*.dylib 
	#( cd "${PREFIX}/lib"; ln -fs
}

pbuild::add_to_group 'Compiler'
pbuild::compile_in_sourcetree
pbuild::set_docfiles 'LICENSE' 'README.md'
pbuild::set_supported_compilers 'gcc' 'intel' 'clang-macos'
pbuild::make_all
pbuild::cleanup_src
