#!/bin/sh -
# ======================================================================
#  Configure script for G4Bench
#
#  Note:
#  This script is an alternative cmake wrapper.
#  The script generates a cmake build directory in "build" directory.
#  You can do the same things with cmake command using -DXXX=VVV options.
# ======================================================================
export LANG=C

# ======================================================================
# help message
# ======================================================================
show_help() {
cat <<EOF

\`configure' cmake configure script for G4Bench apps.

Usage: configure [OPTION]... [VAR=VALUE]...

Options:
  -h, --help                display this help and exit

Installation directories:
  --prefix=PREFIX           installation prefix [config.cmake]

Fine tuning of the library build:
  --with-geant4-dir=DIR     Geant4 installed dir [config.cmake]

Enable/disable options: prefix with either --enable- or --disable-
  vis      OpenGL support [disable]
  opt      optimization (O3) [enable]
  debug    debug mode [disable]
  dev      development mode [config.cmake/disable]

After configuration is done,
cd build
make
make install
EOF
}

# ======================================================================
# functions
# ======================================================================
check_error() {
  if [ $? -ne 0 ]; then
    exit -1
  fi
}

show_line() {
echo "========================================================================"
}

# ======================================================================
# main
# ======================================================================
# default values
prefix=_cmake
g4_install=_cmake
enable_vis=0
enable_opt=1
enable_debug=0
enable_dev=_cmake

# parsing options
while test $# -gt 0
do
  case $1 in
    -*=*) optarg=`echo "$1" | sed 's/[-_a-zA-Z0-9]*=//'` ;;
    *) optarg= ;;
  esac

  case $1 in
    --help|-h) show_help;  exit 0 ;;
    # ---------------------------------------------------------------
    --prefix=*)                prefix=$optarg                    ;;
    --with-geant4-dir=*)       g4_install=$optarg                ;;
    # ---------------------------------------------------------------
    --enable-vis )         enable_vis=1                          ;;
    --disable-vis )        enable_vis=0                          ;;
    --enable-opt )         enable_opt=1                          ;;
    --disable-opt )        enable_opt=0                          ;;
    --enable-debug )       enable_debug=1                        ;;
    --disable-debug )      enable_debug=0                        ;;
    --enable-dev )         enable_dev=1                          ;;
    --disable-dev )        enable_dev=0                          ;;
    # ---------------------------------------------------------------
    -*)
      echo "Unrecognized option: $1"
      exit -1
      ;;
    *)
      echo "Invalid argument: $1"
      exit -1
      ;;
  esac
  shift
done

# ======================================================================
cmake_option=""

echo "Build configuration:"
echo -n "G4Bench prefix   ... "
if [ $prefix = _cmake ]; then
  echo "[config.cmake]"
else
  echo $prefix
  cmake_option="-DCMAKE_INSTALL_PREFIX=${prefix}"
fi

echo -n "Geant4 dir     ... "
if [ $g4_install = _cmake ]; then
  echo "[config.cmake]"
else
  echo $g4_install
  cmake_option="${cmake_option} -DGEANT4_INSTALL=${g4_install}"
fi

echo ""
echo -n "Enabled support for [ "
if [ $enable_vis = 1 ]; then
  echo -n "vis "
  cmake_option="${cmake_option} -DENABLE_VIS=TRUE"
else
  cmake_option="${cmake_option} -DENABLE_VIS=FALSE"
fi

if [ $enable_debug = 1 ]; then
  echo -n "debug "
  cmake_option="${cmake_option} -DDEBUG=TRUE"
  enable_opt=0
else
  cmake_option="${cmake_option} -DDEBUG=FALSE"
fi

if [ $enable_opt = 1 ]; then
  echo -n "opt "
  cmake_option="${cmake_option} -DOPTIMIZE=TRUE"
else
  cmake_option="${cmake_option} -DOPTIMIZE=FALSE"
fi

if [ $enable_dev != _cmake ]; then
  if [ $enable_dev = 1 ]; then
    echo -n "dev "
    cmake_option="${cmake_option} -DDEVMODE=TRUE"
  else
    cmake_option="${cmake_option} -DDEVMODE=FALSE"
  fi
fi

echo "]"
echo " "
echo "cmake flags:${cmake_option}"
# ======================================================================
show_line
echo "@@ execute cmake..."
if [ -d build ]; then
  rm -r -f build
fi
mkdir build
cd build
cmake $cmake_option ..
check_error
exit 0
