VERSION=
ARCH=
PREFIX=
BUILD=
PKGCFG=
OMP=
EXAMPLES=
COMPILER=

function usage {
    echo "usage: configure <CFG-FILE-NAME>"
    echo "    a sample config file can be found in scripts/*.cfg"
    exit -1
}

if [ "$1"  = "" ] ; then
    usage ;
fi 
if [ "$1"  = "-help" ] ; then
    usage ;
fi 
if [ "$1"  = "--help" ] ; then
    usage ;
fi 
if [ "$1"  = "-h" ] ; then
    usage ;
fi 


DEPS="ipp mkl opencv libz libfreenect libmesasr libjpeg libpng unicap xine qt opengl opencl glx opensurf gtest xcf imagemagick libdc videodev genicam pylon rsb protobuf openni eigen3 pcl"

for T in $DEPS ; do 
    eval "path_$T=_" ; 
done



for T in "$@" ; do
    if [ ! -e "$T" ] ; then 
        echo "unable to find configuration file: $T" ;
        exit -1 ;
    fi
    
    while read line ; do
        if [[ ! "$line" =~ ^[\ \t]*\#.* ]] ; then      # comments
            if [ "$line" != "" ] ; then                   # empty lines
                if [[ ! "$line" =~ ^[\ \t]\$ ]] ; then # whitespace lines
                    T1=$(echo $line | cut -d ' ' -f 1 | tr '[:upper:]' '[:lower:]')
                    TR=$(echo $line | cut -d ' ' -s -f 2-)
                    #echo " processing $line (T1: $T1)"
                    case "$T1" in
                        "version") VERSION="$TR" ;;
                        "prefix") PREFIX="$TR" ;;
                        "build") BUILD="$TR" ;;
                        "arch") ARCH="$TR" ;;
                        "examples") EXAMPLES="$TR" ;;
                        "pkgcfg") PKGCFG="$TR" ;;
                        "omp") OMP="$TR" ;;
                        "compiler") COMPILER="$TR" ;;
                        *) eval "path_$T1=$TR" ;;
                    esac
                fi
            fi
        fi
    done < "$T"
done

if [ -z $PKGCFG ] ; then
    PKGCFG=false
fi

if [ -z $VERSION ] ; then 
    VERSION=release
fi
if [ -z $BUILD ] ; then 
    BUILD=build/$VERSION
fi
if [ -z $ARCH ] ; then 
    ARCH=false
fi
if [ -z $EXAMPLES ] ; then 
    EXAMPLES=all
fi
if [ -z $PREFIX ] ; then 
    PREFIX=/vol/nivision
fi

if [ -z $OMP ] ; then 
    OMP=false
fi

for D in $DEPS ; do
    P=$(eval "echo path_$D")
    if [ -z $P ] ; then
        eval "path_$D=/usr"
    fi
done

VERSION_UPPER=$(echo $VERSION | tr '[:lower:]' '[:upper:]')
if [ "$VERSION_UPPER" = "DEBUG" ] ; then
    DEBUG_VAL=TRUE
else
    DEBUG_VAL=FALSE
fi

CMD="cmake -D ICL_XDEP_ALL_ON:BOOL=FALSE -D ICL_VAR_INSTALL_PREFIX:STRING=$PREFIX "
CMD="$CMD -D ICL_VAR_DEBUG_MODE:BOOL=$DEBUG_VAL"
CMD="$CMD -D ICL_VAR_USE_OMP:BOOL=$(echo $OMP | tr '[:lower:]' '[:upper:]')"
CMD="$CMD -D ICL_VAR_ARCHITECTURE_DEPENDENT_BUILD_ON:BOOL=$(echo $ARCH | tr '[:lower:]' '[:upper:]')"
CMD="$CMD -D ICL_VAR_USE_PKG_CONFIG:BOOL=$(echo $PKGCFG | tr '[:lower:]' '[:upper:]')"
CMD="$CMD -D ICL_VAR_EXAMPLES_TO_BUILD:STRING=$EXAMPLES "
if [ "$COMPILER" != "" ] ; then
  CMD="$CMD -D CMAKE_CXX_COMPILER=${COMPILER} "
fi
echo "extracting configuration"
echo "-----------------------------------------------------"
for D in $DEPS ; do
    U=$(echo $D | tr '[:lower:]' '[:upper:]')
    P=$(echo $(eval echo \$path_$D))
    echo $D | awk '{printf "%-14s",$1}'
    if [ "$P" = "_" ] ; then
        CMD="$CMD -D ICL_XDEP_${U}_ON:BOOL=FALSE -D ICL_XDEP_${U}_PATH:STRING=/unexistent-directory"
        echo "off"
    elif [ "$P" = "" ] ; then
        CMD="$CMD -D ICL_XDEP_${U}_ON:BOOL=TRUE -D ICL_XDEP_${U}_PATH:STRING=/usr"
        echo "/usr (default)"
    else
        CMD="$CMD -D ICL_XDEP_${U}_ON:BOOL=TRUE -D ICL_XDEP_${U}_PATH:STRING=$P"
        echo "$P"
    fi
done
echo "-----------------------------------------------------"
echo "ICL version:               $(./VERSION.sh)"
echo "version (debug/release):   $VERSION"
echo "build directory:           $BUILD"
echo "arch dependent build:      $ARCH"
echo "examples:                  $EXAMPLES"
echo "installation prefix:       $PREFIX"
echo "use omp :                  $OMP"
if [ "$COMPILER" != "" ] ; then
  echo "compiler:                  $COMPILER" ;
else
  echo "compiler:                  $(which g++)" ;
fi
echo "-----------------------------------------------------"



if [ ! -e $BUILD ] ; then
    echo creating build directory $BUILD ;
    mkdir -p $BUILD ;
fi

PREVDIR=$PWD
cd $BUILD

CMD="$CMD $PREVDIR"

echo "CMAKE command is"
echo "-----------------------------------------------------"
echo $CMD
echo "-----------------------------------------------------"

CMD="$CMD"
TMPFILE="./.configure-out"
echo -n "run cmake now ? [yes|no] (default: yes): "
read x
if [ "$x" = "" ] ; then 
    echo "precaching cmake output ..."
    $CMD > $TMPFILE;
elif [ "$x" = "yes" ] ; then
    echo "precaching cmake output ..."
    $CMD > $TMPFILE;
elif [ "$x" = "y" ] ; then
    echo "precaching cmake output ..."
    $CMD > $TMPFILE;
elif [ "$x" = "YES" ] ; then
    echo "precaching cmake output ..."
    $CMD > $TMPFILE;
elif [ "$x" = "Yes" ] ; then
    echo "precaching cmake output ..."
    $CMD > $TMPFILE;
else
    exit -1;
fi

CMAKE_DETECTED=""
while read line ; do
    if [ "$(echo $line | grep detected)" != "" ] ; then
        echo -n $(tput bold) ;
        CMAKE_DETECTED="$CMAKE_DETECTED\n$line"
    fi ;
    echo $line ;
    echo -n $(tput sgr0)
done < $TMPFILE
rm -rf $TMPFILE

echo ""
echo "deactivated dependencies"
echo -e $CMAKE_DETECTED | grep DEACTIVATED | sed 's|detected: DEACTIVATED||g'
echo ""
echo "found dependencies"
echo -e $CMAKE_DETECTED | grep TRUE | sed 's|detected: TRUE||g'
echo ""
echo "not found dependencies"
echo -e $CMAKE_DETECTED | grep FALSE | sed 's|detected: FALSE||g'