#!/bin/sh

retval=0;

if [ -z "$STP_DATA_PATH" ] ; then
    STP_DATA_PATH=`pwd`/../main
    export STP_DATA_PATH
fi

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

valgrind=0
valopts=''
extra=''
dontrun=

set_opt() {
    opt=`echo $1 | awk -F= '{print $1}'`
    val=`echo $1 | awk -F= '{print $2}'`
    extra="`printf '%s\nparameter \"%s\" \"%s\";' \"$extra\" $opt $val`"
}

set_message() {
    the_message="`printf '%s\nmessage \"%s\";' \"$the_message\" \"$@\"`"
}

usage() {
    echo "Usage: run-testpattern [-v|--valgrind]"
    echo "                       [-p|--parameter parameter=value]"
    echo "                       [-n|--dontrun]"
    echo "                       [--] [printers...]"
    exit 0;
}

set_args() {
    while true ; do
	case "$1" in
	    -h*|--h*) usage ;;
	    -v|--valgrind) valopts='--skin=memcheck' ; valgrind=`expr $valgrind + 1` ;;
	    -c|--cachegrind) valopts='--skin=cachegrind'; valgrind=4 ;;
	    -g|--gdb-attach) valopts='--gdb-attach=yes' ;;
	    -p|--parameter) shift; set_opt "$1" ;;
    	    -n|--dontrun) dontrun=1 ;;
	    --) shift; args="$@"; return ;;
	    *) return ;;
	esac
    shift
    done
}

print_one_testpattern() {
    printer=$1
    shift
    stuff="$@"
    cat <<EOF
printer "$printer";
$stuff
hsize 0.1;
vsize 0.1;
left 0.15;
top 0.15;
blackline 0;
steps 16;
mode rgb 8;
pattern 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0  0.0 0.0 1.0  0.0 0.0 1.0  0.0 0.0 1.0 ;
pattern 1.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0  0.0 1.0 1.0 0.0 0.0 1.0 0.0 0.0 1.0;
pattern 1.0 1.0 1.0 1.0 1.0 0.0 0.0 1.0  0.0 0.0 1.0 0.0 1.0 1.0 0.0 0.0 1.0;
pattern 1.0 1.0 1.0 1.0 1.0 0.0 0.0 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.0 1.0 0.0 0.9 1.0  0.0 0.1 1.0 0.0 0.1 1.0 0.0 0.0 1.0;
end;
EOF
}

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

set_args `getopt hgvp:nc "$@"`

if [ -z "$args" ] ; then
    args=`./printers`
fi

StandardDithers="EvenTone HybridEvenTone UniTone HybridUniTone Adaptive Ordered Fast VeryFast Floyd Predithered"

the_message=''

print_all_testpatterns() {
    if [ -z "`echo $extra |grep DitherAlgorithm`" ] ; then
	for p in $args ; do
	    set_message "$p
"
	    for d in $StandardDithers ; do
		set_message "    $d"
		print_one_testpattern "$p" "`printf '%s\n' \"parameter \\\"DitherAlgorithm\\\" \\\"$d\\\";\"`" "$extra" "$the_message"
		the_message=''
	    done
	done
    else
	for p in $args ; do
	    the_message=''
	    set_message "$p"
	    print_one_testpattern "$p" "$extra" "$the_message"
	done
    fi
}

if [ -z "$dontrun" ] ; then
    print_all_testpatterns | run_testpattern
else
    print_all_testpatterns
fi

exit $?
