#! /bin/sh
#
# Copyright by The HDF Group.
# All rights reserved.
#
# This file is part of HDF5.  The full HDF5 copyright notice, including
# terms governing use, modification, and redistribution, is contained in
# the COPYING file, which can be found at the root of the source code
# distribution tree, or in https://www.hdfgroup.org/licenses.
# If you do not have access to either file, you may request a copy from
# help@hdfgroup.org.
#
# Tests for the h5fc compiler tool

srcdir=@srcdir@

# Initializations
TESTNAME=h5fc
EXIT_SUCCESS=0
EXIT_FAILURE=1

# Where the tool is installed.
prefix="${prefix:-@prefix@}"
PARALLEL=@PARALLEL@		# Am I in parallel mode?
AR="@AR@"
RANLIB="@RANLIB@"
if [ "$PARALLEL" = no ]; then
    H5TOOL="h5fc"           	# The tool name
else
    H5TOOL="h5pfc"               # The tool name
fi
H5TOOL_BIN="${prefix}/bin/${H5TOOL}"   # The path of the tool binary

CMP='cmp -s'
DIFF='diff -c'

nerrors=$EXIT_SUCCESS
verbose=yes

# setup my machine information.
myos=`uname -s`
myhostnama=`uname -n`

# Generate some source files and library for tests.
suffix=f90		# source file suffix
hdf5main=${H5TOOL}_hdf5main.$suffix
hdf5main_o=${H5TOOL}_hdf5main.o
appmain=${H5TOOL}_appmain.$suffix
appmain_o=${H5TOOL}_appmain.o
prog1=${H5TOOL}_prog1.$suffix
prog1_o=${H5TOOL}_prog1.o
prog2=${H5TOOL}_prog2.$suffix
prog2_o=${H5TOOL}_prog2.o
applib=libapp${H5TOOL}.a

# short hands
# Caution: if some *.h5 files must be cleaned here, list them by names.
# Don't use the wildcard form of *.h5 as it will wipe out even *.h5 generated
# by other test programs. This will cause a racing condition error when
# parallel make (e.g., gmake -j 4) is used.
temp_SRC="$hdf5main $appmain $prog1 $prog2"
temp_OBJ=`echo $temp_SRC | sed -e "s/\.${suffix}/.o/g"`
temp_FILES="a.out $applib"

# Generate appmain:
# An application Main that calls hdf5 and application's own functions.
cat > $appmain <<EOF
     PROGRAM FILEEXAMPLE
     USE HDF5 ! This module contains all necessary modules

     IMPLICIT NONE

     CHARACTER(LEN=8), PARAMETER :: filename = "apptmp.h5" ! File name
     INTEGER(HID_T) :: file_id                            ! File identifier

     INTEGER     ::   error  ! Error flag

     CALL sub1
     CALL h5open_f (error)
     CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)
     CALL h5fclose_f(file_id, error)
     CALL h5close_f(error)
     CALL sub2
     END PROGRAM FILEEXAMPLE
EOF

# generate prog1
cat > $prog1 <<EOF
	subroutine sub1
	print *, "in sub1"
	end
EOF

# generate prog2
cat > $prog2 <<EOF
	subroutine sub2
	print *, "in sub2"
	end
EOF

# Generate HDF5 Main Program:
# An HDF5 sample program that calls hdf5 functions.
cat > $hdf5main <<EOF
     PROGRAM FILEEXAMPLE
     USE HDF5 ! This module contains all necessary modules

     IMPLICIT NONE

     CHARACTER(LEN=8), PARAMETER :: filename = "apptmp.h5" ! File name
     INTEGER(HID_T) :: file_id                            ! File identifier

     INTEGER     ::   error  ! Error flag

     CALL h5open_f (error)
     CALL h5fcreate_f(filename, H5F_ACC_TRUNC_F, file_id, error)
     CALL h5fclose_f(file_id, error)
     CALL h5close_f(error)
     END PROGRAM FILEEXAMPLE
EOF



# Parse option
#   None

# 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'
}


# Debug printing
# Change : to echo to print the debug statement
DPRINT() {
    : $*
}

# 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
# failed output.  The actual output is not removed if $HDF5_NOCLEANUP is
# defined.
#
TOOLTEST() {
    out=test_$H5TOOL_$$.out
    err=test_$H5TOOL_$$.err

    # Run test.
    TESTING $H5TOOL $@
    $H5TOOL_BIN $@ > $out 2>&1
    result=$?
    if [ $result = 0 ]; then
	echo " PASSED"
    else
	echo "*FAILED*"
	nerrors="`expr $nerrors + 1`"
	test yes = "$verbose" && \
	    ( echo "========== results ==========="; cat $out;
	      echo "===============================================") |sed 's/^/    /'
    fi

    # Clean up output file
    if test -z "$HDF5_NOCLEANUP"; then
	rm -f $out
    fi
}

# Print a "SKIP" message
SKIPTEST() {
	 TESTING $H5TOOL $@
	  echo  " -SKIP-"
}


##############################################################################
###			  T H E   T E S T S                                ###
##############################################################################
#
# HDF5 program that calls HDF5 APIs.
echo "***"Simple Compile and Link in one step.
TOOLTEST $hdf5main
# Application program that calls HDF5 and its own functions.
TOOLTEST $appmain $prog1 $prog2

# Compile, then link.
echo "***"Compile and Link in two steps.
TOOLTEST -c $hdf5main
TOOLTEST $hdf5main_o
TOOLTEST -c $appmain $prog1 $prog2
TOOLTEST $appmain_o $prog1_o $prog2_o

# Build external library, then link with it.
echo "***"Build external library and link with it.
TOOLTEST -c $prog1 $prog2
$AR cru $applib $prog1_o $prog2_o
$RANLIB $applib
TOOLTEST $appmain $applib
TOOLTEST $appmain_o $applib

# No preprocess test since -E is not a common option for Fortran compilers.

##############################################################################
# END
##############################################################################

# Clean up  file
if test -z "$HDF5_NOCLEANUP"; then
    rm -f $temp_SRC $temp_OBJ $temp_FILES
fi

if test $nerrors -eq 0 ; then
    echo "All $TESTNAME tests passed."
    exit $EXIT_SUCCESS
else
    echo "$TESTNAME tests failed with $nerrors errors."
    exit $EXIT_FAILURE
fi