#!/bin/sh
# ----------------------------------------------------------------------------
# Generic script for building Trilinos and Sundance on Linux machines with 
# configurations similar to gibbon.math.ttu.edu
#
# Run this script as 
# <kevin@gibbon> ./gibbon-linux-sundance-base [flags]
# where the possible flags are listed by running
# <kevin@gibbon> ./gibbon-linux-sundance-base --help
#
# This isn't meant as a completely general configuration script; rather, it
# allows streamlined setting of the several options I most commonly vary in
# my own work. You'll probably want to tweak this script for your own system
# and your own selection of packages. In particular, I've assumed we're 
# always building Sundance and that the directory layout is identical to 
# that on my development machines gibbon and chimera. 
#
# 22 March 2009
# Kevin Long (kevin.long@ttu.edu)
#
#-----------------------------------------------------------------------------

# Decide whether to build the Meros block preconditioning package.
# Default is no. 
#
ENABLE_MEROS=OFF


# ----------------------------------------------------------------------------
# Set the location of the compilers and libraries. On a simple out-of-the-box
# Linux system, COMPILER_ROOT will probably be someplace like /usr/local. 
# On Kevin's TTU machines, there are multiple compiler versions located
# in directories /usr/local/${COMPILER_VERSION}, e.g., /usr/local/gcc-4.3.2.
# If your system isn't organized this way, you can copy this script
# and modify as per your system.
#

COMPILER_ROOT=/usr/local
BIN_PATH=${COMPILER_ROOT}/bin
LIB_PATH=${COMPILER_ROOT}/lib
INC_PATH=${COMPILER_ROOT}/include

# ----------------------------------------------------------------------------
# Set the fortran library name. On older Linux systems this will 
# need to be changed to -lg2c. 
#
FORTRAN_LIBS="-lgfortran"


# ----------------------------------------------------------------------------
# Set variables that control the type of build
#
# Whether to build shared libraries. Default=no.
BUILD_SHARED=OFF
# Whether to build with MPI. Default=yes.
ENABLE_MPI=OFF
# Whether to build optimized or debug. Default=debug. Set to RELEASE or leave
# blank to enable an optimized build.
BUILD_TYPE=DEBUG
# Whether to enable a paranoid level of compiler warnings. This can cause
# problems with Python. Default: on. 
PARANOID_COMPILER=ON

# ----------------------------------------------------------------------------
# Set variables controlling which third-party libraries are available and
# should be linked in. 
#
# Whether the exodus mesh database libraries are available. Default=yes.
HAVE_EXODUS=ON

# ----------------------------------------------------------------------------
# Specify directories for the Trilinos source and Trilnos data, and for
# the installation of this build.

# Set the installation path
INSTALL_PREFIX=$PWD

# Set the path to the Trilinos source distribution
TRILINOS_SRC_DIR=${HOME}/Code/Trilinos

# Set the path to the Trilinos data files. Some of the Sundance tests require
# large mesh files stored in TrilinosData. If the TrilinosData directory
# cannot be found, these tests will be disabled.
TRILINOS_DATA_DIR=${HOME}/Code/TrilinosData

# ---------------------------------------------------------------------------
#
# At this point, all variables have been assigned default values. We now
# parse the command line to see if any values are overridden.
#
# ---------------------------------------------------------------------------
# Argument parsing logic.
TEMP=`getopt -o x --long help,mpi,serial,shared,static,debug,opt,exodus,no-exodus,meros,lax,src:,data:,prefix:: -- "$@"`

eval set -- "$TEMP"

while true ; do
  case "$1" in 
      --mpi) ENABLE_MPI=ON; shift ;;
      --serial) ENABLE_MPI=OFF; shift ;;
      --shared) BUILD_SHARED=ON; shift;;
      --static) BUILD_SHARED=OFF; shift;;
      --debug) BUILD_TYPE=DEBUG; shift;;
      --opt) BUILD_TYPE=RELEASE; shift;;
      --lax) PARANOID_COMPILER=OFF; shift;;
      --exodus) HAVE_EXODUS=ON; shift;;
      --no-exodus) HAVE_EXODUS=OFF; shift;;
      --meros) ENABLE_MEROS=ON; shift;;
      --src)
            TRILINOS_SRC_DIR="$2"; shift 2;;
      --data)
            TRILINOS_DATA_DIR="$2"; shift 2;;
      --prefix)
            INSTALL_PREFIX="$2"; shift 2;;
      --help)
            echo "Command-line arguments:"
            echo "--help           help"
            echo "--mpi            enable MPI (default: off)"
            echo "--serial         turn off MPI"
            echo "--shared         build shared libraries (default: on)"
            echo "--static         build static libraries (default: off)"
            echo "--debug          turn on debugging features (default: on)"
            echo "--opt            turn on optimized compilation (default: off)"
            echo "--lax            turn off paranoid compiler flags (necessary for python wrappers)"
            echo "--exodus         enable ExodusII readers (requires ExodusII libraries. Default: on)"
            echo "--no-exodus      disable ExodusII readers"
            echo "--meros          enable Meros block preconditioners (default: off)"
            echo "--src <srcdir>   set location of Trilinos sources (default: \${HOME}/Code/Trilinos)"
            echo "--data <dir>     set location of Trilinos data (default: \${HOME}/Code/TrilinosData)"
            echo "--prefix <dir>   set installation directory (default: \${PWD})"
            echo " ";
            exit 0;
            shift
            ;;
      --) shift; break;;
  esac
done

# ---------------------------------------------------------------------------
#
# Now run cmake!!
#
# ---------------------------------------------------------------------------

cmake \
-D CMAKE_BUILD_TYPE:STRING=${BUILD_TYPE} \
-D CMAKE_SYSTEM_LIBRARY_PATH:FILEPATH="$LIB_PATH" \
-D CMAKE_SYSTEM_INCLUDE_PATH:FILEPATH="$INC_PATH" \
-D BUILD_SHARED_LIBS:BOOL=${BUILD_SHARED} \
-D TPL_ENABLE_ExodusII:BOOL=${HAVE_EXODUS} \
-D TPL_ENABLE_MPI:BOOL=${ENABLE_MPI} \
-D Trilinos_EXTRA_LINK_FLAGS:STRING=${FORTRAN_LIBS} \
-D Trilinos_ENABLE_SHADOW_WARNINGS:BOOL=OFF \
-D Trilinos_ENABLE_STRONG_CXX_COMPILE_WARNINGS:BOOL=${PARANOID_COMPILER} \
-D Trilinos_ENABLE_TESTS:BOOL=ON \
-D Trilinos_ENABLE_Sundance:BOOL=ON \
-D Trilinos_ENABLE_Meros:BOOL=${ENABLE_MEROS} \
-D Sundance_ENABLE_BROKEN_CODE:BOOL=OFF \
-D Trilinos_DATA_DIR:FILEPATH="${TRILINOS_DATA_DIR}" \
-D NOX_ENABLE_LOCA:BOOL=OFF \
-D CMAKE_INSTALL_PREFIX:PATH=${INSTALL_PREFIX} \
${TRILINOS_SRC_DIR}

exit 0