#!/bin/sh
#
# Authors:	Qt Cryptographic Architecture library developers 
#		(http://delta.affinix.com/qca/)
#
# Modified by: 	Mariusz Borowski <b0mar@nes.pl>
# 

show_usage() {
cat <<EOT
Usage: ./configure [OPTION]...

This script creates necessary configuration files to build/install.

Main options:
  --prefix=[path]    	Base path for build/install.  Default: /usr/local
  --with-qt-dir=[path]  Directory where Qt is installed.
  --enable-pch		Enable support for precompiled headers
  --docdir=[path]	Documentation path. Default: prefix/doc
  --debug		Produce a debugging version of the application.
  --help             	This help text.

EOT
}

while [ $# -gt 0 ]; do
	case "$1" in
		--prefix=*)
			PREFIX=`expr "${1}" : "--prefix=\(.*\)"`
			shift
			;;

		--with-qt-dir=*)
			QTDIR=`expr "${1}" : "--with-qt-dir=\(.*\)"`
			shift
			;;
			
		--enable-pch)
			PCH_OPT=precompile_header
			shift
			;;

		--docdir=*)
			DOCDIR=`expr "${1}" : "--docdir=\(.*\)"`
			shift
			;;
			
		--debug)
			QT_CONFIG="debug"
			QT_CONFIG="debug"
			shift
			;;
		--help) show_usage; exit ;;
		*) show_usage; exit ;;
	esac
done

PREFIX=${PREFIX:-/usr/local}
DOCDIR=${DOCDIR:-$PREFIX/doc}
QT_CONFIG=${QT_CONFIG:-release}
APP_VERSION=`cat VERSION`

echo "Configuring QtCommander ..."
echo -n "Verifying Qt 3.x Multithreaded (MT) build environment ... "

if [ -z "$QTDIR" ]; then
	for p in /usr/lib/qt /usr/share/qt /usr/share/qt3 /usr/local/lib/qt /usr/local/share/qt /usr/lib/qt3 /usr/local/lib/qt3 /usr/X11R6/share/qt /usr/qt/3 ; do
		if [ -d "$p/mkspecs" ]; then
			QTDIR=$p
			break;
		fi;
	done
	if [ -z "$QTDIR" ]; then
		echo fail
		echo
		echo Unable to find Qt 'mkspecs'. Please set QTDIR
		echo manually. Perhaps you need to install Qt 3
		echo development utilities. You may download them either
		echo from the vendor of your operating system or from
		echo www.trolltech.com
		echo
		exit 1;
	fi
fi

if [ ! -x "$QTDIR/bin/qmake" ]; then
	qm=`type -p qmake`
	if [ ! -x "$qm" ]; then
		echo fail
		echo
		echo Sorry, you seem to have a very unusual setup,
		echo or I missdetected \$QTDIR=$QTDIR
		echo
		echo Please set \$QTDIR manually and make sure that
		echo \$QTDIR/bin/qmake exists.
		echo
		exit 1;
	fi
else
	qm=$QTDIR/bin/qmake
fi

echo OK

export QTDIR
cat > .qmake.cache <<EOF
PREFIX=$PREFIX
DOCDIR=$DOCDIR
APP_VERSION=$APP_VERSION
CONFIG=$QT_CONFIG $PCH_OPT
QT_BIN_DIR=$QTDIR/bin
EOF

if [ -n "$PCH_OPT" ] ; then
    echo "DEFINES+=USING_PCH" >> .qmake.cache
fi


#create Makefiles
for i in `find . -type f -name "*.pro"`; do
    mkf=$(dirname $i)/Makefile
    echo "Create ${mkf}..."
    $qm $i -o $mkf
done

echo
echo "Good, your configure finished."
echo "Base path for install:		$PREFIX"
echo "Documentation install path:	$DOCDIR"
if [ -n "$PCH_OPT" ] ; then
    echo "Support for precompiled headers: enabled"
fi
echo "Now run 'make'"
echo
