#!/bin/bash
##############################################################################
# Copyright (c) 2016-25, Lawrence Livermore National Security, LLC and Umpire
# project contributors. See the COPYRIGHT file for details.
#
# SPDX-License-Identifier: (MIT)
##############################################################################
replay_tests_dir=$1
tools_dir=$2
testprogram=$replay_tests_dir/replay_tests
diffprogram=$tools_dir/replaydiff
replayprogram=$tools_dir/replay
topdir=$tools_dir/..

function cleanupandexit {
  mydir=`pwd`
  cd $topdir
  for f in `find . -name '*.replay.bin' ; find -name '*.stats'`;
  do
    rm -f $f
  done
  cd $mydir
  exit $1
}

#
# The following program will generate a file of Umpire activity that
# will be replayed.
#
echo "UMPIRE_REPLAY='On' $testprogram"
UMPIRE_REPLAY="On" $testprogram
if [ $? -ne 0 ]; then
    echo "Failed: Unable to run $testprogram"
    cleanupandexit 1
fi

echo $diffprogram -q --recompile $replay_tests_dir/test_output.good umpire.*.0.stats
$diffprogram -q --recompile $replay_tests_dir/test_output.good umpire.*.0.stats
if [ $? -ne 0 ]; then
    echo "Diff failed on file generated by test"
    cleanupandexit 1
fi

echo "/bin/mv umpire*stats replay.replay"
/bin/mv umpire*stats replay.replay
echo "UMPIRE_REPLAY='On' $replayprogram -q --recompile -i replay.replay"
UMPIRE_REPLAY="On" $replayprogram -i replay.replay  -q --recompile
if [ $? -ne 0 ]; then
    echo "$replayprogram Failed"
    cleanupandexit 1
fi

echo "$diffprogram -q --recompile $replay_tests_dir/test_output.good umpire.*.0.stats"
$diffprogram -q --recompile $replay_tests_dir/test_output.good umpire.*.0.stats
if [ $? -ne 0 ]; then
    echo "Diff failed on file generated by replay tool"
    cleanupandexit 1
fi

cleanupandexit 0
