#!/bin/bash
#
# $Id: build_ropp 5117 2016-12-15 15:04:41Z idculv $
#
#---------------------------------------------------------------------
# (Re)build ROPP modules using a particular compiler
# Usage: > build_ropp <compiler> [module...]
# where <compiler> is the (required) compiler ID (default local or ifort,
# nagfor, gfortran...) and module is none, one or more (optional) ROP modules
# (default: ropp_utils ropp_io, ropp_pp ropp_fm ropp_1dvar ropp_apps)
#---------------------------------------------------------------------
#
if [[ -z $1 ]] || [[ "$1" == "-h" ]] || \
   [[ "$1" == "--help" ]]; then
  echo
  echo "Purpose:  build ROPP modules"
  echo
  echo "Usage:    build_ropp <compiler> [package...]"
  echo
  echo "Required: F95 <compiler> tag (e.g. local, ifort, gfortran...)"
  echo
  echo "Options:  A list of none, one or more ROPP modules. Note that"
  echo "          modules must be listed in the correct build order"
  echo
  echo "Default:  all (ropp_utils ropp_io ropp_pp ropp_fm ropp_1dvar ropp_apps)"
  echo "          to target ROPP_ROOT which defaults to the directory"
  echo "          containing this script."
  echo
  echo "This script saves a log file to ROPP_ROOT/logs/build_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>/build_ropp.log"
  echo
  echo "NB: dependency packages must be built before any ROPP modules,"
  echo "for instance with build_deps"
  echo
  exit 1
fi
#
# --- Compiler (required)
#
fc=$1
#
# --- List of packages to build
#
packlist=""
while [[ -n $2 ]]; do packlist="$packlist $2"; shift; done
packlist=${packlist## }
if [[ -z $packlist ]]; then
  packlist="ropp_utils ropp_io ropp_pp ropp_fm ropp_1dvar ropp_apps";
fi
packlist=$(echo $packlist | tr [:upper:] [:lower:])
#
# --- Default installation target path
#
if [[ -z $ROPP_ROOT ]]; then export ROPP_ROOT=$(readlink -fnq ${0%/*}); fi
#
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"
    exit
  fi
  logdir=$ROPP_ROOT/$fc
  tgtdir=$ROPP_ROOT/$fc
fi
mkdir -p $logdir $tgtdir
log=$logdir/${0##*/}.log
rm -f $log
#
echo " " | tee $log
echo "-------------------------------------------------------------" | tee -a $log
echo "Building ROPP modules with $fc on $(date)"                     | tee -a $log
echo "Install target: $tgtdir"                                       | tee -a $log
echo "Logging to:     $log"                                          | tee -a $log
for pack in $packlist; do
  PACK=$(echo $pack | tr [:lower:] [:upper:])
  echo "---$PACK------------------------------------------------"    | tee -a $log
  ./buildpack $pack $fc 2>&1 5>&1 6>&1                               | tee -a $log
  errstat=${PIPESTATUS[0]}
  if [[ $errstat -ne 0 ]]; then break; fi
done
echo "-------------------------------------------------------------" | tee -a $log
echo "See $log for build status"
echo
#
exit $errstat

