#!/bin/bash

BUILD_DEBUG="yes"
BUILD_RELEASE="yes"

OPTIONS=""

CMAKE_TEST=`which cmake`    
if test x${CMAKE_TEST} = "x";
then
    echo "Cmake is not installed on your system. Please install it by:"
    echo ""
    echo "   sudo apt-get install cmake     on Ubuntu and Debian based systems"
    echo "   sudo yum install cmake         on RedHat, Fedora or CentOS"
    echo "   sudo zypper install cmake      on OpenSuse"
    echo ""
    echo "You may also install it from the source code at:"
    echo " http://www.cmake.org/download/"
    exit 1
fi

for option in "$@"
do
    case $option in
        --no-debug                    ) BUILD_DEBUG="no" ;;
        --no-release                  ) BUILD_RELEASE="no" ;;        
        *                             ) OPTIONS="${OPTIONS} ${option}" ;;
    esac
done

if test ${BUILD_DEBUG} = "yes";
then
    if [ ! -d Debug ];
    then
       mkdir Debug
    fi
    cd Debug
    if ! ../build --root-dir=.. --build=Debug --install=yes ${OPTIONS}
    then
       exit 1
    fi
    cd ..
fi

if test ${BUILD_RELEASE} = "yes";
then
    if [ ! -d Release ];
    then
       mkdir Release
    fi
    cd Release
    if ! ../build --root-dir=.. --build=Release --install=yes ${OPTIONS};
    then
        exit 1
    fi
    cd ..
fi


PREFIX=${HOME}/.local
for option in "$@"
do
    case $option in
        --prefix=*                     ) PREFIX="${option#*=}" ;;
    esac
done


TNL_TEST=`which tnl-bindir`

if test x${TNL_TEST} = x;
then
    echo ""
    echo "WARNING !!!"
    echo ""
    echo "Your system does not see TNL which was installed right now."
    echo "You need to add it to your system variables PATH and LD_LIBRARY_PATH."
    echo "Add the following to your .bashrc file:"
    echo ""
    
    echo "if test x\${PATH} = x;"
    echo "then"
    echo "   PATH=${PREFIX}/bin"
    echo "else"
    echo "   PATH=\${PATH}:${PREFIX}/bin"
    echo "fi"
    echo "if test x\${LD_LIBRARY_PATH} = x;"
    echo "then"
    echo "   LD_LIBRARY_PATH=${PREFIX}/lib"
    echo "else"
    echo "   LD_LIBRARY_PATH=\${LD_LIBRARY_PATH}:${PREFIX}/lib"
    echo "fi"
    echo "export PATH"
    echo "export LD_LIBRARY_PATH"
fi

if test ${BUILD_RELEASE} = "yes";
then
    BUILD_PREFIX="Release"
fi
if test ${BUILD_DEBUG} = "yes";
then
    BUILD_PREFIX="Debug"
fi

exit 0

