#!/bin/bash
# Copyright(C) 1999-2025 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 sierra-help@sandia.gov for epup support.<-<-<-
      or enter issue at https://github.com/sandialabs/seacas/issues
   Uses: GNU Parallel,
   	 O. Tange (2018): GNU Parallel 2018, Mar 2018, ISBN 9781387509881,
	 DOI https://doi.org/10.5281/zenodo.1146014

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}/getopt.seacas ]; then
    ACCESS_BIN=$basedir
elif [ "$ACCESS" == "" ]; then
    ACCESS_BIN=/clangarm64/bin
else
    ACCESS_BIN=${ACCESS}/bin
fi

if command -v ${basedir}/parallel >/dev/null 2>&1; then
    PARALLEL=$basedir/parallel
elif command -v parallel >/dev/null 2>&1; then
    PARALLEL=parallel
elif command -v ${ACCESS_BIN}/parallel >/dev/null 2>&1; then
    PARALLEL=${ACCESS_BIN}/parallel
else
    echo "ERROR: Could not find an executable named \"parallel\" for use with the script"	
    exit 1
fi

EPU=${ACCESS_BIN}/epu
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
