# File     : test_support
# Purpose  : support for GYRE test scripts

# Get the platform architecture

get_arch ()
{

    local hardware=`uname -m`
    local os=`uname -s`

    echo -n "${hardware}-${os}"

}

# Run GYRE

run_gyre ()
{

    local exec=$1
    local file=$2
    local label=$3

    export GYRE_DIR=`pwd | sed -e 's|test/.*||'`

    if [ -n "$label" ]; then
	echo "TEST $label..."
    fi

    $exec $file > /dev/null
    retval=$?

    if [ $retval -ne 0 ]; then
	echo " ...failed: $exec returned non-zero exit status"
    fi

    return $retval

}

# Check output

check_output ()
{

    local file_a=$1
    local file_b=$2
    local h5diff_flags=${@:3}
    
    # If file_b isn't set, use ref file

    if [ ! -e "$file_b" ]; then
	    file_b="ref/${file_a}"
    fi

    # Check for the existence of the files

    if [ ! -e "$file_a" ]; then
	echo " ...failed: $file_a is missing"
	return 1
    fi

    if [ ! -e "$file_b" ]; then
	echo "  ...failed: $file_b is missing"
	return 1
    fi

    # Do the test

    h5diff $h5diff_flags $file_a $file_b
    retval=$?

    if [ $retval -ne 0 ]; then
	echo " ...failed: $file_a and $file_b differ"
    fi

    # Return

    return $retval

}
