#!/bin/sh

#
#  This file is a part of TiledArray.
#  Copyright (C) 2013  Virginia Tech
#
#  This program is free software: you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation, either version 3 of the License, or
#  (at your option) any later version.
#
#  This program is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#
#  You should have received a copy of the GNU General Public License
#  along with this program.  If not, see <http://www.gnu.org/licenses/>.
#
#  Justus Calvin
#  Department of Chemistry, Virginia Tech
#
#  configure
#  Jul 19, 2013
#

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

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

# Display CMake bootstrap usage
help()
{
echo '
Configure TiledArray. 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
  --prefix=               install prefix (CMAKE_INSTALL_PREFIX=)
  --debug                 debug build (CMAKE_BUILD_TYPE=Debug)
  --unittest              build unit tests (TA_BUILD_UNITTEST=TRUE)
  --lapack=               LAPACK libraries (LAPACK_LIBRARIES=)
  --integer4              Assume integer*4 in Fortran (INTEGER4=TRUE), otherwise
                          integer*8 is the default Fortran integer size.
  --mpicc=                MPI C compiler to deduce MPI environment,
                          only needed if CXX is not set to an MPI compiler wrapper
                          (MPI_C_COMPILER=)
  --disable-mpi           Disable checks and inclusion of MPI (DISABLE_MPI) 
  --boost=                Boost path, only needed if --unittest is set also (BOOST_ROOT=)
  --madness=              MADNESS path (Madness_ROOT_DIR=)
  --elemental=            Elemental path (Elemental_ROOT_DIR=) 
  --error-checking=       specify the default error reporting style,
                          allowed values: none, throw (default), assert
                          (TA_ERROR=)
  --search=               paths to search for files/libraries, separated by semicolon;
                          specify here the locations of Eigen, MADNESS unless you want
                          configure to download and build them automatically
                          (CMAKE_PREFIX_PATH=)
  --expert                Expert mode: disables building dependencies, e.g. Eigen,
                          MADNESS, etc. (TA_EXPERT=TRUE)
  -D*                     passed verbatim to cmake command

Some influential environment variables:
  CPPFLAGS    C/C++ proprocessor flags     (e.g. CPPFLAGS='-I/opt/local/include -DNDEBUG')
  LDFLAGS     linker flags                 (e.g. LDFLAGS=-L/libdir)
  CXX         C++ compiler command         (e.g. CXX='mpicxx')
  CXXFLAGS    C++ compiler flags           (e.g. CXXFLAGS='-O0 -g -std=c++0x')
  F77         Fortran 77 compiler command  (e.g. F77='gfortran')
  FFLAGS      Fortran 77 compiler flags    (e.g. FFLAGS='-fdefault-integer-8')
  CC          C compiler command           (e.g. CC='mpicc')
  CFLAGS      C compiler flags             (e.g. CFLAGS='-O0 -g -std=c11')
'
  exit 1
}

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

# check if local cmake was built
CMAKE=$TOPDIR/cmake/bin/cmake
if [ ! -x "$CMAKE" ]; then
    CMAKE=`which cmake 2>/dev/null`
fi

# if no local/system cmake exist, build!
if [ -z "$CMAKE" ]; then
    make -C cmake
    CMAKE=$TOPDIR/cmake/bin/cmake
fi

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

# log the command-line arguments, 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 ;;
  --prefix=*)      prefix="`arg \"$1\"`" ;;
  --lapack=*)      lapack="`arg \"$1\"`"
                   args="$args -DLAPACK_LIBRARIES=\"$lapack\"" ;;
  --integer4)      args="$args -DINTEGER4=TRUE" ;;
  --debug)         build="Debug" ;; #args="$args -DCMAKE_BUILD_TYPE=Debug" ;;
  --search=*)      search="`arg \"$1\"`"
                   args="$args -DCMAKE_PREFIX_PATH=\"$search\"" ;;
  --mpicc=*)       args="$args -DMPI_C_COMPILER=`arg \"$1\"`" ;;
  --disabe-mpi)    args="$args -DENABLE_MPI=OFF" ;;
  --unittest)      args="$args -DTA_BUILD_UNITTEST=TRUE" ;;
  --boost=*)       args="$args -DBOOST_ROOT=`arg \"$1\"`" ;;
  --madness=*)     args="$args -DMadness_ROOT_DIR=`arg \"$1\"`" ;;
  --elemental=*)   args="$args -DElemental_ROOT_DIR=`arg \"$1\"` -DENABLE_ELEMENTAL=1" ;;
  --error-checking=*)   args="$args -DTA_ERROR=`arg \"$1\"`" ;;
  --expert)        args="$args -DTA_EXPERT=TRUE" ;;
  -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
