#!/bin/sh

OPT_HELP=

# parse arguments
while [ "$#" -gt 0 ]; do
    case $1 in
    -help)
	OPT_HELP=yes
	shift
	;;
    *)
	echo "${1}: unknown argument. Use -help."
	exit 1
	;;
    esac
done

if [ "x$OPT_HELP" = "xyes" ]; then
    cat <<EOF
Usage: configure [-help]
EOF
   exit 0
fi

if [ -z $QTDIR ]
then
    echo "Can't find Qt library. No QTDIR set."
    exit 1
else
    echo "Using Qt library in $QTDIR."
fi

QMAKE_CACHE=${QTDIR}/.qmake.cache
if [ -f ${QMAKE_CACHE} ]; then
    rm -f .qmake.cache
    ln -s ${QMAKE_CACHE} .qmake.cache
else
    echo Trying my best without a .qmake.cache file
fi

# build 2nd stage configure tool
echo "Building 2nd stage configure"
cd configure2
qmake && make > /dev/null
if [ $? -gt 0 -o ! -x configure2 ]; then
    exit 1
fi

# invoke 2nd stage
cd ..
configure2/configure2 || exit 1

find . -name Makefile -exec rm {} \;
qmake examples/examples.pro
qmake src/src.pro
qmake qsa.pro

# echo "Libraries will be installed in $QTDIR/lib"
