#!/bin/sh
usage() {
	
	cat << EOF
usage :
	--help               : show usage
	--prefix=            : set installation prefix [/usr/local]
	--libvol-prefix=     : set where to find vol librairies and headers (optionnal)
	--cc=                : set C++ compiler        [g++]
EOF

}

my_which() {

#	location=`which which`
#	if [ "$location" = "/usr/bin/which" ]
#	then
#		which "$1" | grep -v "^no $1 in" >/dev/null
#		return $?
#	else
#		which "$1" > /dev/null 2>&1
#		return $?
#	fi

	type $1 > /dev/null 2>&1
	return $?
}

PREFIX=/usr/local
CC=g++
VOL_INCLUDE=
VOL_LPATH=

for i in $*
do
	case "$i" in 
	--prefix=*)
		PREFIX=`echo "$i" | cut -d = -f2`
		;;
	--help)
		usage
		exit 0
		;;
	--cc=*)
		CC=`echo "$i" | cut -d = -f2`
		;;
	--libvol-prefix=*)
		volprefix=`echo "$i" | cut -d = -f2`
		VOL_INCLUDE="-I$volprefix/include"
		VOL_LPATH="-L$volprefix/lib"
		;;
	*)
		usage
		exit 1
	esac
done

printf "%s" "Checking for $CC ... "
if my_which $CC
then
	printf "ok\n"
else
	printf "%s\n" "NOT FOUND"
	exit 1
fi

# check that libvol works !
cat > /tmp/$$.cc << EOF
#include <vol.h>

int main() {
	Vol v(10, 10, 10, 0);
	return 0;
}
EOF

printf "checking that libvol works ... "
if $CC -c /tmp/$$.cc -o /tmp/$$.e $VOL_INCLUDE $VOL_LPATH -lvol -lm > /dev/null 2>&1
then
	printf "ok\n"
	rm -f /tmp/$$.cc
else
	printf "FAILED !\nCheck that libvol is correctly installed.\n"
	exit 1
fi

cat > makefile.in << EOF
CC=$CC
PREFIX=$PREFIX
VOL_INCLUDE=$VOL_INCLUDE
VOL_LPATH=$VOL_LPATH
EOF
