#!/bin/sh

TOPDIR=$(cd `dirname $0` && pwd)
#echo $TOPDIR

# Strip prefix from argument
arg () {
  echo "$1" | sed "s/^${2-[^=]*=}//"
}

# Display CMake bootstrap usage
help()
{
echo '
Configure MPQC. This script will invoke cmake.
Alternatively, use cmake or ccmake directly.

Invoked as "configure cmake" it will download and install cmake into ./cmake directory 

Usage: '"$0"' [<options>...]
Options: [defaults in brackets after descriptions]
Configuration:
  --help                  print this message
  --new-features          MPQC new features (-DMPQC_NEW_FEATURES=TRUE)
  --debug                 debug build (CMAKE_BUILD_TYPE=Debug)
  --shared                build as shared libs (BUILD_SHARED_LIBS=ON)
  --prefix=               install prefix (CMAKE_INSTALL_PREFIX=)
  --lapack=               LAPACK libraries (LAPACK_LIBRARIES=)
  --integer8              assume integer*8 in fortran
  --mpicc=                MPI C compiler used to deduce MPI environment (MPI_C_COMPILER=)
                          WARNING: This does not set CC or CXX, set those explicitly
  --libint=               TRUE/FALSE or installed Libint prefix path
  --boost=                Boost path
  --unittest              Will build the boost unit testing library. Requires 
                          boost be built by MPQC. (MPQC_UNITTEST=FALSE)
  --search=               paths to search for files/libraries, separated by semicolon
                          (CMAKE_PREFIX_PATH=)
  --expert                Expert mode: disables building dependencies (MPQC_EXPERT=TRUE)
  --openmp                Enable OpenMP (MPQC_OPENMP=TRUE)
  --python                Enable Python bindings; uses default python executable ('env python')
                          if want specific executable, set cmake variable PYTHON_EXECUTABLE
  --ci=                   Enable/disable CI
  -D*                     defines cmake variables, passed verbatim to cmake command

Environment variables:
  CC          C compiler command       (e.g. CC='mpicc')
  CFLAGS      C compiler flags         (e.g. CFLAGS='-O0 -g -std=c11')
  CXX         C++ compiler command     (e.g. CXX='mpicxx')
  CXXFLAGS    C++ compiler flags       (e.g. CXXFLAGS='-O0 -g -std=c++0x')
  CPPFLAGS    C/C++ proprocessor flags (e.g. CPPFLAGS='-I/opt/local/include -DNDEBUG')
  F77         Fortran 77 compiler command  (e.g. F77='gfortran')
  FFLAGS      Fortran 77 compiler flags    (e.g. FFLAGS='-fdefault-integer-8')
  LDFLAGS     linker flags             (e.g. LDFLAGS=-L/libdir)
  CMAKE       cmake command to use     (e.g. CMAKE='/opt/local/bin/cmake')

MPQC-specific cmake variables:
  INTEGRALLIBINT2_NORMCONV          Normalization convention of IntegralLibint2, 1(cca; default) or 2(exact)
  DEFAULT_MPQC_MEMORY               Limit for how much memory to use by default, in bytes (default=536870912).
'
  exit 10
}

