#! /bin/ksh

if [[ "$#" != 5 ]] ; then
    cat<<EOF 1>&2
This is a helper script for the HWRF automatic cycle checker scripts.
It implements part of their report_and_exit function, printing out the
top portion of the output that is common to all scripts.

It assumes the following variables are set from the holdvars file:
  \$STORM \$STORMID \$basin \$NPROCS_a \$YYYYMMDDHH

Arguments:
  \$1 -- path to com directory
  \$2 -- path to data directory
  \$3 -- failure listing
  \$4 -- warning listing
  \$5 -- informational message listing

Author: Sam Trahan, October 2010

ERROR: SCRIPT REQUIRES FIVE ARGUMENTS
EOF
    exit 1
fi

F_COM="$1"
F_DATA="$2"
v_information="$3"
v_warning="$4"
v_failure="$5"

if [[ ! -z "$v_failure" ]] ; then
    echo '# ======================= ERROR: FAILURES DETECTED ========================='
elif [[ ! -z "$v_warning" ]] ; then
    echo '# ======================== COMPLETE WITH WARNINGS =========================='
else
    echo '# ======================== SUCCESSFUL COMPLETION ==========================='
fi

if [[ ! -z "$v_information" ]] ; then
    echo "# "
    if [[ ! -z "$v_failure" ]] ; then
        echo "$v_failure" | fold -s | sed 's,^,# ,g'
    elif [[ ! -z "$v_warning" ]] ; then
        echo "# "
        echo "$v_warning" | fold -s | sed 's,^,# ,g'
    else
        echo "Information only, not an error: $v_information" | fold -s | sed 's,^,# ,g'
    fi
    echo "# "
    echo '# =========================================================================='
fi

    cat<<EOF

COMDIR="$F_COM"
DATADIR="$F_DATA"

STORM="$STORM"
STORMID="$STORMID"
BASIN="$basin"
NPROCS_a="$NPROCS_a"
YYYYMMDDHH="$YYYYMMDDHH"

# Shell versions of the human-readable warning and error messages:
INFORMATION_MESSAGES="$v_information"
WARNING_MESSAGES="$v_warning"
FAILURE_MESSAGES="$v_failure"

EOF
