#!/bin/bash

#######################################
#### MODIFY THESE SETTINGS, IF DESIRED:
BUILD_VIEW=true
BUILD_PYTHON=true

#CDEBUG="Release"
CDEBUG="Debug"

CPYTHON="On"
#CPYTHON="Off"

CVIEW="On"
#CVIEW="Off"

CLPSOLVE="On"
#CLPSOLVE="Off"
#######################################

# ignore below

export BALL_PATH=`pwd | sed "s/source$//g"`
CDIR=""

export OS=`uname`

download()
{
	if [ -f /usr/bin/curl ]
	then 
		/usr/bin/curl $1 -o $2
	else
		wget $1
	fi
}

askContrib()
{
	cd $BALL_PATH
	echo 
	if [ "${BUILD_VIEW}" = "false" ]; then
		CONTRIB_PACKAGE="Contrib-noView"
		PREFIX="contrib-noview"
	else
		CONTRIB_PACKAGE="Contrib"
		PREFIX="contrib"
	fi
	echo This installer needs the Linux/Unix/MacOS $CONTRIB_PACKAGE package from the 
	echo BALL website: http://www.ballview.org/Downloads/Develop
	echo Have you already downloaded and installed this package?
	echo "(If a previous build of the contrib package failed, choose *No*)"
	OPTIONS="Yes No"
	select INST in $OPTIONS
	do
		if [ "$INST" = "Yes" ]; then
			echo Please enter the position of the contrib directory.
			echo "(Press Return for searching in "$BALL_PATH"/contrib)"
			read CDIR
			if [ "$CDIR" = "" ]; then
				export CDIR="$BALL_PATH/contrib"
			fi
			if [ -d "$CDIR" ]; then
				echo found package directory $CDIR ...
				export CDIR
				return 0
			else
				echo Could not find package directory!
				echo Aborting...
				exit 11
			fi
			else if [ "$INST" = "No" ]; then
				MAJOR_VERSION=`grep ^SET\(BALL_PACKAGE_VERSION_MAJOR "CMakeLists.txt" | sed 's/.*\"\([0-9]*\)\".*/\1/'`
				MINOR_VERSION=`grep ^SET\(BALL_PACKAGE_VERSION_MINOR "CMakeLists.txt" | sed 's/.*\"\([0-9]*\)\".*/\1/'`
				PATCH_VERSION=`grep ^SET\(BALL_PACKAGE_VERSION_PATCH "CMakeLists.txt" | sed 's/.*\"\([0-9]*\)\".*/\1/'`
				export VERSION="${MAJOR_VERSION}.${MINOR_VERSION}.${PATCH_VERSION}"

				echo Assuming version is: $VERSION ....
				if [ "$OS" != Darwin ]
				then
					PACKAGE=${PREFIX}"-"${VERSION}".tar.gz"
				else
					PACKAGE=${PREFIX}"-"${VERSION}"-darwin.tar.gz"
				fi
				echo Trying to download the current Contrib file: $PACKAGE
				URL=http://www.ballview.org/Downloads/Contrib/$PACKAGE
				echo from $URL ...
				echo
				rm -f $PACKAGE 
				download $URL $PACKAGE
				rm -rf contrib
				gunzip $PACKAGE 
				tar xf `basename $PACKAGE .gz`
				# rm $PACKAGE
				export CDIR="$BALL_PATH/contrib"
				cd $CDIR
				export BALL_BUILD_CONTRIB_CLEANUP="true"
				./build_contrib
				return $?
			fi
		fi
	done
}