# was told to build local cmake
if [ $# -eq 1 -a "$1" = "cmake" ]; then
    cd $TOPDIR/cmake && make cmake
    exit
fi

# use user-provided CMAKE
if [ ! -x "$CMAKE" ]; then
    # look for locally-built cmake
    if [ -x $TOPDIR/cmake/bin/cmake ]; then
        CMAKE=$TOPDIR/cmake/bin/cmake
    # else look for system cmake
    else
        CMAKE=`which cmake 2>/dev/null`
	if [ ! -x "$CMAKE" ]; then
	    echo 'cmake is not found; try building cmake by typing: ./configure cmake'
	    exit 11
	fi	
    fi
fi

srcdir=`dirname $0`
args=""
prefix=$PWD
build="Release"

# log the command-line args, preserving quotes
log="#!/bin/sh\n# Last invoked on `date`:\n$0 "
for token in "$@"
  do
    log="$log '$token'"
  done
  
while test $# != 0; do
  case "$1" in
  --help|-h|-help) help ;;
  --prefix=*)      prefix="`arg \"$1\"`" ;;
  --new-features)  args="$args -DMPQC_NEW_FEATURES=TRUE" ;;
  --lapack=*)      lapack="`arg \"$1\"`"
                   args="$args -DLAPACK_LIBRARIES=\"$lapack\"" ;;
  --integer8)      args="$args -DINTEGER8=TRUE" ;;
  --debug)         build="Debug" ;; #args="$args -DCMAKE_BUILD_TYPE=Debug" ;;
  --shared)        args="$args -DBUILD_SHARED_LIBS=ON" ;;
  --search=*)      search="`arg \"$1\"`"
		   args="$args -DCMAKE_PREFIX_PATH=\"$search\"" ;;
  --libint=*)      libint="`arg \"$1\"`"
                   args="$args -DLIBINT=\"$libint\"" ;;
  --mpicc=*)       args="$args -DMPI_C_COMPILER=`arg \"$1\"`" ;;
  --boost=*)       args="$args -DBOOST=`arg \"$1\"`" ;;
  --unittest)      args="$args -DMPQC_UNITTEST=TRUE" ;;
  --expert)        args="$args -DMPQC_EXPERT=TRUE" ;;
  --openmp)        args="$args -DMPQC_OPENMP=TRUE" ;;
  --python)        args="$args -DPYTHON=TRUE" ;;
  --ci=*)          args="$args -DMPQC_CI=`arg \"$1\"`" ;;
  -D*) args="$args \"$1\"" ;; # raw  cmake arg
  CC=*) CC="`arg \"$1\"`" ;;
  CXX=*) CXX="`arg \"$1\"`" ;;
  F77=*) F77="`arg \"$1\"`" ;;
  CPPFLAGS=*) CPPFLAGS="`arg \"$1\"`" ;;
  CFLAGS=*) CFLAGS="`arg \"$1\"`" ;;
  CXXFLAGS=*) CXXFLAGS="`arg \"$1\"`" ;;
  FFLAGS=*) FFLAGS="`arg \"$1\"`" ;;
  LDFLAGS=*) LDFLAGS="`arg \"$1\"`" ;;
  *) exit "Unknown option: \"$1\"" ;;
  esac
  shift
done

# dump command-line arguments into reconf.sh
echo $log > reconf.sh
chmod +x reconf.sh

args="$args -DCMAKE_INSTALL_PREFIX=$prefix"
args="$args -DCMAKE_BUILD_TYPE=$build"

if [ -n "$CC" ]; then
    args="$args -DCMAKE_C_COMPILER=$CC"
fi

if [ -n "$CXX" ]; then
    args="$args -DCMAKE_CXX_COMPILER=$CXX"
fi

if [ -n "$F77" ]; then
    args="$args -DCMAKE_Fortran_COMPILER=$F77"
fi

if [ -n "$CPPFLAGS" ]; then
    args="$args -DCMAKE_CPP_FLAGS=\"$CPPFLAGS\""
fi

if [ -n "$CFLAGS" ]; then
    args="$args -DCMAKE_C_FLAGS=\"$CFLAGS\""
fi

if [ -n "$CXXFLAGS" ]; then
    args="$args -DCMAKE_CXX_FLAGS=\"$CXXFLAGS\""
fi

if [ -n "$FFLAGS" ]; then
    args="$args -DCMAKE_Fortran_FLAGS=\"$FFLAGS\""
fi

if [ -n "$LDFLAGS" ]; then
    args="$args -DCMAKE_EXE_LINKER_FLAGS=\"$LDFLAGS\""
fi

echo "Using CMake $CMAKE"
cmd="$CMAKE $args $srcdir"

set -x
rm -fr CMakeFiles CMakeCache.txt
eval $cmd
