#!/bin/bash

# Run a restart test with the time manager, to make sure that the
# sequence of times is the same for (a) a run that goes all the way
# through, and (b) the same run broken up into two pieces. 
#
# If the test succeeds (i.e., the restart case gives the same time
# sequence as the run all the way through), then there will be no
# output from this script. If there are some differences, these
# differences will be output to stdout.
#
# This requires that the namelists are already set up for three cases:
# (1) the run all the way through
# (2) the first ('initial') run of the restart test
# (3) the second ('continue') run of the restart test
#
# Usage: restart_test testname
# where testname is the name of a directory in the inputs directory
# The following directories must be set up in the inputs directory:
# (1) testname: inputs for the run that goes all the way through
# (2) testname.restart1: inputs for the first ('initial') run of the
# restart test
# (3) testname.restart2: inputs for the second ('continue') run of the
# restart test

# ----------------------------------------------------------------------
# SET PARAMETERS HERE
# ----------------------------------------------------------------------

testprog=./time_management_test

# ----------------------------------------------------------------------
# DONE SETTING PARAMETERS
# ----------------------------------------------------------------------


if [[ $# -ne 1 ]]; then
    echo "Expect one argument: testname"
    exit 1
fi

testname=$1

# ----------------------------------------------------------------------
# RUN TEST DRIVER ON THE THREE CASES
# ----------------------------------------------------------------------

cp inputs/$testname/*_in .
$testprog > test_output.full.$$

cp inputs/${testname}.restart1/*_in .
$testprog > test_output.r1.$$

cp inputs/${testname}.restart2/*_in .
$testprog > test_output.r2.$$


# ----------------------------------------------------------------------
# REMOVE SOME VARIABLES ENDING WITH '_run'
# (we expect differences in these variables)'
# ----------------------------------------------------------------------

for fl in test_output.full.$$ test_output.r1.$$ test_output.r2.$$; do
    cat $fl | grep -v 'nsteps_run *=' | grep -v 'elapsed_days_this_run *=' | grep -v 'elapsed_months_this_run *=' | grep -v 'elapsed_years_this_run *=' > ${fl}_noRun
done

# ----------------------------------------------------------------------
# PROCESS THE R1 & R2 OUTPUT TO REMOVE DUPLICATE INFO
# this includes the initialization section from r2, for example
# ----------------------------------------------------------------------

# Delete the last line (saying "SUCCESSFUL TERMINATION...") from the
# r1 ('initial') case
sed -e '$ d' test_output.r1.$$_noRun > test_output.r1.$$_finalRemoved

# Create an awk script that will print everything after the first
# instance of "END REPORT_TIME " (note the trailing space so we don't
# catch "END REPORT_TIME_INIT"). This will be run on the output of the
# r2 ('continue') case, to throw out the output from initialization.
cat > awk_cmds.$$ <<EOF
BEGIN {found=0}
found >= 1 {print \$0}
/END REPORT_TIME / {found = found+1}
EOF

awk -f awk_cmds.$$ test_output.r2.$$_noRun > test_output.r2.$$_initialRemoved

# ----------------------------------------------------------------------
# COMPARE OUTPUT
# ----------------------------------------------------------------------

# Concatenate the processed output from r1 & r2
cat test_output.r1.$$_finalRemoved test_output.r2.$$_initialRemoved > test_output.rFull.$$

# Compare with the full run
diff test_output.full.$$_noRun test_output.rFull.$$

# ----------------------------------------------------------------------
# CLEANUP
# ----------------------------------------------------------------------

rm test_output.full.$$ test_output.r1.$$ test_output.r2.$$
rm test_output.full.$$_noRun test_output.r1.$$_noRun test_output.r2.$$_noRun
rm test_output.r1.$$_finalRemoved
rm test_output.r2.$$_initialRemoved
rm test_output.rFull.$$
rm awk_cmds.$$