#!@SHELL@

retval=0

if [ -z "$srcdir" -o "$srcdir" = "." ] ; then
    sdir=`pwd`
elif [ -n "`echo $srcdir |grep '^/'`" ] ; then
    sdir="$srcdir"
else
    sdir="`pwd`/$srcdir"
fi

if [ -z "$STP_DATA_PATH" ] ; then
    STP_DATA_PATH="$sdir/../xml"
    export STP_DATA_PATH
fi

if [ -z "$STP_MODULE_PATH" ] ; then
    STP_MODULE_PATH="$sdir/../main:$sdir/../main/.libs"
    export STP_MODULE_PATH
fi

single=''
all_models=''
verbose=''
make_ppds=1
md5dir=''
outdir=''
cupsargs=''

usage() {
    echo "Usage: test-rastertogutenprint [-s] [-v|--valgrind]"
    exit 0;
}

set_args() {
    while true ; do
	case "$1" in
	    -s) single=1 ;;
	    -h*|--h*) usage ;;
	    -v|--valgrind) valopts='--tool=memcheck' ; valgrind=`expr $valgrind + 1` ;;
	    -c|--cachegrind) valopts='--tool=cachegrind'; valgrind=4 ;;
	    -g|--gdb-attach) valopts='--gdb-attach=yes' ;;
	    -V|--verbose) verbose=1 ;;
	    -n|--no-make-ppds) make_ppds='' ;;
	    -O|--output) shift; outdir="$1" ;;
	    -o|--option) shift; cupsargs="$cupsargs $1" ;;
	    -m|--md5dir) shift; md5dir="$1" ;;
	    --) shift; args="$@"; return ;;
	    *) return ;;
	esac
    shift
    done
}

set_args `getopt vcgsVnO:m:o: "$@"`

if [ -n "$outdir" -a ! -d "$outdir" ] ; then
    mkdir -p "$outdir"
fi
if [ -n "$md5dir" -a ! -d "$md5dir" ] ; then
    mkdir -p "$md5dir"
fi

version="@GUTENPRINT_MAJOR_VERSION@.@GUTENPRINT_MINOR_VERSION@";
cupsdir="@cups_conf_serverbin@/filter"
if [ -x "$cupsdir/pstoraster" -o -x "$cupsdir/cgpdftoraster" ] ; then
    pages='page-ranges=24-26 '
else
    pages=''
fi

if [ ! -x "$cupsdir/cgpdftoraster" -a ! -x "$cupsdir/pdftops" ] ; then
    echo 'CUPS does not appear to be installed, skipping test'
    exit 0
fi

cleanup() {
    if [ -f "$tfile" ] ; then
	rm -f $tfile
    fi
    exit 1
}

pdftops="`type -p pdftops`"

if [ ! -n "$pdftops" -o ! -x "$pdftops" ] ; then
    pdftops="`whence pdftops`"
fi

if [ -n "$pdftops" -a ! -x "$cupsdir/cgpdftoraster" ] ; then
    tfile=`mktemp`
    trap cleanup 1 2 3 6 14 15 30
    $pdftops -f 24 -l 26 "$sdir/../../doc/gutenprint-users-manual.pdf" $tfile
fi

if [ -z "$verbose" ] ; then
    STP_SUPPRESS_MESSAGES=1
    export STP_SUPPRESS_MESSAGES
fi

is_duplicate() {
    model=`gunzip -c "$1" | grep '^.StpDriverModelFamily' | awk '{print $2}'`
    for m in $all_models; do
	if [ "$model" = "$m" ] ; then
	    skip=1
	fi
    done
    all_models="$model $all_models"
}

# Note that using CUPS arguments may trigger valgrind memory leaks in
# CUPS.
#cupsargs='PageSize=Custom.400.00x500.00'
#cupsargs='PageSize=Custom.324x495 Resolution=180dpi'
#cupsargs='PageSize=w324h495 Resolution=180dpi'
#cupsargs='PageSize=A8'

run_rastertogp() {
    case "$valgrind" in
	1) valgrind $valopts -q --num-callers=100 --leak-check=yes --error-limit=no ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	2) valgrind $valopts --num-callers=100 --leak-resolution=high --leak-check=yes --error-limit=no ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	3) valgrind $valopts --error-limit=no --num-callers=100 --show-reachable=yes --leak-resolution=high --leak-check=yes ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	4) valgrind $valopts ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	*) ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
    esac
    if [ $? -ne 0 ] ; then
	retval=1
    fi
    exit $retval
}

