#!/bin/sh
# -------------------------------------------------
# LSB test for presence of ghostscript binary and minimal
# driver set 
# -------------------------------------------------
# Modification History:
# 09/16/2007 - Stew Benedict - stewb@linux-foundation.org
# 11/14/2007 - George Liu - george.liu@ricoh-tech.com - 
#              Test pxlmono and pxlcolor driver with test_driver_pxl()
# 11/14/2007 - George Liu - george.liu@ricoh-tech.com - 
#              Switched to use tiger.ps as test file for test_driver_pxl(). 
#              Enabled testing of A3, A4, 11x17 for pxlmono/pxlcolor drivers. 
# 11/15/2007 - George Liu - george.liu@ricoh-tech.com
#              Updated check_stdout_binary_P() to use perl for string search 
#              instead of "grep -P". Contributed by Till Kamppeter
#              Use both testprint.ps and tiger.ps for pxlmono/pxlcolor test.
# -------------------------------------------------
tet_startup="startup"                   # startup function
tet_cleanup="cleanup"                   # cleanup function
iclist="ic1 ic2 ic3 ic4 ic5 ic6 ic7 ic8 ic9 ic10 ic11"
ic1="tp1"
ic2="tp2"
ic3="tp3"
ic4="tp4"
ic5="tp5"
ic6="tp6"
ic7="tp7"
ic8="tp8"
ic9="tp9"
ic10="tp10"
ic11="tp11"

GS=/usr/bin/gs
TFILE=testprint.ps

if [ -x $GS ]; then
  ALLDRIVERS=`echo 'devicenames ==' | $GS -dBATCH -sDEVICE=bbox -q - | sed 's|/||g;s|\[||g;s|\]||g'`
else
  echo "$GS not present, test aborted..."
fi

tp1() {
    tpstart "Reference III.8.1" 
    tet_infoline "The implementation provides an exec-able version"
    tet_infoline "of the $GS command in the /usr/bin directory."
    test -x $GS 2>out.stderr
    check_exit_value $? 0
    check_nostderr
    tpresult                        # set result code
}

tp2() {
    DRV=cups
    tpstart "Reference III.8.2" 
    check_driver $DRV 
}

tp3() {
    DRV=cups
    tpstart "Reference III.8.2"
    test_driver $DRV
}

tp4() {
    DRV=ijs
    tpstart "Reference III.8.2" 
    check_driver $DRV 
}

tp5() {
    DRV=ijs
    tpstart "Reference III.8.2"
    test_driver $DRV
}

tp6() {
    DRV=opvp
    tpstart "Reference III.8.2" 
    check_driver $DRV 
}

tp7() {
    DRV=opvp
    tpstart "Reference III.8.2"
    test_driver $DRV
}

tp8() {
    DRV=pxlmono
    tpstart "Reference III.8.2" 
    check_driver $DRV 
}

tp9() {
    DRV=pxlmono
    tpstart "Reference III.8.2"
    test_driver_pxl $DRV
}

tp10() {
    DRV=pxlcolor
    tpstart "Reference III.8.2"
    check_driver $DRV 
}

tp11() {
    DRV=pxlcolor
    tpstart "Reference III.8.2"
    test_driver_pxl $DRV
}

check_driver() {
    tet_infoline "The gs command provides support for the $1 device."
    echo $ALLDRIVERS | grep -q $1 2>out.stderr
    check_exit_value $? 0
    check_nostderr
    tpresult                        # set result code
}

# This function will execute $1 and compare the exit value with $2
# This is a derived function from LIB/shfuncs.sh/check_exit()
# Differences:
# It does not save stdout to file out.stdout
check_exit_nosaving() # execute command (saving output) and check exit code
{
    # $1 is command, $2 is expected exit code (0 or "N" for non-zero)
    eval "$1" 2> out.stderr
    CODE=$?
    if [ $2 = 0 -a $CODE -ne 0 ]
    then
        tet_infoline "Command ($1) gave exit code $CODE, expected 0"
        FAIL=Y
    elif [ $2 != 0 -a $CODE -eq 0 ]
    then
        tet_infoline "Command ($1) gave exit code $CODE, expected non-zero"
        FAIL=Y
    fi
}

