#!/bin/bash

if [ $# -ge 0 ]; then

  if [ $# -ge 3 ]; then
    echo "too many arguments."
    echo "usage: $0 prefix [nCpu]"
    exit
  fi

  prefix=$PWD
  [ $# -ge 1 ] && prefix=$1
  nCpu=1
  [ $# -eq 2 ] && nCpu=$2
  echo "using prefix $prefix and $nCpu CPU(s)"

  ./autogen.sh
  autoreconf -v --force --install -Wall

  mkdir -v build
  cd build
  # BOOST_ROOT set by `module load boost/1.54.0` in ~/.login works on PDSF
  boostopts='--with-boost'
  [ ! -z $BOOST_ROOT ] && boostopts=$boostopts"=$BOOST_ROOT"
  boostopts=$boostopts' --with-boost-filesystem --with-boost-system'
  boostopts=$boostopts' --with-boost-regex --with-boost-program-options'
  ../configure --prefix=$prefix $boostopts
  make -j $nCpu CXXFLAGS="-Wall -Werror"
  make install

  echo "===> ckon build done."

else
  echo "usage: $0 [prefix [nCpu]]"
  echo 'prefix = install dir [default=$PWD]'
  echo "nCpu = #cpu's for parallel build (-j) [default=1]"
fi
