#!/usr/bin/env sh

#####################################################
# Jahshaka configuration script
# sets up for either jahshaka or jahplayer build

help=false
debug=true
prefix=/usr/local
static=false
plugins=true
openalframework=false
BUILDTYPE=jahshaka
CONFIGURED=false

# Parse the args
for i in "$@"
do
	case $i in
		--help )				help=true ;;
		--prefix=* )			prefix="${i#--prefix=}" ;;
		--disable-debug )		debug=false ;;
		--enable-static )		static=true ;;
		--disable-plugins )		plugins=false ;;
		--enable-openalframework )	openalframework=true ;;
		jahplayer | jahshaka )	BUILDTYPE=$i ;;
		* )						echo "Unrecognised argument $i" ;;
	esac
done

# This is included by jahshakaSettings.pro and jahplayerSettings.pro
[ "$help" = "false" ] &&
cat << EOF > config.pro
JAHDEBUG=$debug
JAHSTATIC=$static
JAHPREFIX=$prefix
JAHPLUGINBUILD=$plugins
BUILDTYPE=$BUILDTYPE
OPENALFRAMEWORK=$openalframework
EOF

#####################################################


if [ "$help" = "false" -a "$BUILDTYPE" = "jahshaka" ] 
then
    echo ">>Configuring build type for jahshaka"
    echo
    sed -e "s,config.pro,`pwd`/config.pro,g" jahshakaSettings.pro >Settings.pro
    qmake jahshaka.pro && CONFIGURED="true"
elif [ "$help" = "false" -a "$BUILDTYPE" == "jahplayer" ]
then
    echo ">>Configuring build type for jahplayer"
    echo
    sed -e "s,config.pro,`pwd`/config.pro,g" jahplayerSettings.pro >Settings.pro
    qmake jahshaka.pro && CONFIGURED="true"
fi

if [ "$help" = "true" -o "$CONFIGURED" = "false" ] 
then
    echo "---------------------------------------"
    echo "Jahshaka Configure Script User Commands"
    echo "---------------------------------------"
    echo  
    echo "SYNOPSIS"
	echo
    echo "     ./configure [ switches ]"
    echo "     ./configure [ switches ] jahshaka"
    echo "     ./configure [ switches ] jahplayer"
    echo 
    echo "DESCRIPTION"
	echo
    echo "     Configures the jahshaka build process to build either jahshaka"
	echo "     or jahplayer"
    echo      
    echo "     By default running configure with no arguments will activate the"
	echo "     build for jahshaka"
    echo
    echo "     You need to follow the configure command with the make command"
    echo "     in order to actually build the software!"
    echo      
	echo "SWITCHES"
	echo
	echo "     --prefix=path"
	echo "          sets the path for the installation (default: /usr/local)"
	echo
	echo "     --disable-debug"
	echo "          sets debug mode off"
	echo
	echo "     --disable-plugins"
	echo "          disables the plugin build and install"
	echo
	echo "     --enable-static"
	echo "          disables the dynamic loading of various open libraries"
    echo
else
	echo 
	echo "SUMMARY"
	echo 
	echo "     Configuration complete."
	echo
	echo "     Building         = $BUILDTYPE"
	echo "     Install prefix   = $prefix"
	echo "     Debug            = $debug"
	echo "     Static libs      = $static"
	echo "     Building plugins = $plugins"
	echo "     OpenAL framework = $openalframework"
	echo

	echo "BUILD AND INSTALL"
	echo

	if [ "$plugins" = "true" ]
	then
		echo "     To build the application and plugins, run:"
		echo
		echo "     $ make"
		echo
	else
		echo "     To build the application, run:"
		echo
		echo "     $ make"
		echo
		echo "     To manually compile the plugins, run:"
    	echo
    	echo "     $ cd plugins"
    	echo "     $ ./configure"
    	echo "     $ make"
		echo
	fi
	echo "     To install, run this as your superuser:"
	echo
	echo "     # make install"
	echo
fi

