#!/bin/bash
# Copyright 2014-2015, Dominique LaSalle. Used with permission.


###############################################################################
# CONFIG VARIABLES ############################################################
###############################################################################

NAME="splatt"

###############################################################################
# FUNCTIONS ###################################################################
###############################################################################

die() {
  echo "ERROR: ${@}" 1>&2
  exit 1
}

abspath() {
  if [[ "${@::1}" == "/" ]]; then
    echo "${@}"
  else
    echo "${PWD}/${@}"
  fi
}

show_help() {
  echo "USAGE: configure [options]"
  echo ""
  echo "OPTIONS:"
  echo "  --prefix=<prefix>"
  echo "    Set the install prefix."
  echo "  --cc=<cc>"
  echo "    Set the C compiler to use."
  echo "  --mpi"
  echo "    Build with MPI support"
  echo "  --mtmetis"
  echo "    Build with MT-Metis graph partitioning (must be previously installed)."
  echo "  --patoh"
  echo "    Build with PaToH hypergraph partitioning (must be previously installed)."
  echo "  --ashado"
  echo "    Build with Ashado hypergraph partitioning (must be previously installed)."
  echo "  --debug"
  echo "    Build with debugging symbols, assertions, and turn optimizations off."
  echo "  --dev"
  echo "    Build in development mode. Warnings and extra logging enabled."
  echo ""
}


###############################################################################
# OPTION PARSING ##############################################################
###############################################################################


#CONFIG_FLAGS="-DCMAKE_VERBOSE_MAKEFILE=1"
CONFIG_FLAGS=""


# default values
CMAKE="$(which cmake)"
BUILDDIR="build/$(uname -s)-$(uname -m)"


# parse arguments
for i in "${@}"; do
  case "${i}" in
    # help
    -h|--help)
    show_help
    exit 0
    ;;
    # MPI support
    --mpi)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DUSE_MPI=1"
    ;;
    --mtmetis)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DUSE_MTMETIS=1"
    ;;
    --patoh)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DUSE_PATOH=1"
    ;;
    --ashado)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DUSE_ASHADO=1"
    ;;
    # prefix
    --prefix=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_INSTALL_PREFIX=${i#*=}"
    ;;
    # cc
    --cc=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_C_COMPILER=${i#*=}"
    ;;
    # debug
    --debug)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DDEBUG=1"
    ;;
    # dev
    --dev)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DDEV_MODE=1"
    ;;
    # bad argument
    *)
    die "Unknown option '${i}'"
    ;;
  esac
done


# check if cmake exists
if [[ ! -x "${CMAKE}" ]]; then
  die "Could not find usable cmake: '${CMAKE}'" 
else
  echo "Found CMAKE: '${CMAKE}'"
fi

# clean out build directory if it exists
if [[ -d "${BUILDDIR}" ]]; then
  echo "Removing old build directory '${BUILDDIR}'..."
  rm -rf "${BUILDDIR}"
fi


# create build directory 
mkdir -vp "${BUILDDIR}" || \
    die "Failed to create build directory: '${BUILDDIR}'"


###############################################################################
# RUN CMAKE ###################################################################
###############################################################################

ROOTDIR="${PWD}"
pushd "${BUILDDIR}"

echo "Calling cmake with arguments '${CONFIG_FLAGS}'"
"${CMAKE}" "${ROOTDIR}" ${CONFIG_FLAGS}
if [[ "$?" != "0" ]]; then
  echo "CMake failed with '$?'" 1>&2
  exit $?
fi

popd

# create proxy makefile
(
echo "#######################################################################"
echo "# Makefile generated by '$0' at $(date)"
echo "# Using flags:"
for d in ${CONFIG_FLAGS}; do
  echo "#	${d}"
done
echo "#######################################################################"
echo ""
echo "all clean install uninstall:"
echo "	make -C ${BUILDDIR} \$@ \$(MAKEFLAGS)"
echo ""
echo "distclean:"
echo "	rm -rf ${BUILDDIR} Makefile"
echo ""
) > Makefile



