#!/bin/csh -x
# Script for automated compilation of libcchem using cmake 
# Intended for travis ci 

source ../install.info

#================================#
#== set some initial variables ==#
#================================#
set jflag=`nproc`

set eigen="-DEIGEN_ROOT=$GMS_EIGEN_PATH"
set hdf5="-DHDF5_ROOT=$GMS_HDF5_PATH"
set matlib="-DMATHLIB=$GMS_MATHLIB"
set matroot="-DMATHLIB_ROOT=$GMS_MATHLIB_PATH"
set mpi="-DMPI_ROOT=$GMS_MPI_PATH"
set misc=""

if ($GMS_LIBCCHEM_GPU_SUPPORT == true) then
   set cuda="-DGPU_BOARD=$GMS_CUDA_BOARD"
endif

if (($GMS_FORTRAN == gfortran) || ($GMS_FORTRAN == aocc)) then
   set cxxcomp="-DCMAKE_CXX_COMPILER=g++"
   set cccomp="-DCMAKE_C_COMPILER=gcc"
else if ($GMS_FORTRAN == ifort) then
   set cxxcomp="-DCMAKE_CXX_COMPILER=icpc"
   set cccomp="-DCMAKE_C_COMPILER=icc"
else if ($GMS_FORTRAN == pgfortran) then
   set cxxcomp="-DCMAKE_CXX_COMPILER=pgc++"
   set cccomp="-DCMAKE_C_COMPILER=pgcc"
else
   set cxxcomp=""
   set cccomp=""
endif

set boost="-DBOOST_ROOT=$GMS_BOOST_PATH"
set ga="-DGA_ROOT=$GMS_GA_PATH"

set modules=""
if ($GMS_CCHEM_HF == true) then
   set modules="$modules -DBUILD_HF=1"  
endif
if ($GMS_CCHEM_MP2 == true) then
   set modules="$modules -DBUILD_MP2=1"
endif
if ($GMS_CCHEM_RI == true) then
   set modules="$modules -DBUILD_RI=1"
endif
if ($GMS_CCHEM_CC == true) then
   set modules="$modules -DBUILD_CC=1"
endif

set base="cmake $cccomp $cxxcomp $boost $eigen $hdf5 $matlib $matroot $mpi $ga $misc $modules ../"
set rys="cmake $cccomp $cxxcomp $boost $misc $modules ../"

if ($GMS_LIBCCHEM_GPU_SUPPORT == true) then
   set base="$base $cuda"
   set rys="$rys $cuda"
endif

#================#
#== build rysq ==#
#================#
if (-d rysq/build) then
   rm -rf rysq/build 
endif
mkdir rysq/build; 

cd rysq/build

$rys
if ($status > 0) then
  exit
endif

make -j $jflag
if ($status > 0) then
  exit
endif

make install
if ($status > 0) then
  exit
endif

#====================#
#== build libcchem ==#
#====================#
cd ../../

if (-d build ) then
    rm -rf build
endif
mkdir build

cd build

$base
if ($status > 0) then
  exit
endif

make -j $jflag
if ($status > 0) then
  exit
endif

make install
if ($status > 0) then
  exit
endif

#==================#
#== build GAMESS ==#
#==================#
cd ../../

make clean
if ($status > 0) then
  exit
endif

make clean_ddi
if ($status > 0) then
  exit
endif

make ddi
if ($status > 0) then
  exit
endif

make modules
if ($status > 0) then
  exit
endif

make -j $jflag
if ($status > 0) then
  exit
endif

