#!/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.
  --docdir=[path]	Documentation path. Default: prefix/doc
  --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
			;;

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

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

echo "Configuring QtCommander ..."

if [ "$QC_DEBUG" = "Y" ]; then
echo
echo PREFIX=$PREFIX
echo QTDIR=$QTDIR
echo
fi

printf "Verifying Qt 3.x Multithreaded (MT) build environment ... "

if [ -z "$QTDIR" ]; then
	if [ "$QC_DEBUG" = "Y" ]; then
		echo \$QTDIR not set... trying to find Qt manually
	fi
	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
	if [ "$QC_DEBUG" = "Y" ]; then
		echo Warning: qmake not in \$QTDIR/bin/qmake
		echo trying to find it in \$PATH
	fi
	qm=`type -p qmake`
	if [ -x "$qm" ]; then
		if [ "$QC_DEBUG" = "Y" ]; then
			echo qmake found in $qm
		fi
	else
		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
EOF

#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"
echo "Now run 'make'"
echo
