#!/bin/sh
#
# Copyright by  The HDF Group (THG) and
#               The Board of Trustees of the University of Illinois.
# All rights reserved.
#
# This file is part of H4H5TOOLS. The full H4H5TOOLS copyright notice,
# including terms governing use, modification, and redistribution, is
# contained in the files COPYING and Copyright.html.  COPYING can be found
# at the root of the source code distribution tree; Copyright.html can be
# found at the root level of an installed copy of the electronic H4H5TOOLS
# document set and is linked from the top-level documents page.  It can
# also be found at http://www.hdfgroup.org/H4H5TOOLS/doc/Copyright.html.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#
#  
# Test script for the h4toh5 tests.
# Using the h4toh5 convert to convert a pre-created hdf file to
# an hdf5 file (output file), then compare it with a pre-created
# corresponding hdf5 file (expected file).
# If the same, that particular test passes.
# If not the same, the output file and expected file are processed
# by the h5dump tool to see if they produce the same results.
# If the same, the test passes.
# If not, show the difference of the two results and report the test failed.
#
# h5dump is default to use the one just built.  It can be overridden
# by setting $H5DUMP to a different value such as /usr/local/bin/h5dump.

#currently only add h5dump path in this file
h5path=@HDF5PATH@
export LD_LIBRARY_PATH=@LD_LIBRARY_PATH@
H4TOH5DEF=h4h5apitestdefault              
H4TOH5NAM=h4h5apitestname                 
H4TOH5SDS=h4h5apitestsds                  
H4TOH5VDATA=h4h5apitestvdata              
H4TOH5BASVGROUP=h4h5apitestbasvgroup      
H4TOH5ADVGROUP=h4h5apitestadvgroup
H4TOH5IMAGE=h4h5apitestimage            
H4TOH5IMAGEATTR=h4h5apitestimageattr
H4TOH5SDSATTR=h4h5apitestsdsattr
H4TOH5IMAGELONE=h4h5apitestloneimage
H4TOH5SDSLONE=h4h5apitestlonesds
H4TOH5VDATALONE=h4h5apitestlonevdata

srcdir=@srcdir@

#H4TOH5_BIN=`pwd`/$H4TOH5DEF

CMP='cmp -s'
DIFF='diff -c'
CP='cp'
DIRNAME='dirname'
LS='ls'
AWK='awk'

RM='rm -f'
SED='sed '
H5DIFF="$h5path/h5diff"

# Verify if $H5DUMP is a valid command.
tmpfile=/tmp/testh4toh5.$$
$H5DIFF -h > $tmpfile
if test -s "$tmpfile"; then
    :
else
    echo "    Could not run the '$H5DIFF' command. The test can still proceed"
    echo "    but it may fail if '$H5DIFF' is needed to verify the output."
    echo "    You can make sure '$H5DIFF' is among your shell PATH and run"
    echo "    the test again. You may also visit http://hdf.ncsa.uiuc.edu"
    echo "    or email hdfhelp@ncsa.uiuc.edu for more information."
    H5DIFF=:
fi
$RM $tmpfile

# The build (current) directory might be different than the source directory.
if test -z "$srcdir"; then
    srcdir=.
fi

mkdir ../testfiles >/dev/null 2>&1

SRCDIR="$srcdir/../testfiles"
TESTDIR="../testfiles"
OUTDIR="../testfiles/Results"

test -d "$OUTDIR" || mkdir -p $OUTDIR

nerrors=0
verbose=yes

######################################################################
# test files
# --------------------------------------------------------------------
# All the test files copy from source directory to test directory
# NOTE: Keep this framework to add/remove test files.
#       Any test files from other tools can be used in this framework.
#       This list are also used for checking exist.
#       Comment '#' without space can be used.
# --------------------------------------------------------------------
LIST_HDF_TEST_FILES="
$SRCDIR/image_lib_lonetest.hdf
$SRCDIR/image_lib_test.hdf
$SRCDIR/sds_lib_lonetest.hdf
$SRCDIR/sds_lib_test.hdf
$SRCDIR/testallvgroup.hdf
$SRCDIR/vdata_lib_lonetest.hdf
$SRCDIR/vdata_lib_test.hdf
$SRCDIR/vg_all_test.hdf
$SRCDIR/vg_all_testname.hdf
$SRCDIR/vg_bas_test.hdf
"
#
# copy test files and expected output files from source dirs to test dir
#
COPY_TESTFILES="$LIST_HDF_TEST_FILES"

