#!/bin/sh
#
# Configures to build the project
#

#-------------------------------------------------------------------------------
# script initialization
#-------------------------------------------------------------------------------

# Making releases:
#   set the new version number:
#     odd minor -> development series
#     even minor -> stable series
#     increment micro for each release within a series
#   set nano_version to 0
#   update debian/changelog, use dch
#   make the release, tag it
#   set nano_version to 1

if [ -z $VERSION ]; then
    MAJOR_VERSION=1
    MINOR_VERSION=1
    MICRO_VERSION=1
    NANO_VERSION=
    if [ -z $NANO_VERSION ] || [ $NANO_VERSION -eq "0" ]; then
        VERSION="$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION"
    else
        VERSION="$MAJOR_VERSION.$MINOR_VERSION.$MICRO_VERSION.$NANO_VERSION"
    fi
fi

# Qt Version
QMAKE=`which qmake 2>/dev/null`

if [ -z "$QMAKE" ] ; then
	QMAKE=`which qmake-qt4 2>/dev/null`
fi

if [ -z "$QMAKE" ] ; then
	echo
	echo "You need qmake in your PATH to build contactsd"
	echo "Cannot proceed."
	exit 1
fi

export QMAKE

QT_VERSION=`$QMAKE -query QT_VERSION`
if [ -z "$QT_VERSION" ] ; then
	echo
	echo "Cannot proceed without QT_VERSION determined."
	exit 1
fi

relpath=`dirname $0`
relpath=`(cd "$relpath"; /bin/pwd)`
outpath=`/bin/pwd`

PREFIX=/usr
LOCALSTATEDIR=/var

TOP_SOURCEDIR=$relpath
TOP_BUILDDIR=$outpath

while [ "$#" -gt 0 ]; do
    case "$1" in
    -h|--help)
        echo "Usage: ./configure [OPTION]..."
        echo
        echo "Configuration:"
        echo "    -h, --help              display this help and exit"
        echo
        echo "Installation directories:"
        echo "    --prefix PREFIX         install everything relative to PREFIX"
        echo "                            [/usr]"
        echo
        echo "Fine tuning of the installation directories:"
        echo "    --bindir DIR            user executables [PREFIX/bin]"
        echo "    --libdir DIR            object code libraries [PREFIX/lib]"
        echo "    --includedir DIR        C header files [PREFIX/include]"
        echo "    --localstatedir DIR     modifiable single-machine data [/var]"
        echo
        exit
        ;;
    --prefix)
        shift
        PREFIX=$1
        ;;
    --bindir)
        shift
        BINDIR=$1
        ;;
    --libdir)
        shift
        LIBDIR=$1
        ;;
    --includedir)
        shift
        INCLUDEDIR=$1
        ;;
    --localstatedir)
        shift
        LOCALSTATEDIR=$1
        ;;
    --enable-coverage)
        ENABLE_COVERAGE=coverage ;;
    --disable-coverage)
        ENABLE_COVERAGE=nocoverage ;;
    *)
        echo >&2 "configure: error: unrecognized option: '$1'"
        echo >&2 "Try './configure --help' for more information."
        exit
        ;;
    esac
    shift
done

[ -z $BINDIR ] && BINDIR=$PREFIX/bin
[ -z $LIBDIR ] && LIBDIR=$PREFIX/lib
[ -z $INCLUDEDIR ] && INCLUDEDIR=$PREFIX/include

#coverage options

if [ -z "$ENABLE_COVERAGE" ]
then
    echo "Coverage not enabled"
    ENABLE_COVERAGE=nocoverage
fi

# save configuration into .qmake.cache
CACHEFILE="$outpath/.qmake.cache"
[ -f "$CACHEFILE" ] && rm -f "$CACHEFILE"

cat >> "$CACHEFILE" << EOF
CONFIGURED = \$\$quote(yes)
VERSION = \$\$quote($VERSION)
PREFIX = \$\$quote($PREFIX)
BINDIR = \$\$quote($BINDIR)
LIBDIR = \$\$quote($LIBDIR)
INCLUDEDIR = \$\$quote($INCLUDEDIR)
LOCALSTATEDIR = \$\$quote($LOCALSTATEDIR)
TOP_SOURCEDIR = \$\$quote($TOP_SOURCEDIR)
TOP_BUILDDIR = \$\$quote($TOP_BUILDDIR)
CONFIG += \$\$quote($ENABLE_COVERAGE)
EOF

infiles="contactsd-1.0.pc tests/mktests.sh"

for infile in $infiles; do
    echo "Generating $infile"
    # pre-process .in files
    mkdir -p `dirname $infile`
    cat $TOP_SOURCEDIR/$infile.in |
        sed -e "s,@VERSION@,${VERSION},g" \
            -e "s,@PREFIX@,${PREFIX},g" \
            -e "s,@BINDIR@,${BINDIR},g" \
            -e "s,@LIBDIR@,${LIBDIR},g" \
            -e "s,@INCLUDEDIR@,${INCLUDEDIR},g" \
            -e "s,@LOCALSTATEDIR@,${LOCALSTATEDIR},g" \
            -e "s,@TOP_SOURCEDIR@,${TOP_SOURCEDIR},g" \
            -e "s,@TOP_BUILDDIR@,${TOP_BUILDDIR},g" \
        > $TOP_BUILDDIR/$infile
done

echo
echo "contactsd is now configured for building. Now run 'qmake' and then 'make'."
echo "Once everything is built, you must run 'make install'."
echo "contactsd will be installed into $PREFIX"
echo
echo "To reconfigure, run 'make confclean' and 'configure'."
