#!/bin/sh

# save environment variable
ORIG_QTDIR=$QTDIR
 
show_usage() {

cat <<EOT
Usage: ./configure [OPTION]...
This script creates Makefiles and some configuration files necessary
to compile psi.

Available options:
Mandatory arguments to long options are mandatory for short options too.
  -p, --prefix=PREFIX       PREFIX will be added to all configuration
                            paths. Default: /usr/local
  -b, --bindir=BINDIR       directory for binaries. Default: PREFIX/bin
  -l, --libdir=LIBDIR       directory for libraries. Default: PREFIX/share/psi
  -q, --qtdir=QTDIR         directory where Qt is installed.
                            Default: autodetected
  -h, --help                this help text

This is just a simple script and doesn't support all the options
autoconf knows.

EOT

}

while [ $# -gt 0 ]; do
	case "$1" in
		-p|--prefix)
			if [ $# -lt 2 ]; then
				show_usage
				exit
			fi
			PREFIX=$2
			shift 2
			;;

		--prefix=*)
			PREFIX=`expr "${1}" : "--prefix=\(.*\)"`
			shift
			;;

		-b|--bindir)
			if [ $# -lt 2 ]; then
				show_usage
				exit
			fi
			BINDIR=$2
			shift 2
			;;

		--bindir=*)
			BINDIR=`expr "${1}" : "--bindir=\(.*\)"`
			shift
			;;

		-l|--libdir)
			if [ $# -lt 2 ]; then
				show_usage
				exit
			fi
			LIBDIR=$2
			shift 2
			;;

		--libdir=*)
			LIBDIR=`expr "${1}" : "--libdir=\(.*\)"`
			shift
			;;

		-q|--qtdir)
			if [ $# -lt 2 ]; then
				show_usage
				exit
			fi
			QTDIR=$2
			shift 2
			;;

		--qtdir=*)
			QTDIR=`expr "${1}" : "--qtdir=\(.*\)"`
			shift
			;;

		-h|--help) show_usage; exit ;;
		*) show_usage; exit ;;
	esac
done

echo Configuring Psi ...

PREFIX=${PREFIX:-/usr/local}
BINDIR=${BINDIR:-$PREFIX/bin}
LIBDIR=${LIBDIR:-$PREFIX/share/psi}

if [ -z "$QTDIR" ]; then
	echo 
	echo \$QTDIR not set... trying to find qt manually
	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 ; do
		if [ -d "$p/mkspecs" ]; then
			QTDIR=$p
			break;
		fi;
	done
	if [ -z "$QTDIR" ]; then
		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
		exit 1;
	fi
fi 

if [ ! -x "$QTDIR/bin/qmake" ]; then
	echo Warining: qmake not in \$QTDIR/bin/qmake
	echo trying to find it in \$PATH
	qm=`type -p qmake`
	if [ -x "$qm" ]; then
		echo qmake found in $qm
	else 
		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.
		exit 1;
	fi
else
	qm=$QTDIR/bin/qmake
fi

# check for /usr/X11R6/include/zlib.h
if [ -f "/usr/X11R6/include/zlib.h" -a -f "/usr/include/zlib.h" ]; then
       ADD_USR_INCLUDE="       INCLUDEPATH  += . /usr/include -"
else
       ADD_USR_INCLUDE=""
fi

(
	cd conf
	$qm qconf.pro >/dev/null
	QTDIR=$QTDIR make clean >/dev/null 2>&1
	QTDIR=$QTDIR make >/dev/null 2>&1
)

if [ "$?" != "0" ]; then
	echo There was an error compiling qconf.  Be sure you have a proper
	echo Qt 3.x build environment set up.
	if [ ! -f "$QTDIR/lib/libqt-mt.so.3" ]; then
		echo
		echo "One possible reason is that you don't have"
		echo "libqt-mt.so.3 installed in $QTDIR/lib/."
	fi
	exit 1;
fi

(
	cd conf
	./qconf
)

if [ "$?" != "0" ]; then
	exit 1;
fi

cat >configure.pri <<EOT
DEFINES += HAVE_CONFIG

libfiles.path=$LIBDIR
libfiles.files=image iconsets sound certs README COPYING

target.path=$BINDIR

INSTALLS = libfiles target

!isEmpty(KDE) {
	# desktop icon
	KDESHARE = \$\$KDE/share
	dt.path=\$\$KDE/share/applnk/Internet/
	dt.extra = cp -f -p psi.desktop \$(INSTALL_ROOT)\$\$dt.path

	# mandrake
	exists(\$\$KDESHARE/applnk-mdk) {
		dt.extra += ; cp -f -p psi.desktop '\$(INSTALL_ROOT)\$\$KDESHARE/applnk-mdk/Networking/Instant messaging'
	}
	exists(\$\$KDESHARE/applnk-mdk-simplified) {
		dt.extra += ; cp -f -p psi.desktop '\$(INSTALL_ROOT)\$\$KDESHARE/applnk-mdk-simplified/Use the Internet'
	}

	# icon files
	icon1.path=\$\$KDE/share/icons/hicolor/16x16/apps
	icon1.extra = cp -f -p image/icon_16.png \$(INSTALL_ROOT)\$\$icon1.path/psi.png
	icon2.path=\$\$KDE/share/icons/hicolor/32x32/apps
	icon2.extra = cp -f -p image/icon_32.png \$(INSTALL_ROOT)\$\$icon2.path/psi.png
	icon3.path=\$\$KDE/share/icons/hicolor/48x48/apps
	icon3.extra = cp -f -p image/icon_48.png \$(INSTALL_ROOT)\$\$icon3.path/psi.png
	INSTALLS += dt icon1 icon2 icon3
}

$ADD_USR_INCLUDE
EOT

cat >src/config.h <<EOT
#define LIBDIR "$LIBDIR"
EOT
# run qmake
$qm psi.pro

echo
if [ "$QTDIR" != "$ORIG_QTDIR" ]; then 
	echo Good, your configure finished.  Now run \'QTDIR=$QTDIR make\'.
else
	echo Good, your configure finished.  Now run \'make\'.
fi
echo

