#!/bin/bash
# Copyright 2014-2016, Dominique LaSalle
#


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

NAME="wildriver"


###############################################################################
# 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 "  --examples"
  echo "    Build the examples."
  echo "  --prefix=<prefix>"
  echo "    Set the install prefix."
  echo "  --static"
  echo "    Generate a staic version of lib${NAME} instead of a shared one."
  echo "  --cc=<c compiler>"
  echo "    Set the C compiler to use."
  echo "  --cxx=<c++ compiler>"
  echo "    Set the C++ compiler to use."
  echo "  --dimension-type={int32_t|int64_t|uint32_t|uint64_t}"
  echo "    Set the type to use for matrix dimensions (rows/columns) and "
  echo "    graph vertex numbers (default int64_t)."
  echo "  --index-type={int32_t|int64_t|uint32_t|uint64_t}"
  echo "    Set the type to use for matrix non-zero indexes and graph edge "
  echo "    indexes (default int64_t)."
  echo "  --value-type={int32_t|int64_t|uint32_t|uint64_t|float|double}"
  echo "    Set the type to use for matrix values and graph weights "
  echo "    (default double)."
  echo "  --devel"
  echo "    Turn on compiler warnings."
  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"
    ;;
    # examples
    --examples)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DEXAMPLES=1"
    ;;
    # prefix
    --prefix=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_INSTALL_PREFIX=${i#*=}"
    ;;
    # static 
    --static)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DSTATIC=1"
    ;;
    # devel
    --devel)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DDEVEL=1"
    ;;
    # cc
    --cc=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_C_COMPILER=${i#*=}"
    ;;
    # cxx
    --cxx=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DCMAKE_CXX_COMPILER=${i#*=}"
    ;;
    # dimension type
    --dimension-type=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DWILDRIVER_DIMENSION_TYPE=${i#*=}"
    ;;
    # index type
    --index-type=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DWILDRIVER_INDEX_TYPE=${i#*=}"
    ;;
    # value type
    --value-type=*)
    CONFIG_FLAGS="${CONFIG_FLAGS} -DWILDRIVER_VALUE_TYPE=${i#*=}"
    ;;
    # 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


# 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 ""
echo ".PHONY: test"
echo ""
) > Makefile



