#!/bin/bash
#
# $Id: build_deps 5714 2019-02-13 15:51:08Z idculv $
#
#---------------------------------------------------------------------
# (Re)build ROPP dependency packages using a particular compiler
# Usage: > build_deps <compiler> [package...]
# where <compiler> is the (required) compiler ID (ifort, nagfor, gfortran...) 
# and package is none, one or more (optional) supported
# packages (default: mobufr netcdf netcdff grib sofa - assuming zlib & hdf5
# are system-installed and mobufr is preferred over ecbufr)
#---------------------------------------------------------------------
#
if [[ -z $1 ]] || [[ "$1" == "-h" ]] || \
   [[ "$1" == "--help" ]]; then
  echo
  echo "Purpose:  build ROPP dependency packages"
  echo
  echo "Usage:    build_deps <compiler> [package...]"
  echo
  echo "Required: F95 <compiler> tag (e.g. ifort, nagfor, gfortran...)"
  echo
  echo "Options:  A list of none, one or more supported packages"
  echo
  echo "Default:  mobufr netcdf netcdff grib sofa"
  echo
  echo "Target:   ROPP_ROOT, which defaults to the directory containing this script."
  echo
  echo "This script saves a log file to ROPP_ROOT/logs/build_deps.log or if"
  echo "compiler(s) are given on the command line (explicitly or with 'all')"
  echo "then the targets are ROPP_ROOT/<compiler> and"
  echo "ROPP_ROOT/<compiler>/build_deps.log"
  echo
  exit 1
fi
#
TOP=$(dirname $0)
if [[ $TOP == "." ]]; then
  TOP=$PWD
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="mobufr netcdf netcdff grib sofa";
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 dependency packages with $fc on $(date)"         | tee -a $log
echo "Install target: $tgtdir"                                       | tee -a $log
echo "Logging to:     $log"                                          | tee -a $log
test -n "$ROPP_BUILD_DEPS_PATH" && { echo "cd $ROPP_BUILD_DEPS_PATH"; cd $ROPP_BUILD_DEPS_PATH || exit; }
for pack in $packlist; do
  PACK=$(echo $pack | tr [:lower:] [:upper:])
  echo "---$PACK------------------------------------------------"    | tee -a $log
  ${TOP}/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

