#!/bin/bash
# Copyright 2014-2015, Dominique LaSalle
#
# I know I should write this in plain 'sh', but I haven't...


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

NAME="mt-metis"
DOMLIB_PATH="domlib"
WILDRIVER_PATH="wildriver"
METIS_PATH="metis"


###############################################################################
# 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 "  --debug"
  echo "    Build with debugging symbols and turn optimizations off."
  echo "  --assert"
  echo "    Build with assertions on."
  echo "  --prefix=<prefix>"
  echo "    Set the install prefix."
  echo "  --shared"
  echo "    Generate a shared version of lib${NAME} instead of a static one."
  echo "  --cc=<cc>"
  echo "    Set the C compiler to use."
  echo "  --devel"
  echo "    Turn on compiler warnings."
  echo "  --edges64bit"
  echo "    Use 64 bit data types for edges instead of 32 bit."
  echo "  --vertices64bit"
  echo "    Use 64 bit data types for vertices instead of 32 bit."
  echo "  --weights64bit"
  echo "    Use 64 bit data types for weights instead of 32 bit."
  echo "  --partitions64bit"
  echo "    Use 64 bit data types for partitions instead of 32 bit."
  echo "  --test"
  echo "    Enable unit testing."
  echo ""
}


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


CONFIG_FLAGS="-DCMAKE_VERBOSE_MAKEFILE=1"


# 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
    ;;
    # debug
    --debug)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DDEBUG=1"
    ;;
    # assert
    --assert)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DASSERT=1"
    ;;
    # prefix
    --prefix=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_INSTALL_PREFIX=${i#*=}"
    ;;
    # shared
    --shared)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DSHARED=1"
    ;;
    # devel
    --devel)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DDEVEL=1"
    ;;
    # cc
    --cc=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_C_COMPILER=${i#*=}"
    ;;
    # 64 bit edges
    --edges64bit)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DBIGEDGES=1"
    ;;
    # 64 bit vertices
    --vertices64bit)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DBIGVERTICES=1"
    ;;
    # 64 bit wegihts
    --weights64bit)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DBIGWEIGHTS=1"
    ;;
    # 64 bit partitions
    --partitions64bit)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DBIGPARTITIONS=1"
    ;;
    # testing
    --test)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DTESTS=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}'"
  echo "--> $("${CMAKE}" --version)"
fi


# check if domlib exists
if [[ ! -d "${DOMLIB_PATH}" ]]; then
  die "Could not find domlib: '${DOMLIB_PATH}'"
else
  CONFIG_FLAGS="${CONFIG_FLAGS} -DDOMLIB_PATH=${DOMLIB_PATH}"
fi


# check if wildriver exists
if [[ ! -d "${WILDRIVER_PATH}" ]]; then
  die "Could not find wildriver: '${WILDRIVER_PATH}'"
else
  CONFIG_FLAGS="${CONFIG_FLAGS} -DWILDRIVER_PATH=${WILDRIVER_PATH}"
fi


# check if metis exists
if [[ ! -d "${METIS_PATH}" ]]; then
  die "Could not find metis: '${METIS_PATH}'"
else
  CONFIG_FLAGS="${CONFIG_FLAGS} -DMETIS_PATH=${METIS_PATH}"
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 test clean install:"
echo "	make -C ${BUILDDIR} \$@ \$(MAKEFLAGS)"
echo ""
echo "distclean:"
echo "	rm -rvf ${BUILDDIR} Makefile"
echo ""
) > Makefile



