#!@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=''
valgrind=0
make_ppds=1
md5dir=''
outdir=''
cupsargs=''
postscript=''
npages=3
enable_static='@ENABLE_STATIC@'
enable_shared='@ENABLE_SHARED@'

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=$((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" ;;
	    -p|--pages) shift; npages="$1" ;;
	    -P|--postscript) shift; postscript=1 ;;
	    --) shift; args="$@"; return ;;
	    *) return ;;
	esac
    shift
    done
}

set_args `getopt hvcgsVnO:m:o:p: "$@"`

if [ "$valgrind" -gt 0 -a "$enable_shared" != "no" ] ; then
    echo 'Valgrind is not compatible with --enable-shared in tree.' 1>&2
    echo 'Please use autogen.sh --disable-shared.' 1>&2
    exit 1
fi

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

version="5.2";
cupsdir="/usr/lib/cups/filter"
if [ -x "$cupsdir/pstoraster" -o -x "$cupsdir/gstoraster" -o -x "$cupsdir/cgpdftoraster" ] ; then
    pages="24-`expr 24 + $npages - 1`"
    if [ -n "$postscript" ] ; then
	pages="page-ranges=$pages"
    fi
else
    pages=''
fi

if [ ! -x "$cupsdir/cgpdftoraster" -a ! -x "$cupsdir/pdftops" -a ! -x "$cupsdir/gstoraster" ] ; 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
}

pdfjam="`which pdfjam`"
if [ -z "$pdfjam" ] ; then
    postscript=1
fi

if [ -n "$postscript" ] ; then
    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 `expr 24 + $npages - 1` "$sdir/../../doc/gutenprint-users-manual.pdf" $tfile
    fi
else
    tfile=`mktemp`
    trap cleanup 1 2 3 6 14 15 30
    "$pdfjam" -q "$sdir/../../doc/gutenprint-users-manual.pdf" "$pages" -o $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 --log-fd=3 --num-callers=50 --leak-check=yes --error-limit=no ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	2) valgrind $valopts --log-fd=3 --num-callers=50 --leak-resolution=high --leak-check=yes --error-limit=no ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	3) valgrind $valopts --log-fd=3 --error-limit=no --num-callers=50 --show-reachable=yes --leak-resolution=high --leak-check=yes ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	4) valgrind $valopts --log-fd=3 ./rastertogutenprint.$version 1 1 1 1 "$cupsargs" ;;
	5) cat ;;
	*) ./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 -n "`basename $f |sed -e 's/stp-//' -e 's/@GUTENPRINT_RELEASE_VERSION@.ppd.*$//'`... "
	    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 3>&2 `"
	    elif [ -f "$tfile" -a -x "$cupsdir/gstoraster" ] ; then
		output="`($cupsdir/gstoraster 1 1 1 1 \"$cupsargs\" < $tfile 2>/dev/null | run_rastertogp | do_output) 2>&1 3>&2 `"
	    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 3>&2 `"
	    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 3>&2 `"
	    elif [ -x "$cupsdir/gstoraster" ] ; then
		output="`($cupsdir/pdftops 1 1 1 1 \"$pages$cupsargs\" < $sdir/../../doc/gutenprint-users-manual.pdf 2>/dev/null | $cupsdir/gstoraster 1 1 1 1 \"$pages$cupsargs\" 2>/dev/null | run_rastertogp | do_output) 2>&1 3>&2 `"
	    else
		output="`($cupsdir/imagetoraster 1 1 1 1 \"$pages$cupsargs\" < calibrate.ppm 2>/dev/null | run_rastertogp | do_output) 2>&1 3>&2`"
	    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
