#!/bin/bash
# Copyright(C) 1999-2020 National Technology & Engineering Solutions
# of Sandia, LLC (NTESS).  Under the terms of Contract DE-NA0003525 with
# NTESS, the U.S. Government retains certain rights in this software.
#
# See packages/seacas/LICENSE for details

########################################################################
function usage {
cat <<EPU_USAGE_EOF
Usage:  epup --subcycle <cycle_count> ...normal epu options...

   Runs multiple copies of epu simultaneously to create <cycle_count>
   sub-files from the original files.

   ->->-> Send email to gdsjaar@sandia.gov for epup support.<-<-<-

EPU_USAGE_EOF
exit 1
}

########################################################################
function execute_epu {
    cycles=`expr $1 - 1`
    all_options=$2

    $PARALLEL --will-cite -u "$EPU -cycle {} $all_options" ::: $($SEQ 0 $cycles)

    epu_rc=$?
    return $epu_rc
}

########################################################################
# initialize variables
# Text color variables
txtund=$(tput sgr 0 1)    # Underline
txtbld=$(tput bold)       # Bold
txtred=$(tput setaf 1)    # Red
txtgrn=$(tput setaf 2)    # Green
txtylw=$(tput setaf 3)    # Yellow
txtblu=$(tput setaf 4)    # Blue
txtpur=$(tput setaf 5)    # Purple
txtcyn=$(tput setaf 6)    # Cyan
txtwht=$(tput setaf 7)    # White
txtrst=$(tput sgr0)       # Text reset

cycles=-1

pushd $(dirname "${0}") > /dev/null
basedir=$(pwd -P)
popd > /dev/null
if [ -x ${basedir}/epu -a -x ${basedir}/parallel -a -x ${basedir}/getopt.seacas ]; then
    ACCESS_BIN=$basedir
elif [ "$ACCESS" == "" ]; then
    ACCESS_BIN=@ACCESSDIR@/bin
else
    ACCESS_BIN=${ACCESS}/bin
fi

EPU=${ACCESS_BIN}/epu
PARALLEL=${ACCESS_BIN}/parallel
SEQ="seq -w"
if [ $# -eq 0 ] ; then
  usage
fi

########################################################################
# epu options:
GETOPT=${ACCESS_BIN}/getopt.seacas

all_options="$@"

TEMP=`${GETOPT} -q -o hs:f -a \
    --long help,subcycle: \
    -n 'epup' -- "$@"`

# Note the quotes around `$TEMP': they are essential!
eval set -- "$TEMP"
while true ; do
	case "$1" in
	    -h|--help)
	      usage ; shift ;;
	    -s|--subcycle)
	      cycles="$2" ; shift 2 ;;
	    --) shift ; break ;;
	esac
done

########################################################################
if [ $# -eq 0 ] ; then
    echo "${txtred}ERROR:  No epu options specified${txtrst}"
    usage
fi

execute_epu "$cycles" "$all_options"
if [ $? -ne 0 ]
then
    echo "${txtred}ERROR     During epu execution. Check error output above and rerun${txtrst}"
    exit 1
else
    echo "${txtgrn}...epup successful execution${txtrst}"
fi