get_ppds() {
    args="$@"
    if [ -n "$args" ] ; then
	for f in "$@" ; do
	    if [ -r "$f" ] ; then
		echo $f
	    elif [ -r "ppd/C/$f" ] ; then
		echo "ppd/C/$f"
	    elif [ -f "ppd/C/${f}.ppd" ] ; then
		echo "ppd/C/${f}.ppd"
	    elif [ -f "ppd/C/${f}.ppd.gz" ] ; then
		echo "ppd/C/${f}.ppd.gz"
	    elif [ -f "ppd/C/${f}.ppd.GZ" ] ; then
		echo "ppd/C/${f}.ppd.GZ"
	    elif [ -f "ppd/C/${f}.ppd.bz2" ] ; then
		echo "ppd/C/${f}.ppd.bz2"
	    elif [ -f "ppd/C/${f}.ppd.BZ2" ] ; then
		echo "ppd/C/${f}.ppd.BZ2"
	    elif [ -f "ppd/C/${f}.ppd.z" ] ; then
		echo "ppd/C/${f}.ppd.z"
	    elif [ -f "ppd/C/${f}.ppd.Z" ] ; then
		echo "ppd/C/${f}.ppd.Z"
	    elif [ -f "ppd/C/stp-${f}.ppd" ] ; then
		echo "ppd/C/stp-${f}.ppd"
	    elif [ -f "ppd/C/stp-${f}.ppd.gz" ] ; then
		echo "ppd/C/stp-${f}.ppd.gz"
	    elif [ -f "ppd/C/stp-${f}.ppd.GZ" ] ; then
		echo "ppd/C/stp-${f}.ppd.GZ"
	    elif [ -f "ppd/C/stp-${f}.ppd.bz2" ] ; then
		echo "ppd/C/stp-${f}.ppd.bz2"
	    elif [ -f "ppd/C/stp-${f}.ppd.BZ2" ] ; then
		echo "ppd/C/stp-${f}.ppd.BZ2"
	    elif [ -f "ppd/C/stp-${f}.ppd.z" ] ; then
		echo "ppd/C/stp-${f}.ppd.z"
	    elif [ -f "ppd/C/stp-${f}.ppd.Z" ] ; then
		echo "ppd/C/stp-${f}.ppd.Z"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.gz" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.gz"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.GZ" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.GZ"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.bz2" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.bz2"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.BZ2" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.BZ2"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.z" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.z"
	    elif [ -f "ppd/C/stp-${f}.${version}.ppd.Z" ] ; then
		echo "ppd/C/stp-${f}.${version}.ppd.Z"
	    fi
	done
    else
	echo ppd/C/*.ppd*
    fi
}

if [ -n "$make_ppds" -o ! -d ppd/C ] ; then
    make ppd-nonls
## Not all systems can work with gzipped PPDs
    find ppd/C -name '*.ppd.gz' | xargs gunzip -f
fi

do_output() {
    if [ -n "`echo "$PPD" |grep '\.gz$'`" ] ; then
	driver=`gunzip -c "$PPD" | grep '^\*StpDriverName:' |awk '{print $2}' | sed 's/"//g'`
    else
	driver=`grep '^\*StpDriverName:' "$PPD" |awk '{print $2}' | sed 's/"//g'`
    fi
    if [ -n "$outdir" ] ; then
	cat > "$outdir/$driver.prn"
	if [ -n "$md5dir" ] ; then
	    (cd "$outdir"; cat "$driver.prn") | md5sum | sed "s/-/\*$driver/" > "$md5dir/$driver.md5"
	fi
    elif [ -n "$md5dir" ] ; then
	cat | md5sum | sed "s/-/\*$driver/" > "$md5dir/$driver.md5"
    else
	cat > /dev/null
    fi
}

if [ -d ppd/C ] ; then
    for f in `get_ppds $args` ; do
	skip=''
	if [ -n "$single" ] ; then
	    is_duplicate $f
	fi
	if [ -z "$skip" ] ; then
	    echo "$f "
	    PPD=$f
	    export PPD
	    if [ -x "$cupsdir/cgpdftoraster" ] ; then
		output="`($cupsdir/cgpdftoraster 1 1 1 1 $pages < $sdir/../../doc/gutenprint-users-manual.pdf 2>/dev/null | run_rastertogp | do_output) 2>&1`"
	    elif [ -f "$tfile" ] ; then
		output="`($cupsdir/pstops 1 1 1 1 \"$cupsargs\" < $tfile 2>/dev/null | $cupsdir/pstoraster 2>/dev/null | run_rastertogp | do_output) 2>&1`"
	    elif [ -x "$cupsdir/pstoraster" ] ; then
		output="`($cupsdir/pdftops 1 1 1 1 \"$pages$cupsargs\" < $sdir/../../doc/gutenprint-users-manual.pdf 2>/dev/null | $cupsdir/pstops 1 1 1 1 \"$pages$cupsargs\" 2>/dev/null | $cupsdir/pstoraster 2>/dev/null | run_rastertogp | do_output) 2>&1`"
	    else
		output="`($cupsdir/imagetoraster 1 1 1 1 \"$pages$cupsargs\" < calibrate.ppm 2>/dev/null | run_rastertogp | do_output) 2>&1`"
	    fi
	    if [ $? -ne 0 ] ; then
		retval=1
	    fi
	    echo "$output"
	    echo "$output" |grep ERROR
	    if [ $? -eq 0 ] ; then
		retval=1
	    fi
	fi
    done
fi
if [ -f "$tfile" ] ; then
    rm -f $tfile
fi
exit $retval