automaticInstaller()
{
	echo 
	echo Running CMake ...
	cd $BALL_PATH
	mkdir build
	cd build
	touch config.lic
	if [ "${BUILD_VIEW}" = "true" ]; then
		CVIEW="On"
	else
		CVIEW="Off"
	fi

	cmake .. -DBALL_RELEASE_TYPE=$CDEBUG\
	         -DBALL_HAS_VIEW=$CVIEW\
	         -DBALL_PYTHON_SUPPOR=$CPYTHON\
	         -DUSE_LPSOLVE=$CLPSOLVE\
	         -DBALL_CONTRIB_PATH=$CDIR\
	         -DUSE_FIND_SIP_PY=Off\
	         -DSIP_EXECUTABLE="$CDIR/bin/sip"\
	         -DSIP_LIBRARY="$CDIR/lib/sip.so"\
	         -DSIP_INCLUDE_DIR="$CDIR/include" || exit
	make || exit
	cd $BALL_PATH

	LPATH="$BALL_PATH/build/lib/"

	LPATHN="LD_LIBRARY_PATH"
	if [ "$OS" == Darwin ] ; then
		LPATHN=DYLD_LIBRARY_PATH
	fi

	if test ${BUILD_VIEW} = true ; then
		SFILE="$BALL_PATH/build/bin/startBALLView"
		echo "#!/bin/bash" > $SFILE
		echo "PYTHONPATH=$LPATH:$CDIR/lib" >> $SFILE
		echo "export PYTHONPATH" >> $SFILE
		echo "BALL_DATA_PATH=$BALL_PATH/data" >> $SFILE
		echo "export BALL_DATA_PATH" >> $SFILE
		echo "$LPATHN=$LPATH" >> $SFILE
		echo "export $LPATHN" >> $SFILE
		echo "__GL_FSAA_MODE=6" >> $SFILE
		echo "export __GL_FSAA_MODE=6" >> $SFILE
		echo "export LANG=C" >> $SFILE
		echo "$BALL_PATH/build/bin/BALLView" >> $SFILE
		chmod +x $SFILE

		echo
		echo To start BALLView, just run $SFILE
		echo
	fi

	echo Build finished successfully!
}

customInstaller()
{
	cd $BALL_PATH
	echo
	echo Do you want to include visualization support in the library? This
	echo requires a functional OpenGL installation and will allow to build
	echo BALLView.
	echo
	OPTIONS="Yes No"
	select WITH_VIEW in $OPTIONS; do
		if [ "${WITH_VIEW}" = "Yes" ]; then
			export BUILD_VIEW=true
		elif [ "${WITH_VIEW}" = "No" ]; then
			export BUILD_VIEW=false
		fi
		break
	done
	echo ${BUILD_VIEW}
	askContrib 
	if test $? != 0; then
		echo "Autobuild aborted."
		exit
	fi
	BINDIR=$CDIR/bin
	INCDIR=$CDIR/include
	LIBDIR=$CDIR/lib
	automaticInstaller
}

if [ $# != 0 ] 
then
	if [ $1 == "--help" ]
	then
		echo Usage: either without any arguments or with the
		echo position of the contrib directory.
		exit 0
	fi

	CDIR=$1
	if [ ! -d $CDIR ]
	then 
		echo Could not find contrib directory $CDIR
		echo Aborting ...
		echo
		exit 22
	fi
	automaticInstaller
	exit $0
else
	echo
	echo This is the automatic BALL installer, using this script is
	echo only allowed if you have read and accepted the BALL licence
	echo file under BALL/source/.licence.
	echo 
	echo Please note that the build process of the contrib packages
	echo requires about 2Gb of disk space and can take several hours,
	echo depending on your machine. This is mostly due to compiling
	echo Qt. After compiling the contrib packages, you will be offered
	echo to clean object files and source directories, which will reduce
	echo the required disk space to ~250Mb.
	echo
	echo Furthermore, make sure that you have the following packages 
	echo installed on your machine:
	echo bison, flex, python, python-dev, libXmu-dev, libXi-dev, 
	echo libglu-dev, libx11-dev, autoconf, doxygen, tidy, dvipdf, latex, 
	echo wget *or* curl
	echo
	echo To proceed, please answer the following questions:
	echo "(Control-C will abort the installer.)"
	echo
	echo Which kind of installation do you want?
	OPTIONS="Automatic Custom"
	select INST in $OPTIONS; do
		if [ "$INST" = "Automatic" ]; then
			askContrib
			if test $? != 0; then
				echo "Autobuild aborted."
				exit
			fi
			BINDIR=$CDIR/bin
			INCDIR=$CDIR/include
			LIBDIR=$CDIR/lib
			automaticInstaller
		elif [ "$INST" = "Custom" ]; then
			customInstaller
		fi
		exit
	done
fi