COPY_TESTFILES_TO_TESTDIR()
{
    # copy test files. Used -f to make sure get a new copy
    for tstfile in $COPY_TESTFILES
    do
        # ignore '#' comment
        echo $tstfile | tr -d ' ' | grep '^#' > /dev/null
        RET=$?
        if [ $RET -eq 1 ]; then
            # skip cp if srcdir is same as destdir
            # this occurs when build/test performed in source dir and
            # make cp fail
            SDIR=`$DIRNAME $tstfile`
            INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
            INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
            if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
                $CP -f $tstfile $TESTDIR
                if [ $? -ne 0 ]; then
                    echo "Error: FAILED to copy $tstfile ."

                    # Comment out this to CREATE expected file
                    exit $EXIT_FAILURE
                fi
            fi
        fi
    done
}

CLEAN_TESTFILES_AND_TESTDIR()
{
    # skip rm if srcdir is same as destdir
    # this occurs when build/test performed in source dir and
    # make cp fail
    SDIR=$SRCDIR
    INODE_SDIR=`$LS -i -d $SDIR | $AWK -F' ' '{print $1}'`
    INODE_DDIR=`$LS -i -d $TESTDIR | $AWK -F' ' '{print $1}'`
    if [ "$INODE_SDIR" != "$INODE_DDIR" ]; then
        $RM -r $TESTDIR
    fi
}


# Print a line-line message left justified in a field of 70 characters
# beginning with the word "Testing".
TESTING() {
    SPACES="                                                               "
    echo "Testing $* $SPACES" |cut -c1-70 |tr -d '\012'
}

# Run a test and print PASS or *FAIL*.  If a test fails then increment
# the `nerrors' global variable and (if $verbose is set) display the
# difference between the actual and the expected hdf4 files. The
# expected hdf5 files are in testfiles/Expected directory.
# The actual hdf5 file is not removed if $H4H5_NOCLEANUP is set to a non-null
# value.

CONVERT() {
    #
    # Set up arguments to run the conversion test.
    # The converter assumes all hdf4 files has the .hdf suffix as in the form
    # of foo.hdf.  It creates the corresponding hdf5 files with the .h5 suffix
    # as in the form of foo.h5.  One exception is that if exactly two file
    # names are given, it treats the first argument as an hdf4 file and creates
    # the corresponding hdf5 file with the name as the second argument, WITOUT
    # any consideration of the suffix.  (For this test script, in order to
    # match the output hdf5 file with the expected hdf5 file, it expects the
    # second file of the two-files tests has the .h5 suffix too.)
    #
    # If SRCDIR != OUTDIR, need to copy the input hdf4 files from the SRCDIR
    # to the OUTDIR and transform the input file pathname because of the suffix
    # convention mentioned above.  This way, the hdf5 files are always created
    # in the OUTDIR directory.
    #

    INFILES=""
    OUTFILES=""
    MULTIRUN=""

    case "$1" in
    -m)		# multiple files conversion
	MULTIRUN="-m"
	shift
	for f in $*
	do
	    if test "$SRCDIR" != "$OUTDIR"; then
		cp $SRCDIR/$f $OUTDIR/$f
	    fi
	    INFILES="$INFILES $f"
	    OUTFILES="$OUTFILES `basename $f .hdf`.h5"
	    shift
	done
	;;

    *)			# Single file conversion
	case $# in
	1)  if test "$SRCDIR" != "$OUTDIR"; then
		cp $SRCDIR/$1 $OUTDIR/$1
	    fi
	    INFILES="$1"
	    OUTFILES="`basename $1 .hdf`.h5"
	    ;;

	2) 		# hdf4 file specified
	    if test "$SRCDIR" != "$OUTDIR"; then
		cp $SRCDIR/$1 $OUTDIR/$1
	    fi
	    INFILES="$1"
	    OUTFILES="$2"
	    ;;

	*)		# Illegal
	    echo "Illegal arguments"
	    exit 1
	    ;;
	esac
	;;
    esac

    # run the conversion and remove input files that have been copied over
    (
	cd $OUTDIR
	$H4TOH5_BIN $INFILES $OUTFILES 2>/dev/null
	if test "$SRCDIR" != "$OUTDIR"; then
	    $RM $INFILES
	fi
    )

    # Verify results
    result="passed"
    for f in $OUTFILES
    do
	if $CMP $SRCDIR/Expected/$f $OUTDIR/$f
	then
	    :
	else
	    # Use h5diff to compare the HDF5 files and verify the output.
	    outfile=`basename $f .h5`
	    expect_out=$outfile.expect
	    actual_out=$outfile.actual

            $H5DIFF $SRCDIR/Expected/$outfile.h5 $OUTDIR/$outfile.h5
            RET=$?
            if [ $RET != 0 ] ; then
             echo "*FAILED*"
             nerrors="`expr $nerrors + 1`"
            else
             echo " PASSED"
            fi
        fi

	# Clean up output file
	if test -z "$H4H5_NOCLEANUP"; then
	    $RM $expect_out $actual_out
	    $RM $OUTDIR/$f
	fi
    done
}

