#!/bin/bash

echo "Orocos configure script.  WARNING: ONLY WORKS FOR GNULINUX INSTALLATIONS"

function usage
{
   echo "This script allows you to set the compiler and CFLAGS/LDFLAGS."
   echo "Usage : ../configure [--prefix=/usr/local] [--with-target=/target/path] [CC=compiler] [CXX=compiler]"
   echo
   echo "Supported --with-target's: --with-xenomai --with-lxrt --with-gnulinux"
   echo
   echo "Options:"
   echo "   --enable-corba      Enable CORBA, if installed on the system."
   echo "   --disable-shared    Build static libraries instead of shared libraries."
   echo "   --embedded          Disable Scripting, Exceptions and reduce the type system."
   echo "   --with-linux=/path  Provide a Linux path for RTAI/LXRT or old Xenomai versions."
   echo
   echo "Example: CXXFLAGS='-g' $0 --prefix=/usr/local/orocos --with-xenomai=/usr/xenomai CC=gcc-3.3.6 CXX=g++-3.3.6"
   echo
   echo "For more advanced configuration, use the 'ccmake' utility directly."
}

function getoptions
{
    arg="$1"
    if [ $(expr match "$arg" "CC=") != 0 ]; then
	CMAKE_CC="$arg"
    fi

    if [ $(expr match "$arg" "CXX=") != 0 ]; then
	CMAKE_CXX="$arg"
    fi

    if [ $(expr match "$arg" "--prefix=") != 0 ]; then
	CMAKE_PREFIX="${arg:9}"
	CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_INSTALL_PREFIX=$CMAKE_PREFIX"
    fi
    if [ $(expr match "$arg" "--with-xenomai") != 0 ]; then
        CMAKE_XENO_PATH="${arg:15}"
	CMAKE_ARGS="$CMAKE_ARGS -DOROCOS_TARGET=xenomai"
	if [ x$CMAKE_XENO_PATH != x ]; then
	    CMAKE_ARGS="$CMAKE_ARGS -DXENOMAI_INSTALL_DIR=$CMAKE_XENO_PATH"
	fi   
    fi
    if [ $(expr match "$arg" "--with-lxrt") != 0 ]; then
	CMAKE_LXRT_PATH="${arg:12}"
	CMAKE_ARGS="$CMAKE_ARGS -DOROCOS_TARGET=lxrt"
	if [ x$CMAKE_LXRT_PATH != x ]; then
	    CMAKE_ARGS="$CMAKE_ARGS -DRTAI_INSTALL_DIR=$CMAKE_LXRT_PATH"
	fi
    fi
    if [ $(expr match "$arg" "--with-linux") != 0 ]; then
	CMAKE_LINUX_PATH="${arg:13}"
	if [ x$CMAKE_LINUX_PATH != x ]; then
	    CMAKE_ARGS="$CMAKE_ARGS -DLINUX_SOURCE_DIR=$CMAKE_LINUX_PATH"
#       else
#           CMAKE_ARGS="$CMAKE_ARGS -DLINUX_SOURCE_DIR=/usr/include"
	fi
    fi   
    if [ $(expr match "$arg" "--enable-corba") != 0 ]; then
	CMAKE_ARGS="$CMAKE_ARGS -DENABLE_CORBA=TRUE"
    fi
    if [ $(expr match "$arg" "--disable-shared") != 0 ]; then
	CMAKE_ARGS="$CMAKE_ARGS -DBUILD_STATIC=ON"
    fi
    if [ $(expr match "$arg" "--enable-static") != 0 ]; then
	CMAKE_ARGS="$CMAKE_ARGS -DBUILD_STATIC=ON"
    fi
    if [ $(expr match "$arg" "--embedded") != 0 ]; then
	CMAKE_ARGS="$CMAKE_ARGS -DOS_EMBEDDED=ON"
    fi
}

while [ "$1" != "" ]; do
    case $1 in
        -h | --help )           usage
                                exit
                                ;;
        * )                     getoptions $1
    esac
    shift
done

# if [ -f ./configure ]; then
#    echo "Error: run configure from a 'build' directory:"
#    echo " $ mkdir build"
#    echo " $ cd build"
#    echo " $ ../configure [options]"
#    exit 1
# fi

src_dir="$(dirname $0)"

if [ -f ./CMakeCache.txt ]; then
   echo "Error: One can not change the compiler once configured !"
   echo "Error: In order to change the compiler, use a fresh build directory."
   exit 1
fi

if [ x$(which cmake) == x ]; then
   echo "Error: 'cmake' executable not found. Download it from http://www.cmake.org"
   echo "Error: or install the cmake package of your distribution version 2.2 or higher."
   exit 1
fi

# If CXXFLAGS set, build type must be set to None to have them picked up.
if [ "x$CXXFLAGS" != "x" ]; then
    CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=None"
    export CXXFLAGS
fi
if [ "x$LDFLAGS" != "x" ]; then
    #CMAKE_ARGS="$CMAKE_ARGS -DCMAKE_BUILD_TYPE=None"
    export LDFLAGS
fi

CMAKE_EXE=$(which cmake)

if [ x$CMAKE_EXE = x ]; then
  echo "Error: cmake program not found. See http://www.cmake.org"
  exit 1;
fi

echo "Invoking: '$CMAKE_CC $CMAKE_CXX $CMAKE_EXE .. $CMAKE_ARGS'"
if [ x$CMAKE_CC != x ]; then
export $CMAKE_CC 
fi
if [ x$CMAKE_CXX != x ]; then
export $CMAKE_CXX 
fi
$CMAKE_EXE $src_dir $CMAKE_ARGS || { echo -e "\ncmake produced an error - removing cache."; rm CMakeCache.txt; exit 1; }

echo
echo "OK: configure done. Now issue 'make'."
