#!/bin/sh

#
# this script does not expect to be run directly by the user.
# it is 'exec'ed by the test programs 'test_nnn'
#

#
# entered with the following environment:
#
# Test              = The test name
# Synopsis          = A one line test description
# Description       = A longer test description
# ExitStatus        = The expected icmchk1 exit status
# Counters          = The expected icmchk1 message counters
# Output            = The expected icmchk1 output
# ICM               = The ICM test data
#
# $*                = whatever arguments were passed
#
# if a test fails, the script exits with status 1, otherwise 0
#

#
# NOTE - This script normally expects to function within
#        the default working directory, where it will 
#        create a number of working files that are
#        automatically cleaned when done.
#
#        Use 'extract' if you wish to review these files.
#

write_icm_file ()
{
rm -f ${Test}.icm
grep -v '^EOF$' <<EOF >${Test}.icm
${ICM}
EOF
}

write_out_file ()
{
rm -f ${Test}.out
grep -v '^EOF$' <<EOF >${Test}.out
${Output}
EOF
}

write_extra_files ()
{
if [ -n "${ExtraFiles}" ]
then
    touch ${ExtraFiles}
fi
}

extract_files ()
{
    write_icm_file
    write_out_file
    write_extra_files
}

print_description ()
{
grep -v '^EOF$' <<EOF
${Description}
EOF
}

clean_up ()
{
    rm -f ${Test}.icm ${Test}.tst ${Test}.out
    if [ -n "${ExtraFiles}" ] ; then
	rm -f ${ExtraFiles}
    fi
}

brief_test ()
{
#
# for a v1.0 parse with a v1.1 parser, we must force the version
#
    if [ -n "${ICM_Version}" ] ; then
        icmchk_version="${ICM_Version}"
    else
	icmchk_version="1.0"
    fi

    case ${icmchk_version} in
	1.0) icmchk_options="-f${icmchk_version}" ;;
        *)   icmchk_options="" ;;
    esac

#
# run a test and produce a one line summary
#
    extract_files
    
    rm -f ${Test}.tst
    echo -n "${Test}->"
    ${exedir}/icmchk1 ${icmchk_opts} ${icmchk_options} ${Test}.icm >${Test}.tst 2>&1; ES=$?
    echo -n "[${ES}]"
    grep "${Counters}" ${Test}.out >/dev/null 2>&1
    if [ ${ExitStatus} -eq ${ES} ] ; then
	echo -n "(P) Diff"
    else 
	echo -n "(F) Diff"
    fi
    if [ -f ${Test}.out ] ; then
	diff ${Test}.out ${Test}.tst >/dev/null 2>&1; DS=$?
	if [ 0 -eq ${DS} ] ; then 
	    echo -n "(P)"
	else 
	    echo -n "(F)"
	fi
    else 
	echo -n "(-)"
	DS=1
    fi
    if [ ${ExitStatus} -ne ${ES}  -o  0 -ne ${DS} ] ; then
	echo -n ": FAIL"
	ScriptStatus=1
    else
	echo -n ": PASS"
	ScriptStatus=0
    fi
    echo ' <'"${Synopsis}"'>'
}

full_test ()
{
    echo
    echo "--------- Test Description -----------------------------------------------------------------"
    print_description
    echo "--------- Result ---------------------------------------------------------------------------"
    brief_test
    echo "--------- Expected Output ------------------------------------------------------------------"
    cat ${Test}.out
    echo "--------- Actual Output --------------------------------------------------------------------"
    cat ${Test}.tst
    echo "============================================================================================"
    echo
}

silent_test ()
{
    brief_test $1 >/dev/null 2>&1
}

check_test ()
{
    silent_test GNUCheck
    if [ ${ScriptStatus} -ne 0 -a ${ScriptStatus} -ne 77 ] ; then
	full_test
    fi
}

capture_test ()
{

#
# write a new test file, replacing the original
#

#
# capture fresh results
#

silent_test

ExitStatus="${ES}"
Counters=`grep "^Finished\." ${Test}.tst | sed -e 's,^Finished\. *,,'`
Output=`cat ${Test}.tst`
date=`date`

write_test_program

}

write_test_program ()
{

#
# write a new test file
#

rm -f ${Test}.tmp
grep -v '^EOF$' <<EOF >${Test}.tmp
#!/bin/sh
#
# Test results last captured at "${date}"
#
# This is an IBIS icm test program
#
# It is a self-contained package of all of the 
# files and data required to run an icm parser test.
#
# Usage:  ${srcdir}/${Test}  <arg>
#
# <nothing>               = Run the test silently in GNU check mode
# "Brief"                 = Run the test.
#                           If there is no error, produce a one line summary
#                           Otherwise, produce a full test report.
# "Full"                  = Run the test and always produce a full test report
# "Capture"               = Capture test results and REWRITE the test program
# "Extract"               = Extract the .icm and .out files
#

Test=\`basename \$0\`
Synopsis="${Synopsis}"
ExitStatus="${ES}"
Counters="${Counters}"
ExtraFiles="${ExtraFiles}"
EOF

grep -v '^EOF2$' <<EOF2 >>${Test}.tmp

Description=\`grep -v '^EOF\$' <<'EOF'
${Description}
EOF\`
EOF2

grep -v '^EOF2$' <<EOF2 >>${Test}.tmp

Output=\`grep -v '^EOF\$' <<'EOF'
${Output}
EOF\`
EOF2

rm -f ${Test}_icm.tmp

grep -v '^EOF$' <<EOF | sed -e 's,\\,\\\\,g;s,`,\\`,g' >${Test}_icm.tmp
${ICM}
EOF

grep -v '^EOF2$' <<EOF2 >>${Test}.tmp

ICM=\`grep -v '^EOF\$' <<'EOF'
`cat ${Test}_icm.tmp`
EOF\`
EOF2

rm -f ${Test}_icm.tmp
grep -v '^EOF$' <<'EOF' >>${Test}.tmp

#
# we are either running the test directly in the test directory
# or are running make check.  make sets the srcdir variable
# so we can tell the difference easily
#

if [ -z "${srcdir}" ]
then
    srcdir="."
    exedir="../../src"
else
    srcdir="${srcdir}/v_1_0"
    exedir="../src"
fi

export srcdir exedir Test Synopsis ExitStatus Counters ExtraFiles Description Output ICM

exec ${srcdir}/run_test "$@"
EOF

mv -f ${Test}.tmp ${Test}
chmod a+x ${Test}

}

#
# start here
#

#
# make sure the source directory is defined
# and that the working directory is writeable.
#

if [ -z "${srcdir}" ]
then
    srcdir="."
    exedir="../../src"
fi

touch ${Test}.foo >/dev/null 2>&1 || {
  echo "The working directory is not writeable."
  exit 1;
}

rm -f ${Test}.foo

#
# Do what was asked.
# The default is to operate silently unless there is some error
#

case ZZ_$1 in
    ZZ_)              check_test;    clean_up; exit ${ScriptStatus}; ;;
    ZZ_[Bb][Rr]*)     brief_test;    clean_up; exit ${ScriptStatus}; ;;
    ZZ_[Ff][Uu]*)     full_test;     clean_up; exit ${ScriptStatus}; ;;
    ZZ_[Cc][Aa]*)     capture_test;  clean_up; exit ${ScriptStatus}; ;;
    ZZ_[Ee][Xx]*)     extract_files;           exit 0;             ;;
esac

exit 0
