#! /bin/bash

OS=`uname -s | tr '[:upper:]' '[:lower:]'`
ARCH=`uname -p`
TARGET=debug
TOOLCHAIN=gcc
HOST=""
EXTERNAL_ZLIB="no"
EXTERNAL_ZLIB_CPPFLAGS=""
EXTERNAL_ZLIB_LDFLAGS=""
EXTERNAL_LIBXML="no"
EXTERNAL_LIBXML_CPPFLAGS=""
EXTERNAL_LIBXML_LDFLAGS=""


while test $# -ge 1
do
case "$1" in
	-h* | --help)
		echo 'usage:'
		echo ' configure [--with-python="PATH TO PYTHON EXECUTABLE"]'
         echo '           [--platform="OS-TARGET-TOOLCHAIN", e.g. --platform="linux-debug-gcc-i386"]'
         echo '           [--os="OS", e.g. --os="linux"]'
         echo '           [--target="TARGET", e.g. --target="release"]'
         echo '           [--toolchain="TOOLCHAIN", e.g. --toolchain="clang"]'
         echo '           [--arch="ARCH", e.g. --arch="i386"]'
		exit 0 ;;
	--with-python=*) PYTHON_PATH=`echo $1 | sed 's/.*=//'`; shift ;;
	--platform=*) PLATFORM=`echo $1 | sed 's/.*=//'`; shift ;;
	--target=*) TARGET=`echo $1 | sed 's/.*=//'`; shift ;;
	--toolchain=*) TOOLCHAIN=`echo $1 | sed 's/.*=//'`; shift ;;
	--os=*) OS=`echo $1 | sed 's/.*=//'`; shift ;;
	--arch=*) ARCH=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-zlib=*) EXTERNAL_ZLIB=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-zlib-cppflags=*) EXTERNAL_ZLIB_CPPFLAGS=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-zlib-ldflags=*) EXTERNAL_ZLIB_LDFLAGS=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-libxml=*) EXTERNAL_LIBXML=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-libxml-cppflags=*) EXTERNAL_LIBXML_CPPFLAGS=`echo $1 | sed 's/.*=//'`; shift ;;
	--with-libxml-ldflags=*) EXTERNAL_LIBXML_LDFLAGS=`echo $1 | sed 's/.*=//'`; shift ;;
	*) echo "unknown option: $1"; echo "$0 --help for help"; exit 1 ;;
esac
done

BUILDDIR="build"

PLIBCONFIG=""
PLIBFILES="include/plib/plib.h=>%PACKAGE%/config/%PLATFORM%/plib/include/plib/"

ZLIBCONFIG=""
ZLIBFILES="zconf.h=>config/%PLATFORM%/%PACKAGE%/"
LIBXMLCONFIG="--without-c14n \
--without-catalog \
--without-docbook  \
--without-fexceptions \
--without-ftp \
--without-history  \
--without-html \
--without-http \
--without-iconv \
--without-iso8859x \
--without-legacy \
--without-output \
--without-pattern \
--without-push \
--without-python \
--with-reader \
--without-regexps \
--without-sax1 \
--without-schemas \
--without-schematron \
--without-threads \
--without-tree \
--without-valid \
--with-writer \
--without-xinclude \
--without-xpath \
--without-xptr \
--without-modules \
--without-zlib"
LIBXMLFILES="config.h=>config/%PLATFORM%/%PACKAGE%/ include/libxml/xmlversion.h=>config/%PLATFORM%/%PACKAGE%/libxml"

