#!/bin/sh
# test lpr's options

. $TESTDIR/src/functions.sh
echo "cleaning spool dirs... "; clean-spools
echon "waiting for any active jobs... "; wait-for-daemon; echo

TMPDOC="`basename $DOC`.tmp"

# we "cp $DOC $DOC.tmp" in these functions to avoid problems
# when we're testing "lpr -r".
thrice () {
    $*; cp $DOC $TMPDOC; $*; cp $DOC $TMPDOC; $*
}

do_lpr_test () {
    PRINTER="$1"; export PRINTER
    LPRCMD="$2"
    FILTERS="$3"

    do_test_pre $PRINTER "$FILTERS"
    cp $DOC $TMPDOC; $LPRCMD $TMPDOC
    do_test_post $PRINTER "$FILTERS"

    # if we're using "lpr -r", check to make sure the data file has
    # been removed.
    case "$PRINTER" in
    *_r | *_r_* )
	if [ -r $TMPDOC ] ; then
	    echo "$PRINTER: *** failed *** -- data file still present"
	fi ;;
    esac
}

do_separate_lpr_test () {
    PRINTER="$1"; export PRINTER
    LPRCMD="$2"
    FILTERS="$3"

    do_test_pre $PRINTER "$FILTERS"
    $LPRCMD $TMPDOC.1 $TMPDOC.2 $TMPDOC.3
    do_test_post $PRINTER "$FILTERS"

    # if we're using "lpr -r", check to make sure the data file has
    # been removed.
    case "$PRINTER" in
    *_r | *_r_* )
	if [ -r $TMPDOC.1 -o -r $TMPDOC.2 -o -r $TMPDOC.3 ] ; then
	    echo "$PRINTER: *** failed *** -- data file still present"
	fi ;;
    esac
}

###########################################################################

if [ $# -gt 0 ] ; then
    do_lpr_test $*
    exit 0
fi

###########################################################################
OUT=spool/OUT.default
unset PRINTER
do_lpr_test t_lpr_default	'thrice lpr'			banner

###########################################################################
OUT=spool/OUT.envtest
PRINTER=t_lpr_env; export PRINTER
do_lpr_test t_lpr_env		'thrice lpr'			banner

###########################################################################
# if envtest fails, so will all these; they rely on $ENV{'PRINTER'}
# setting the destination.

OUT=spool/OUT
do_lpr_test t_lpr_nobanner	'thrice lpr -h'
do_lpr_test t_lpr_pr		'thrice lpr -Fp'		banner,pr

do_lpr_test t_lpr_r		'thrice lpr -r'			banner
do_lpr_test t_lpr_s		'thrice lpr -s'			banner
# it's impossible to test "lpr -r -s" with one data file!

do_lpr_test t_lpr_hash		'lpr -#3'			banner
do_lpr_test t_lpr_hash_r	'lpr -#3 -r'			banner
do_lpr_test t_lpr_hash_s	'lpr -#3 -s'			banner
do_lpr_test t_lpr_hash_r_s	'lpr -#3 -r -s'			banner

do_lpr_test t_lpr_fe		'thrice lpr'			banner,fe
do_lpr_test t_lpr_fe_r		'thrice lpr -r'			banner,fe
do_lpr_test t_lpr_fe_s		'thrice lpr -s'			banner,fe
do_lpr_test t_lpr_fe_r_s	'thrice lpr -r -s'		banner,fe

do_lpr_test t_lpr_fe_hash	'lpr -#3'			banner,fe
do_lpr_test t_lpr_fe_hash_r	'lpr -#3 -r'			banner,fe
do_lpr_test t_lpr_fe_hash_s	'lpr -#3 -s'			banner,fe
do_lpr_test t_lpr_fe_hash_r_s	'lpr -#3 -r -s'			banner,fe

rm -f $TMPDOC.*
cp $DOC $TMPDOC.1; cp $DOC $TMPDOC.2; cp $DOC $TMPDOC.3
do_separate_lpr_test t_lpr_separate_r		'lpr -r'	banner

rm -f $TMPDOC.*
cp $DOC $TMPDOC.1.s; ln -s $TMPDOC.1.s $TMPDOC.1
cp $DOC $TMPDOC.2.s; ln -s $TMPDOC.2.s $TMPDOC.2
cp $DOC $TMPDOC.3.s; ln -s $TMPDOC.3.s $TMPDOC.3
do_separate_lpr_test t_lpr_separate_s		'lpr -s'	banner

rm -f $TMPDOC.*
cp $DOC $TMPDOC.1.s; ln -s $TMPDOC.1.s $TMPDOC.1
cp $DOC $TMPDOC.2.s; ln -s $TMPDOC.2.s $TMPDOC.2
cp $DOC $TMPDOC.3.s; ln -s $TMPDOC.3.s $TMPDOC.3
do_separate_lpr_test t_lpr_separate_r_s		'lpr -r -s'	banner
