#! /bin/sh
# vim: expandtab sw=4 ts=4 sts=4:
# Wrapper for cmake to keep minimal compatibility with auto*

# Exit on error and undefined variables
set -e -u

cat <<EOT
Wrapper script for configuring CMake for Gammu.

This provides limited compatibility with configure, if you want full
configuration control, use directly CMake. More information about CMake
is available at <http://www.cmake.org>.

EOT

for cmd in cmake readlink dirname ; do
    if ! type $cmd > /dev/null 2>&1 ; then
        echo "ERROR: $cmd not found, please install it, it is required for build."
        exit 1
    fi
done

help() {
    cat <<EOT
Usage: ./configure [options]

--help|-h           shows this help
--prefix=<path>     installation prefix
--enable-shared     enables shared build
--enable-debug      enables debug build
--enable-tiger      enables Mac OS X 10.4 (Tiger) build
--enable-backup     enable backup support
--enable-win32      enable mingw crosscomilation
--enable-protection enable compile time protections
--without-gnapplet  disable installation of gnapplet
--without-completion disable installation of bash completion script

All enable params have their disable counterparts.

EOT
    exit 2
}

# directory where sources are located
SOURCE_DIR="$(dirname $(readlink -f $0))"

# directory where we will build
BUILD_DIR="$SOURCE_DIR/build-configure"

# cmake parameters
CMAKE_PREFIX=
CMAKE_SHARED=
CMAKE_DEBUG=
CMAKE_BACKUP=
CMAKE_CROSS=
CMAKE_PROTECTION=
CMAKE_GNAP=
CMAKE_COMPLETE=

# process command line
while [ "$#" -gt 0 ] ; do
    case "$1" in
        --help|-h)
            help
            ;;
        --prefix=*)
            CMAKE_PREFIX="-DCMAKE_INSTALL_PREFIX=${1##--prefix=}"
            ;;
        --enable-backup)
            CMAKE_BACKUP="-DWITH_BACKUP=ON"
            ;;
        --disable-backup)
            CMAKE_BACKUP="-DWITH_BACKUP=OFF"
            ;;
        --enable-win32)
            CMAKE_CROSS="-DCROSS_MINGW=ON"
            ;;
        --disable-win32)
            CMAKE_CROSS="-DCROSS_MINGW=OFF"
            ;;
        --enable-shared)
            CMAKE_SHARED="-DBUILD_SHARED_LIBS=ON"
            ;;
        --disable-shared)
            CMAKE_SHARED="-DBUILD_SHARED_LIBS=OFF"
            ;;
        --enable-protection)
            CMAKE_PROTECTION="-DENABLE_PROTECTION=ON"
            ;;
        --disable-protection)
            CMAKE_PROTECTION="-DENABLE_PROTECTION=OFF"
            ;;
        --enable-debug)
            CMAKE_DEBUG="-DCMAKE_BUILD_TYPE=Debug"
            ;;
        --disable-debug)
            CMAKE_DEBUG=
            ;;
        --without-gnapplet)
            CMAKE_GNAP="-DINSTALL_GNAPPLET=OFF"
            ;;
        --without-completion)
            CMAKE_COMPLETE="-DINSTALL_BASH_COMPLETION=OFF"
            ;;
        --build=*)
            ;;
        --disable-dependency-tracking)
            ;;
        --disable-maintainer-mode)
            ;;
        --includedir=*)
            ;;
        --mandir=*)
            ;;
        --infodir=*)
            ;;
        --sysconfdir=*)
            ;;
        --localstatedir=*)
            ;;
        --libexecdir=*)
            ;;
        *)
            echo "Unknown parameter: $1"
            echo
            help
            ;;
    esac
    shift
done

# create build dir if needed
if [ ! -d "$BUILD_DIR" ] ; then
    mkdir -p "$BUILD_DIR"
fi

# go to build dir
cd "$BUILD_DIR"

# invoke cmake to do configuration
cmake $SOURCE_DIR $CMAKE_PREFIX $CMAKE_SHARED $CMAKE_DEBUG $CMAKE_BACKUP $CMAKE_CROSS $CMAKE_PROTECTION $CMAKE_GNAP $CMAKE_COMPLETE
