#! /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

if ! type cmake > /dev/null 2>&1 ; then
    echo 'ERROR: CMake not found, please install it, it is required for build.'
    exit 1
fi

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-backup     enable backup support
--enable-win32      enable mingw crosscomilation
--enable-protection enable compile time protections

All enable params have their disable counterparts.

EOT
    exit 2
}

# directory where we will build
BUILD_DIR=build-configure

# directory where sources are located
SOURCE_DIR=`pwd`

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

# process command line
while [ "$#" -gt 0 ] ; do
    case "$1" in
        --help|-h)
            help
            ;;
        --prefix=*)
            CMAKE_PREFIX="-DCMAKE_INSTALL_PREFIX=`echo $1|sed 's/^--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="-DENABLE_SHARED=ON"
            ;;
        --disable-shared)
            CMAKE_SHARED="-DENABLE_SHARED=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=
            ;;
        *)
            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
