#!/bin/sh

# Go to the root
cd ${0%%/*}
ROOTDIR=`pwd`

###
# Defaults
###
VERSION=0.2.99
PREFIX=/usr/local
PREFIX_BIN=${PREFIX}/bin
PREFIX_LIB=${PREFIX}/lib/tuxanci
PREFIX_DATA=${PREFIX}/share/tuxanci
PREFIX_DOC=${PREFIX}/share/doc/tuxanci-${VERSION}
PREFIX_CONF=/etc
PREFIX_LOCALE=${PREFIX}/share/locale
PREFIX_FONT=${PREFIX_DATA}/font
DESTDIR=
DEBUG=1
SERVER=0
CLIENT=1
AUDIO=1
OPENGL=1
GETTEXT=1

###
# Predefined functions
###
help() {
	echo "Supported options are:"
	echo "--help                    print this help and exit"
	echo "--prefix=<path>           final path for the game [/usr/local]"
	echo "--prefix-bin=<path>       path for binaries [\$prefix/bin]"
	echo "--prefix-lib=<path>       path for the modules [\$prefix/lib/tuxanci]"
	echo "--prefix-data=<path>      data path [\$prefix/share/tuxanci]"
	echo "--prefix-doc=<path>       documentation path [\$prefix/share/doc/tuxanci-${VERSION}]"
	echo "--prefix-conf=<path>      configuration files path [/etc]"
	echo "--prefix-locale=<path>    locale path [\$prefix/share/locale]"
	echo "--prefix-font=<path>      fonts path [\$prefix/share/tuxanci/font]"
	echo "--destdir=<path>          useful option for packagers"
	echo ""
	echo "--disable-debug           enables optimization and quiet output [default: no]"
	echo "--enable-server           build server [default: no]"
	echo "--enable-client           build client [default: yes]"
	echo "--enable-audio            build with audio facility [default: yes]"
	echo "--enable-opengl           build with 3D acceleration [default: yes]"
	echo ""
	echo "--without-gettext         English-only version [default: no]"
}

check_pkg() {
	lib="$1"
	[ "$2" ] && lib="${lib} $2"
	[ "$3" ] && lib="${lib} $3"
	if [ "`pkg-config --exists --print-errors "${lib}" | grep "No package '$1' found"`" != "" ]; then
		echo "no"
		echo "Error: Cannot find ${lib}"
		exit 1
	else
		echo "yes"
	fi
}

cmpver() {
	##
	# 0: equal
	# 1: newer
	# 2: older
	##

	v1=`echo $1 | sed -e "s/\./ /g"`
	v2=`echo $2 | sed -e "s/\./ /g"`

	# major
	j1=`echo $v1 | awk '{ print $1 }'`
	j2=`echo $v2 | awk '{ print $1 }'`
	if [ $j1 -lt $j2 ]; then
		echo 1 && exit
	elif [ $j1 -gt $j2 ]; then
		echo 2 && exit
	fi      

	# minor
	n1=`echo $v1 | awk '{ print $2 }'`
	n2=`echo $v2 | awk '{ print $2 }'`
	if [ $n1 -lt $n2 ]; then
		echo 1 && exit
	elif [ $n1 -gt $n2 ]; then
		echo 2 && exit
	fi

	# patch
	p1=`echo $v1 | awk '{ print $3 }'`
	p2=`echo $v2 | awk '{ print $3 }'`
	if [ $p1 -lt $p2 ]; then
		echo 1 && exit
	elif [ $p1 -gt $p2 ]; then
		echo 2 && exit
	fi

	# default
	echo 0 && exit
}

###
# Parse options
###
echo "==> Parsing options"

while [ $# -gt 0 ]; do
	case $1 in
		--help)
			help
			exit 0
			;;
		--prefix=*)
			PREFIX=`echo $1 | sed 's/--prefix=//'`
			;;
		--prefix-bin=*)
			PREFIX_BIN=`echo $1 | sed 's/--prefix-bin=//'`
			;;
		--prefix-data=*)
			PREFIX=`echo $1 | sed 's/--prefix-data=//'`
			;;
		--prefix-doc=*)
			PREFIX=`echo $1 | sed 's/--prefix-doc=//'`
			;;
		--destdir=*)
			DESTDIR=`echo $1 | sed 's/--destdir=//'`
			;;
		--enable-*)
			case ${1#--enable-} in
				debug)
					DEBUG=1
					;;
				server)
					SERVER=1
					;;
				client)
					CLIENT=1
					;;
				audio)
					AUDIO=1
					;;
				opengl)
					OPENGL=1
					;;
				*)
				echo "Error: unknown option --enable-$1"
					help
					exit 1
					;;
			esac
			;;
		--disable-*)
			case ${1#--disable-} in
				debug)
					DEBUG=0
					;;
				server)
					SERVER=0
					;;
				client)
					CLIENT=0
					;;
				audio)
					AUDIO=0
					;;
				opengl)
					OPENGL=0
					;;
				*)
				echo "Error: unknown option --disable-$1"
					help
					exit 1
					;;
			esac
			;;
		--with-*)
			case ${1#--with-} in
				gettext)
					GETTEXT=1
					;;
				*)
				echo "Error: unknown option --with-$1"
					help
					exit 1
					;;
			esac
			;;
		--without-*)
			case ${1#--with-} in
				gettext)
					GETTEXT=0
					;;
				*)
				echo "Error: unknown option --without-$1"
					help
					exit 1
					;;
			esac
			;;
		*)
		echo "Error: unknown option $1"
			help
			exit 1
			;;
	esac
	shift
done


###
# Check for cmake dependency
###
printf "==> Checking for cmake >= 2.6.0... "
cmakebin=`which cmake`
if [ "${cmakebin}" == "" ]; then
	echo "no"
	echo "Error: Cannot find cmake"
	exit 1
else
	cmakeversion=`${cmakebin} --version | sed -e "s/-patch//" | awk '{ print $3, $4 }' | sed -e "s/ /./"`
	cmakestatus=`cmpver "2.6.0" ${cmakeversion}`
	if [ ${cmakestatus} == 1 ] || [ ${cmakestatus} == 0 ]; then
		echo "yes"
	else
		echo "no"
		echo "Error: Cannot find cmake"
		exit 1
	fi
fi


###
# Run cmake for client
###
if [ ${CLIENT} == 1 ]; then
	echo "==> Running cmake stage for client and creating subMakefile"
	rm -rf build_client/
	mkdir build_client/
	cd build_client/
	cmakeopts="-DBUILD_SERVER=0"
	# debug
	if [ ${DEBUG} == 1 ]; then cmakeopts="${cmakeopts} -DENABLE_DEBUG=1"
	else cmakeopts="${cmakeopts} -DENABLE_DEBUG=0"; fi
	# audio
	if [ ${AUDIO} == 1 ]; then cmakeopts="${cmakeopts} -DWITH_AUDIO=1"
	else cmakeopts="${cmakeopts} -DWITH_AUDIO=0"; fi
	# opengl
	if [ ${OPENGL} == 1 ]; then cmakeopts="${cmakeopts} -DWITH_OPENGL=1"
	else cmakeopts="${cmakeopts} -DWITH_OPENGL=0"; fi
	# gettext
	if [ ${GETTEXT} == 1 ]; then cmakeopts="${cmakeopts} -DWITH_NLS=1"
	else cmakeopts="${cmakeopts} -DWITH_NLS=0"; fi

	cmakecmd="cmake .. ${cmakeopts}"
	echo "Executing: ${cmakecmd}"
	${cmakecmd} > cmake.log
fi


echo "==> Creating main Makefile"
cat > Makefile <<EOF
EOF

echo "==> Configuring successfully finished!"