execConfigure() {
	RET=0
	SRCDIR=$1
	PACKAGE=$2
	OPTIONS=$3
	FILES=$4
	OPT_HOST=$5
	echo "Configuring $SRCDIR/$PACKAGE"
	mkdir -p "$BUILDDIR/copy"
	rm -rf "$BUILDDIR/copy/$PACKAGE"
	rm -rf "$BUILDDIR/$PLATFORM/$PACKAGE"
	cp -r "$SRCDIR/$PACKAGE" "$BUILDDIR/copy/$PACKAGE"
	if [ -d "$BUILDDIR/copy/$PACKAGE" ]; then
		pushd "$BUILDDIR/copy/$PACKAGE" > /dev/null
		if [ -x "./configure" ]; then
			./configure $OPT_HOST $OPTIONS
			if [ $? -ne 0 ]; then
				popd -n > /dev/null
				echo "*** error running \"./configure $OPT_HOST $OPTIONS\" in \"$BUILDDIR/$PACKAGE\""
				echo "*** please fix this first!"
				exit 1
			fi
		else
			echo "configure not found in directory \"$BUILDDIR/$PACKAGE\""
		fi
		popd > /dev/null

		PAIR=$FILES
		for PAIR in $FILES; do
			FILE=(${PAIR/=>/ })
			SRCFILE="$BUILDDIR/copy/$PACKAGE/${FILE[0]}"
			if [ ! -f "$SRCFILE" ]; then
				echo "*** file \"$SRCFILE\" in has not been generated by configure."
				exit 1
			fi
			TARGETDIR="$BUILDDIR/${FILE[1]}"
			TARGETDIR=${TARGETDIR/\%PLATFORM\%/$PLATFORM}
			TARGETDIR=${TARGETDIR/\%PACKAGE\%/$PACKAGE}
			mkdir -p "$TARGETDIR"
			cp "$SRCFILE" "$TARGETDIR"
			TARGETFILE="$TARGETDIR/`basename ${FILE[0]}`"
			if [ ! -f "$TARGETFILE" ]; then
				echo "*** file "$SRCFILE" could not be copied to "$TARGETFILE" (either directory could not be created or copy failed)"
				exit 1
			fi
		done
	else
		echo "could not create temporary copy of \"$SRCDIR/$PACKAGE\" in \"$BUILDDIR/copy/$PACKAGE\""
	fi
	rm -rf "$BUILDDIR/copy/$PACKAGE"
}

if [ x$PLATFORM = x ]; then 
	PLATFORM="$OS-$TARGET-$TOOLCHAIN-$ARCH"
fi
#if [ x$CC = x ]; then
#	CC=${PLATFORM##*-}
#fi
#export CC
if [ x$PYTHON_PATH = x ]; then 
	PYTHON_PATH=python3
fi

if [[ $PLATFORM =~ ^[^-]+-[^-]+-[^-]+-[^-]+$ ]]; then
	PYTHON_VERSION="`$PYTHON_PATH -V 2>&1`"
	if [[ $PYTHON_VERSION =~ ^"Python 2.6."[1-9][0-9]*$|^"Python 2."[7-9][0-9]*(\.[0-9]*(\+)?)?$|^"Python "[3][0-9]*(\.[0-9]*(\.[0-9]*)(\+)?)?$ ]]; then
		FLAGS=`$PYTHON_PATH generate.py --include Makefile.platform.xml --config-dir $BUILDDIR --with-zlib=$EXTERNAL_ZLIB --with-zlib-cppflags="$EXTERNAL_ZLIB_CPPFLAGS" --with-zlib-ldflags="$EXTERNAL_ZLIB_LDFLAGS" --with-libxml=$EXTERNAL_LIBXML --with-libxml-cppflags="$EXTERNAL_LIBXML_CPPFLAGS" --with-libxml-ldflags="$EXTERNAL_LIBXML_LDFLAGS" --print-env $PLATFORM`
		eval $FLAGS
		CONFIG_OPT=""
		if [ x"" != x$HOST ]; then
			CONFIG_OPT="--host=$HOST"
		fi
		execConfigure "." "plib" "$PLIBCONFIG" "$PLIBFILES" "$CONFIG_OPT"
		if [ x"no" == x$EXTERNAL_ZLIB ]; then 
			execConfigure "third_party" "zlib-1.2.5" "$ZLIBCONFIG" "$ZLIBFILES" ""
		fi
		if [ x"no" == x$EXTERNAL_LIBXML ]; then 
			execConfigure "third_party" "libxml2-2.7.7" "$LIBXMLCONFIG" "$LIBXMLFILES" "$CONFIG_OPT"
		fi
		$PYTHON_PATH generate.py --include Makefile.xml --config-dir $BUILDDIR --with-zlib=$EXTERNAL_ZLIB --with-zlib-cppflags="$EXTERNAL_ZLIB_CPPFLAGS" --with-zlib-ldflags="$EXTERNAL_ZLIB_LDFLAGS" --with-libxml=$EXTERNAL_LIBXML --with-libxml-cppflags="$EXTERNAL_LIBXML_CPPFLAGS" --with-libxml-ldflags="$EXTERNAL_LIBXML_LDFLAGS" $PLATFORM
		if [ $? -ne 0 ]; then
			echo "*** error generating Makefile!"
			exit 1
		fi
	else
		echo "Need python version >2.6.1. You have \"$PYTHON_VERSION\"."
	fi
else
	echo "Can not guess PLATFORM quadruple. It is set to \"$PLATFORM\" but it should be something like \"linux-debug-gcc-i386\"."
fi
echo "Configured platform $PLATFORM."
echo "Executables will be generated in \"build/$PLATFORM\"."