CONVERT1() {
    # Run h4toh5 convert.
    TESTING $H4TOH5DEF 
    H4TOH5_BIN=`pwd`/$H4TOH5DEF 
    CONVERT vg_all_test.hdf vg_all_test.h5
}
CONVERT2() {
    # Run h4toh5 convert.
    TESTING $H4TOH5NAM 
    H4TOH5_BIN=`pwd`/$H4TOH5NAM 
    CONVERT vg_all_testname.hdf vg_all_testname.h5
}
CONVERT3() {
    # Run h4toh5 convert.
    TESTING $H4TOH5SDS 
    H4TOH5_BIN=`pwd`/$H4TOH5SDS 
    CONVERT sds_lib_test.hdf sds_lib_test.h5
}
CONVERT4() {
    # Run h4toh5 convert.
    TESTING $H4TOH5IMAGE 
    H4TOH5_BIN=`pwd`/$H4TOH5IMAGE 
    CONVERT image_lib_test.hdf image_lib_test.h5
}
CONVERT5() {
    # Run h4toh5 convert.
    TESTING $H4TOH5VDATA 
    H4TOH5_BIN=`pwd`/$H4TOH5VDATA 
    CONVERT vdata_lib_test.hdf vdata_lib_test.h5
}
CONVERT6() {
    # Run h4toh5 convert.
    TESTING $H4TOH5IMAGEATTR 
    H4TOH5_BIN=`pwd`/$H4TOH5IMAGEATTR 
    CONVERT image_lib_test.hdf image_lib_testattr.h5
}
CONVERT7() {
    # Run h4toh5 convert.
    TESTING $H4TOH5SDSATTR
    H4TOH5_BIN=`pwd`/$H4TOH5SDSATTR 
    CONVERT sds_lib_test.hdf sds_lib_testattr.h5
}

CONVERT8() {
    # Run h4toh5 convert.
    TESTING $H4TOH5BASVGROUP
    H4TOH5_BIN=`pwd`/$H4TOH5BASVGROUP
    CONVERT vg_bas_test.hdf vg_bas_test.h5
}
CONVERT9() {
    # Run h4toh5 convert.
    TESTING $H4TOH5ADVGROUP
    H4TOH5_BIN=`pwd`/$H4TOH5ADVGROUP
    CONVERT testallvgroup.hdf testallvgroup.h5
}
CONVERT10() {
    # Run h4toh5 convert.
    TESTING $H4TOH5IMAGELONE
    H4TOH5_BIN=`pwd`/$H4TOH5IMAGELONE
    CONVERT image_lib_lonetest.hdf image_lib_lonetest.h5
}
CONVERT11() {
    # Run h4toh5 convert.
    TESTING $H4TOH5SDSLONE
    H4TOH5_BIN=`pwd`/$H4TOH5SDSLONE
    CONVERT sds_lib_lonetest.hdf sds_lib_lonetest.h5
}
CONVERT12() {
    # Run h4toh5 convert.
    TESTING $H4TOH5VDATALONE
    H4TOH5_BIN=`pwd`/$H4TOH5VDATALONE
    CONVERT vdata_lib_lonetest.hdf vdata_lib_lonetest.h5
}

##############################################################################
###			  T H E   T E S T S                                ###
##############################################################################
##############################################################################
# prepare for test
$RM $OUTDIR/*.hdf $OUTDIR/*.tmp
COPY_TESTFILES_TO_TESTDIR

#
# The HDF5 filenames are created based upon the HDF4 filenames
# without the extension.
#

# test for converting H5 groups to H4 Vgroups.
#CONVERT vg.hdf


#
# The test for conversion are the same as above with the only difference
# being that the HDF5 filenames are given explicitly.
#

$RM $OUTDIR/*.tmp
CONVERT1 
CONVERT2 
CONVERT3 
CONVERT4
CONVERT5 
CONVERT6
CONVERT7
CONVERT8
CONVERT9
CONVERT10
CONVERT11
CONVERT12
#
# Again, the test for conversion are the same as the first set of test.
# Here, multiple conversion are done on HDF4 files at one time.
#

$RM $OUTDIR/*.hdf $OUTDIR/*.tmp
#CONVERT -m vg.hdf

if test $nerrors -eq 0 ; then
	echo "All h4toh5 tests passed."
fi

$RM -r $OUTDIR
# Clean up temporary files/directories
CLEAN_TESTFILES_AND_TESTDIR

exit $nerrors
