#!/bin/tcsh
#
# Test the program in $1.  Optional $2 is "-v" for verbose output, "-vv" for
# very, or "-vvv" for very very verbose, or "-n" for no output.  The last of
# these values is used by run.csh, so the overall regression is based on the
# known differences of outputs from program versions that are known to be
# purposely erroneous, e.g., stats-wrong.c.
#

#
# Set the name of the program to be tested, which is in the parent dir.
#
set prog = ../$1

#
# Set a prog name var and shift off the prog name, in case there's a "-v..."
# arg.
#
set prog_name = $1
shift

#
# Make the prog-name output and diffs subsubdirs if necessary, or clear them
# out if they exist.
#
if (! -e output/$prog_name) then
    mkdir output/$prog_name
else
    if (`ls output/$prog_name` != "") then
        rm -f output/$prog_name/*
    endif
endif

if (! -e diffs/$prog_name) then
    mkdir diffs/$prog_name
else
    if (`ls diffs/$prog_name` != "") then
        rm -rf diffs/$prog_name/*
    endif
endif

#
# Define a couple vars for ease on the eyes in the code that follows.
#
set output = output/$prog_name
set diffs = diffs/$prog_name

#
# Iterate through all of the files in the input subdir.
#
foreach f (input/*)

    #
    # Set the root filename, for use as the common filename in all four testing
    # subdirs -- input, output, expected-output, and diffs.
    #
    set root_file = $f:t

    #
    # Run the program on this input, putting output in ../output.
    #
    $prog < $f >& $output/$root_file
    if ($1 == "-vv") then
        echo "$prog < $f >& $output/$root_file"
    endif

    #
    # Difference actual and expected outputs.  If no diffs, say nothing.  If
    # diffs, output a reasonably understandable message.
    # 
    diff $output/$root_file expected-output/$root_file >& $diffs/$root_file
    if ($1 == "-vv") then
        echo "diff $output/$root_file expected-output/$root_file >& $diffs/$root_file"
    endif

    if (-z $diffs/$root_file) then
        rm $diffs/$root_file
    else
        if ($1 != "-n") then
            echo
            echo "For $f, $prog_name output differs from the expected output."
            echo "    See the file $diffs/$root_file for details."
        endif
    endif

end
