#!/bin/sh

# simple configure script for user-friendliness

# file to put output into for cmake to read in...
SRC_DIR=$(dirname $0)
SRC_DIR=`cd $SRC_DIR && pwd`
if test "$SRC_DIR" = "." ; then
	SRC_DIR=$(pwd)
fi
MAKEFILE=$SRC_DIR/"Makefile.cmake"
OUTFILE=Makefile.cmake.in
CMAKEOUTFILE=$SRC_DIR/CMakeOptions.txt

# --- FUNCTIONS ---

usage()
{
echo "

Hi there.  You can use this script to configure parameters used by cmake.
Currently, understood parameters are as follows:

  --prefix=PREFIX         install architecture-independent files in PREFIX
  --enable-tests=ARG      enable test suite (yes|no) [default=no]
  --with-pilot-link=PATH  set prefix for pilot-link files [default=check]
  --with-mal=PATH         set path for libmal files [default=check]

  --show                  show existing configuration values

More obscure options:

  --with-simple-builddir=ARG      use 'build' instead of longer name (yes|no)
  --with-pilot-link-includes=PATH set include directory for pilot-link
  --with-pilot-link-lib=PATH      set full path to libpisock.so

"
}

getvalue()
{
	KEY="$1"
	# use dynamic variable...
	eval VAL='$'$KEY

	ECHO="$2"
	
	if test -n "$VAL"
	then
		CMAKE_FLAGS="${CMAKE_FLAGS}set(${KEY} \"${VAL}\")
"
		if [ "$ECHO" = "y" ]
		then
			echo "$KEY=\"$VAL\""
		fi
	fi

}

outputCmakeValues()
{

# only include what we're passed
CMAKE_FLAGS=""

getvalue CMAKE_INSTALL_PREFIX n
getvalue KDE4_BUILD_TESTS n
getvalue PILOTLINK_BASE n
getvalue MAL_BASE n
getvalue PILOTLINK_INCLUDE_DIR n
getvalue PILOTLINK_LIBRARY n

echo "$CMAKE_FLAGS"
}

outputMakeValues()
{
getvalue SRC_DIR y
getvalue BUILD_DIR y
}

# --- MAIN ---

# first, if there's no args, don't lose what we had stored (badness).
# simply show what available arguments are and exit...
if test -z "$1"; then
	usage
	exit
fi

ENABLE_TESTS="NO"
BUILD_DIR=build-`uname -sr | tr -d [:space:] | tr -Cs a-zA-Z0-9 _`

while test -n "$1"
do
	case "$1" in
		--prefix=*)
			CMAKE_INSTALL_PREFIX=$(echo $1 | cut -d "=" -f2)
			;;
		--enable-test*)
			T=$(echo "$1" | cut -d = -f2 | tr '[A-Z]' '[a-z]')
			if test "$T" = "$1" || test "yes" = "$T" ; then
				KDE4_BUILD_TESTS=ON
			else
				KDE4_BUILD_TESTS=OFF
			fi
			;;
		--with-simple-builddir*)
			T=$(echo "$1" | cut -d = -f2 | tr '[A-Z]' '[a-z]')
			if test "$T" = "$1" || test "yes" = "$T" ; then
				BUILD_DIR=build
			fi
			;;
		--with-pilot-link-includes=*)
			PILOTLINK_INCLUDE_DIR=$(echo $1 | cut -d = -f2)
			;;
		--with-pilot-link-lib=*)
			PILOTLINK_LIBRARY=$(echo $1 | cut -d = -f2)
			;;
		--with-pilot-link=*)
			PILOTLINK_BASE=$(echo $1 | cut -d "=" -f2)
			;;
		--with-mal=*)
			MAL_BASE=$(echo $1 | cut -d "=" -f2)
			;;
		--show)
			echo "Existing configuration values:"
			echo "-----------"
			cat "$OUTFILE" 2>/dev/null
			sed 's/^set(\([A-Z_]*\) "\(.*\)")/\1="\2"/' "$CMAKEOUTFILE" 2>/dev/null
			echo "-----------"
			exit
			;;
		--)
			:
			;;
		*)
			usage
			exit
			;;
	esac

	shift

done

###
#
# BSD uses gmake for the GNU make which we need ...
#
if uname -s | grep BSD > /dev/null 2>&1 ; then
	MAKE=gmake
else
	MAKE=make
fi

outputCmakeValues > "$CMAKEOUTFILE.new"
outputMakeValues > "$OUTFILE.new"


###
#
# If the configure values have changed, then we should update the 
# CMakeLists.txt in order to prompt a re-run of cmake.
#
update=no
failed=no
if test -f "$CMAKEOUTFILE" ; then
	diff -q "$CMAKEOUTFILE" "$CMAKEOUTFILE.new" > /dev/null 2>&1 || update=yes
else
	update=yes
fi

if test -f "$OUTFILE" ; then
	diff -q "$OUTFILE" "$OUTFILE.new" > /dev/null 2>&1 || update=yes
else
	update=yes
fi

if test yes = "$update" ; then
	cp "$CMAKEOUTFILE.new" "$CMAKEOUTFILE"
	cp "$OUTFILE.new" "$OUTFILE"
	touch CMakeLists.txt
fi

rm -f "$CMAKEOUTFILE.new"
rm -f "$OUTFILE.new"

###
#
# Inform user and create settings file.
#
if test "yes" = "$failed" ; then
	echo "Configuration failed, so take a good look at the build output."
else
echo "
Thanks.  Here are the values I will be using...

$(outputCmakeValues)

$(outputMakeValues)

To compile KPilot, now run GNU make, like so:

    $MAKE -f $MAKEFILE

"
fi

if ! test -f Makefile ; then
	( for target in all install clean
	do
		echo "$target:" 
		echo "	$MAKE -f $MAKEFILE $target"
	done 
	) > Makefile
fi

