#!/usr/bin/env bash
#
# vim: ft=sh

install_dir="${INSTALL_HPP_DIR-"${DEVEL_HPP_DIR}/install"}"
src_dir="${DEVEL_HPP_DIR}/src"

BDIR=build
LOGFILE="/tmp/hppmake.log"

run_co=0
git=`which git`
co_command="$git clone"
co_options="--recursive"
co_url="git@github.com:humanoid-path-planner"
co_branch="master"

run_pull=0
pull_command="$git pull"
pull_options="--recurse-submodules"
pull_remote_name="origin"

run_cmake=0
cmakeargs=""
debug=1

makeargs="-s -j4"
makecommand="install"

function usage() {
  echo -e "Usage: $0 <options> <packages> <options> <packages> ..."
  echo -e ""
  echo -e "Options are:"
  echo -e "-h|--help"
  echo -e "-d|--debug       \tequivalent to --build-dir build"
  echo -e "-r|--release     \tequivalent to --build-dir build-rel"
  echo -e "--build-dir      \twhere make is executed [${BDIR}]"
  echo -e "--src-dir        \tset the source directory [${src_dir}]"
  echo -e "--install-dir    \tset the install directory [${install_dir}]"
  echo -e ""
  echo -e "--checkout       \tcheckout repository if it does not exist"
  echo -e "--no-checkout    \tdo not checkout repository if it does not exist [default]"
  echo -e "--pull           \tpull from remote repository if it does exist"
  echo -e "--no-pull        \tdo not pull [default]"
  echo -e "--checkout-url   \tbase url [${co_url}]"
  echo -e "--checkout-branch\tbranch [${co_branch}]"
  echo -e ""
  echo -e "--cmake          \trun cmake for every following build"
  echo -e "--no-cmake       \tdo not run cmake for every following build [default]"
  echo -e "--cmake-args     \tcmake arguments [${cmakeargs}]"
  echo -e ""
  echo -e "-c|--make-command\tmake targets [${makecommand}]"
  echo -e "-a|--make-args   \tmake arguments [${makeargs}]"
  echo -e ""
  echo -e "-l|--show-log    \tshow make output [default]"
  echo -e "-L|--no-show-log \tredirect make output to ${LOGFILE}"
  echo -e ""
  echo -e "Examples:"
  echo -e "$0 -d hpp-core -r hpp-corbaserver -d hpp-manipulation -r hpp-manipulation-urdf hpp-manipulation-corba"
}

function check_retcode() {
  if [ $1 != 0 ]; then exit $1; fi
}

function checkout()
{
  if [ ! -d "${src_dir}/$1" ]; then
    echo "cd ${src_dir}"
    echo "${co_command} ${co_options} --branch ${co_branch} "${co_url}/$1""
    cd ${src_dir} && ${co_command} ${co_options} --branch ${co_branch} "${co_url}/$1"
    check_retcode $?
  fi
}

function pull()
{
  if [ -d "$1" ]; then
    echo "cd $1"
    echo "${pull_command} ${pull_options}"
    cd $1 && ${pull_command} ${pull_options}
    check_retcode $?
  fi
}

function configure()
{
  src=$1
  build=$2
  if [ $debug == 0 ]; then
    build_type="Release"
  else
    build_type="Debug"
  fi
  if [ ! -d "${build}" ]; then
    mkdir --parents ${build}
  fi
  echo "cd ${build}"
  echo "cmake -DCMAKE_INSTALL_PREFIX=${install_dir} -DCMAKE_BUILD_TYPE=${build_type} ${cmakeargs} ${src}"
  cd "${build}" && cmake -DCMAKE_INSTALL_PREFIX=${install_dir} -DCMAKE_BUILD_TYPE=${build_type} ${cmakeargs} "${src}" >&3
  check_retcode $?
}

function compile ()
{
  build=$1
  echo "cd ${build}"
  echo "make $makeargs $makecommand"
  cd "${build}" && make $makeargs $makecommand >&3
  check_retcode $?
}

test -f ${LOGFILE} && rm ${LOGFILE}
exec 3>&1

show_usage=true
while  [[ $# > 0 ]]
do
  key="$1"

  case $key in
    -h|--help)
      usage
      exit 0
      ;;
    -d|--debug)
      BDIR=build
      debug=1
      ;;
    -r|--release)
      BDIR=build-rel
      debug=0
      ;;
    --build-dir)
      BDIR="$2"
      shift # past argument
      ;;
    --src-dir)
      src_dir="$2"
      shift # past argument
      ;;
    --install-dir)
      install_dir="$2"
      shift # past argument
      ;;

    --checkout)
      run_co=1
      ;;
    --no-checkout)
      run_co=0
      ;;
    --pull)
      run_pull=1
      ;;
    --no-pull)
      run_pull=0
      ;;
    --checkout-url)
      co_url="$2"
      shift # past argument
      ;;
    --checkout-branch)
      co_branch="$2"
      shift # past argument
      ;;

    --cmake)
      run_cmake=1
      ;;
    --no-cmake)
      run_cmake=0
      ;;
    --cmake-args)
      cmakeargs=$2
      shift
      ;;

    -c|--make-command)
      makecommand=$2
      shift
      ;;
    -a|--make-args)
      makeargs=$2
      shift
      ;;

    -l|--show-log)
      exec 3>&1
      ;;
    -L|--no-show-log)
      echo "Redirecting stdout to ${LOGFILE}"
      exec 3> /tmp/hppmake.log
      ;;
      *)
      # Run make on current package
      echo "============================="
      if [ ${run_pull} != 0 ]; then
        pull "${src_dir}/$1"
      fi
      if [ ${run_co} != 0 ]; then
        checkout "$1"
      fi
      if [ ${run_cmake} != 0 ]; then
        configure "${src_dir}/$1" "${src_dir}/$1/$BDIR"
      fi
      compile "${src_dir}/$1/$BDIR"
      echo "============================="
      show_usage=false
      ;;
  esac
  shift # past argument or value
done

if $show_usage ; then
  usage
  exit 1
fi