# This is a derived function from LIB/shfuncs.sh/check_stdout()
# It checks whether string $1 is present in file out.stdout
# Differences:
# 1. It does not output file when check_stdout failed
# 2. It's using Perl rather than grep (grep -P is not available in all distros)
#
# Extra features
# 1. Search terms can be multi-line, you can search for \n and \r, and ^
# and $ are not only the beginning and the end of the whole string, but
# also of each line. Use [^\n\r]* to match arbitrary characters within a
# line, .* can also go over line breaks.
#
# 2. You can give an arbitrary number of command line arguments. If you
# give more than one, every command line argument is considered a search
# term and they have to be all in out.stdout, in the given order and
# without overlapping. They get joined as "<arg1>.*<arg2>.*..." and then
# passed through Perl as one search term, remember that .* can go over
# line breaks.
#
# 3. When the search term is written into the result file due to a
# failure, the '\' get quoted by replacing them with '\\', so they appear
# actually in the result file and no warnings get issued.
check_stdout_binary_P() 
{
     SEARCHTERM=''
     case $1 in
     "")
         if [ ! -s out.stdout ]
         then
             tet_infoline "Expected output to stdout, but none written"
             FAIL=Y
         fi
         ;;
     *)
        for s in $*
        do
            if [ -n "$SEARCHTERM" ]
            then
                SEARCHTERM=$SEARCHTERM'.*'
            fi
            SEARCHTERM=$SEARCHTERM$s
        done
        cat out.stdout | perl -e 'my $a = join("",<>); exit !($a =~
/'$SEARCHTERM'/sm)'
         if [ $? -ne 0  ]
         then
                 tet_infoline "Output written to stdout did not contain
\""`echo $SEARCHTERM | perl -p -e 's/\\\\/\\\\\\\\/g'`"\""
                 FAIL=Y
         fi
         ;;
     esac
}

# ----------------------------------------------------------------------------
# Test the basic functionalities of pxlmono/pxlcolor driver in Ghostscript.
# The test suite contains multiple test cases, each test case will
# 1. Invoke Ghostscript pxlmono/pxlcolor driver with a set of parameters. Output will be saved into a file.
# 2. Examine output file to validate the existance of correspondent PCL-XL commands.
# The following parameters are tested:
# Resolution (-r): [300x300, 600x300, 600x600, 1200x1200]
# PaperSize (-sPAPERSIZE): [letter, a4, a3, 11x17] 
# InputSlot: [Auto or Current] Although GS does not support InputSlot parameter, every version of GS insert either 
#            Auto or Current InputSlot command. All existing pxlmono/pxlcolor drivers modifies it to support 
#            printer input trays.
# ColorMode: [Not tested] ColorSpace=eGray might be optional for pxlmono device; pxlcolor might be able to use ColorSpace=eGray 
# ----------------------------------------------------------------------------
test_driver_pxl() {
    TFILE2=tiger.ps
    OUTPUTFILE=out.stdout
    DTEST="$GS -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=$1 -r600x600 -sPAPERSIZE=letter -sOutputFile=$OUTPUTFILE $TFILE"
    tet_infoline "Checking result of processing $TFILE with '$DTEST'"
    
    check_exit_nosaving "$DTEST" 0
    # GS will generate PageInfo as stderr
    # check_stderr() 
    tet_infoline "Verifying Resolution=600x600"
    check_stdout_binary_P "\xd1\x58\x02\x58\x02\xf8\x89"
    tet_infoline "Verifying Resolution Measure=dpi"
    check_stdout_binary_P "\xc0\x00\xf8\x86"
    tet_infoline "Verifying InputSlot=[Auto|Current]"
    check_stdout_binary_P "\xc0[\x00\x01]\xf8\x26"    
    tet_infoline "Verifying PageSize=letter"
    check_stdout_binary_P "\xc0\x00\xf8\x25" 
    # Color mode? It's probably OK for pxlmono driver to skip setColorSpace command
    # It's probably also OK for pxlcolor driver to use eGray setColorSpace command
    # Color mode testing will be skipped here.
    # tet_infoline "Verifying ColorMode"
    # check_stdout_binary_P "\xc0[\x00\x01]\xf8\x03\x6a"     

    DTEST="$GS -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=$1 -r1200x1200 -sPAPERSIZE=a4 -sOutputFile=$OUTPUTFILE $TFILE2"
    tet_infoline "Checking result of processing $TFILE2 with '$DTEST'"
    check_exit_nosaving "$DTEST" 0
    # GS will generate PageInfo as stderr
    # check_stderr() 
    tet_infoline "Verifying Resolution=1200x1200"
    check_stdout_binary_P "\xd1\xb0\x04\xb0\x04\xf8\x89"
    tet_infoline "Verifying PageSize=a4"
    check_stdout_binary_P "\xc0\x02\xf8\x25" 

    DTEST="$GS -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=$1 -r600x300 -sPAPERSIZE=11x17 -sOutputFile=$OUTPUTFILE $TFILE2"
    tet_infoline "Checking result of processing $TFILE2 with '$DTEST'"
    check_exit_nosaving "$DTEST" 0
    # GS will generate PageInfo as stderr
    # check_stderr() 
    tet_infoline "Verifying Resolution=600x300"
    check_stdout_binary_P "\xd1\x58\x02\x2c\x01\xf8\x89"
    tet_infoline "Verifying PageSize=11x17"
    check_stdout_binary_P "\xc0\x04\xf8\x25" 

    DTEST="$GS -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=$1 -r300x300 -sPAPERSIZE=a3 -sOutputFile=$OUTPUTFILE $TFILE2"
    tet_infoline "Checking result of processing $TFILE2 with '$DTEST'"
    check_exit_nosaving "$DTEST" 0
    # GS will generate PageInfo as stderr
    # check_stderr() 
    tet_infoline "Verifying Resolution=300x300"
    check_stdout_binary_P "\xd1\x2c\x01\x2c\x01\xf8\x89"
    tet_infoline "Verifying PageSize=a3"
    check_stdout_binary_P "\xc0\x05\xf8\x25" 

    TFILE=testprint.ps
    tpresult

}

