#!/bin/bash
# Test suite to compare C examples with other language bindings
#
# Copyright (C) 2008 Andrew Ross
#
# This file is part of PLplot.
#
# PLplot is free software; you can redistribute it and/or modify
# it under the terms of the GNU Library General Public License as published
# by the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# PLplot is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU Library General Public License for more details.
#
# You should have received a copy of the GNU Library General Public License
# along with PLplot; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
#

ret=0
# Comparison C results have no xsuffix.
xsuffix_c=

# Compare C results with c++, f95, java, octave, python, tcl, perl, 
# ada, ocaml, lua and d results
for lang in c++ f95 java octave python tcl perl ada adathick ocaml lua d; do

  # Check which suffix is used for this binding
    case $lang in 
	c++)
	    xsuffix=
	    suffix=cxx
	    ;;
	f95)
	    xsuffix=
	    suffix=f95
	    ;;
	java)
	    xsuffix=
	    suffix=j
	    ;;
	octave)
	    xsuffix=
	    suffix=o
	    ;;
	python)
	    xsuffix=
	    suffix=p
	    ;;
	tcl)
	    xsuffix=
	    suffix=t
	    ;;
	perl)
	    xsuffix=
	    suffix=pdl
	    ;;
	ada)
	    xsuffix=
	    suffix=a
	    ;;
	adathick)
	    xsuffix=thick
	    suffix=a
	    ;;
	ocaml)
	    xsuffix=
	    suffix=ocaml
	    ;;
	lua)
	    xsuffix=
	    suffix=lua
	    ;;
	d)
	    xsuffix=
	    suffix=d
	    ;;
    esac

    missing=""
    different=""
    diffstdout=""
    missingstdout=""

    # List of standard examples
    INDEX_LIST="00 01 02 03 04 05 06 07 08 09 10 11 12 13 14 14a 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 33"
    # Check if any examples exist for this language.
    EXAMPLES_EXIST="no"
    for index in ${INDEX_LIST} ; do
	if [ -f x${xsuffix}${index}${suffix}.psc ] ; then
	    EXAMPLES_EXIST="yes"
	    break
	fi
    done
    if [ "$EXAMPLES_EXIST" = "yes" ] ; then
	for index in ${INDEX_LIST} ; do
	    if [ ! -f x${xsuffix_c}${index}c.psc ] ; then
		echo "C example ${index} is missing"
	    else
		if [ ! -f x${xsuffix}${index}${suffix}.psc ] ; then
		    missing="${missing} ${index}"
		else
		    if [ "ON" = "ON" ] ; then
		    # Skip first 190 bytes of comparison to ignore date stamp.
			/usr/bin/cmp -s -i 190 x${xsuffix_c}${index}c.psc x${xsuffix}${index}${suffix}.psc
			if [ $? != 0 ] ; then
			    different="${different} ${index}"
			fi
		    else
		    # Drop first 8 lines from comparison to ignore date stamp.
			/usr/bin/tail -n +9 x${xsuffix_c}${index}c.psc > test1.psc
			/usr/bin/tail -n +9 x${xsuffix}${index}${suffix}.psc > test2.psc
			/usr/bin/diff -q test1.psc test2.psc 2>&1 > /dev/null
			if [ $? != 0 ] ; then
			    different="${different} ${index}"
			fi
		    fi
		    if [ "$index" != "14a" ] ; then
			if [ -f x${xsuffix}${index}${suffix}_psc.txt ] ; then
			    /usr/bin/diff -q x${xsuffix_c}${index}c_psc.txt x${xsuffix}${index}${suffix}_psc.txt 2>&1 > /dev/null
			    if [ $? != 0 ] ; then
				diffstdout="${diffstdout} ${index}"
			    fi
			else
			    missingstdout="${missingstdout} ${index}"
			fi
		    fi
		fi
	    fi
	done
	echo "${lang}"
	echo "  Missing examples            : ${missing}"
	echo "  Differing postscript output : ${different}"
	echo "  Missing stdout              : ${missingstdout}"
	echo "  Differing stdout            : ${diffstdout}"
	if [ "${different}" != "" -o "${diffstdout}" != "" ] ; then
	    ret=1
	fi
    fi 
done

if [ "${ret}" != "0" ] ; then
    echo "WARNING: Some PostScript or stdout results were different"
fi
