#!/bin/bash
#
# $Id: buildhdf5_ropp 50 2016-03-05 21:49:02Z frdo $
#
#---------------------------------------------------------------------
# Build HDF5 package in ROPP_ROOT tree using (by default)
# a locally-defined compiler, or if 'all' a set of F95
# compilers available on MetO Linux desktops,
# or a sub-set specified in a list on the command line
# e.g.:
#  > ./buildhdf5_ropp ifort gfortran
#
#---------------------------------------------------------------------
#
dtstamp(){
  echo "$(date --utc +"%d-%b-%Y %H:%M") UT"
}
elapsed(){
  local now=$(date "+%s")
  local s=$(($now-$1))
  local m=$(($s/60))
  local h=$(($s/3600))
  m=$(($m%60)); s=$(($s%3600%60))
  printf "%2.2d:%2.2d:%2.2d\n" $h $m $s
}
#
# --- Help
#
if [[ "$1" == "-h" ]] || \
   [[ "$1" == "--help" ]]; then
  echo
  echo "Purpose: build HDF5 for ROPP"
  echo
  echo "Usage:   buildhdf5_ropp [compiler ...]"
  echo
  echo "Options: one or more F95 compiler tags or 'all' for a set of"
  echo "         compilers available on Met Office Linux systems"
  echo
  echo "Default: build with a locally-defined compiler to target ROPP_ROOT"
  echo "         which defaults to the directory containing this script."
  echo
  echo "This script saves a log file to ROPP_ROOT/logs/buildhdf5_ropp.log or if"
  echo "compiler(s) are given on the command line (explicitly or with 'all')"
  echo "then the targets are ROPP_ROOT/<compiler> &"
  echo "ROPP_ROOT/<compiler>/buildhdf5_ropp.log"
  echo
  exit
fi
#
# --- List of Fortran compilers to use
#
fclist=""
while [[ -n "$1" ]]; do fclist="$fclist $1"; shift; done
fclist=${fclist## }
if [[ -z $fclist ]]; then
  fclist="local"
elif [[ $fclist == "all" ]]; then
  fclist="gfortran \
          ifort ifort13 ifort15 ifort16 \
          nagfor nagfor53 nagfor60 \
          pgf95 pgf15 \
          sunf95"
fi
#
# --- Default installation target path
#
if [[ -z $ROPP_ROOT ]]; then export ROPP_ROOT=$(readlink -fnq ${0%/*}); fi
#
echo "=========================== HDF5 Builder ============================"
#
# --- Build package with each compiler in the list, with
#     output to screen & a log file
#
sepline="---------------------------------------------------------------------"
errstat=0
loglist=""
for fc in $fclist; do
  if [[ $fc == "local" ]]; then
    logdir=$ROPP_ROOT/log
    tgtdir=$ROPP_ROOT
  else
    which $fc > /dev/null 2>&1
    if [[ $? -ne 0 ]]; then
      echo "*** Compiler $fc not installed"
      continue
    fi
    logdir=$ROPP_ROOT/$fc
    tgtdir=$ROPP_ROOT/$fc
  fi
  mkdir -p $logdir $tgtdir
  log=$logdir/${0##*/}.log
  rm -f $log
  loglist="$loglist $log"
  echo " "                                                           | tee $log
  echo $sepline | tee -a $log
  echo "Building HDF5 package on $(dtstamp) host $(hostname) by $USER" | tee -a $log
  echo $sepline | tee -a $log
  start_time=$(date "+%s")
  echo ">> Compiler: $fc"                                            | tee -a $log
  echo "Install target: $tgtdir"                                     | tee -a $log
  echo "Logging to:     $log"                                        | tee -a $log
  ./buildpack hdf5 $fc 2>&1 5>&1 6>&1                                | tee -a $log
  errstat=${PIPESTATUS[0]}
  echo $sepline | tee -a $log
  echo ">> Finished at: $(dtstamp)  Elapsed: $(elapsed $start_time)" | tee -a $log
  echo $sepline | tee -a $log
  if [[ $errstat -ne 0 ]]; then break; fi
done
#
echo
echo "====================================================================="
echo "For a record of the build status, see log files:"
for log in $loglist; do
  echo $log
done
echo
#
exit $errstat