test_driver() {
    DTEST="$GS -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=$1 -sOutputFile=/dev/null $TFILE"
    if [ "$1" = "ijs" ];then
        # spaces in -sDeviceModel cause the tet wrapper to mangle things
        # such that they do not work - '-sDeviceModel="HP' 'LaserJet"'
	# instead on -sDeviceModel="HP LaserJet"
	# got tired of fighting it, just spawn another script
        DTEST="./testijs"
    fi
    if [ "$1" = "opvp" ];then
	# this one is slightly different too, and it does not like our
	# testprint.ps
	TFILE=example.ps
	DTEST="$GS -q -dBATCH -dPARANOIDSAFER -dQUIET -dNOPAUSE -sDEVICE=opvp -sDriver=/opt/lsb/test/printing/drivers/libopvpnull.so -sOutputFile=/dev/null $TFILE"
    fi

    tet_infoline "Checking result of processing a known .ps file using the $1 device"
    $DTEST 2>out.stderr
    if [ "$?" -eq 0 ];then
        tpresult PASS
    else
        tet_infoline "$GS returned $? using the $1 driver, expected 0"
        tpresult FAIL
    fi
}

check_md5() {
    tet_infoline "Checking md5sum of a known .ps file processed by the $1 device"
    TMD5=`$GS -sDEVICE=$1 -sOutputFile=- -q - < $TFILE 2>/dev/null | md5sum - | awk '{print $1}'`
    if [ "$TMD5" = "$2" ];then
        tpresult PASS
    else
        tet_infoline "md5sum returned $TMD5, expected $2"
        tpresult FAIL
    fi
}
 
startup() {
	rm -f tet_xres
}

cleanup() {
	if [ -f tet_xres ];then
		#mv tet_xres journal.gs-test
		rm -f out.stderr
	fi
}

# source common shell functions

. $TET_EXECUTE/LIB/shfuncs.sh
. $TET_EXECUTE/LIB/lsbfuncs.sh

TET_ROOT=/opt/lsb-tet3-lite
# execute shell test case manager - must be last line
. $TET_ROOT/lib/xpg3sh/tcm.sh

