#!/bin/sh
# test_ppa v 0.2
# designed for Red Hat Linux systems
BINDIR=/usr/bin

echo  "*** Test/Calibration page printing utility for HP DeskJet PPA printers"
echo  "    Give PPA printer model ( 710 | 712 | 720 | 722 | 820 | 1000 ):"

read response
printer=""

if   $(echo $response | grep -e "710" > /dev/null ) ; then printer=710
elif $(echo $response | grep -e "712" > /dev/null ) ; then printer=710
elif $(echo $response | grep -e "720" > /dev/null ) ; then printer=720
elif $(echo $response | grep -e "722" > /dev/null ) ; then printer=720
elif $(echo $response | grep -e "820" > /dev/null ) ; then printer=820
elif $(echo $response | grep -e "1000" > /dev/null ) ; then printer=1000
fi

if [ "$printer" = "" ] ; then 
   echo "*** Error: printer model $response is not a valid PPA printer"
   exit 0
fi

echo  "    Give desired paper size ( letter | legal | a4 ):"
read response
paper=""

case $response in letter | Letter | us | US )
	paper="letter"
        page="/usr/lib/rhs/rhs-printfilters/testpage.ps"
	;;
esac

case $response in legal | Legal  )
	paper="legal"
        page="/usr/lib/rhs/rhs-printfilters/testpage.ps"
	;;
esac

case $response in a4 | A4 )
	paper="a4"
        page="/usr/lib/rhs/rhs-printfilters/testpage-a4.ps"
	;;
esac

if [ "$paper" = "" ] ; then 
   echo "*** Error: $response is not a valid paper size"
   exit 0
fi

echo  "    Give desired printer port /dev/lpn, n = (0 | 1 | 2):"
read response

case $response in  0 | 1 | 2 )
     port="/dev/lp$response"
     ;;
esac

if ! [ -e $port ] ; then
    echo "*** Error: printer port $port does not exist"
    exit 0
fi

echo  "  there are two PPA printer drivers:"
echo  "     1. pnm2ppa is the new color driver;"
echo  "     2. pbm2ppa is the old black-and-white driver."
echo  "  which driver do yo wish to test? [ 1 | 2 ] (1):?"

GSDEVICE=ppmraw
DRIVER=pnm2ppa
CALIB=calibrate
read response
if [ "$response" = "2" ] ; then
    DRIVER=pbm2ppa
    GSDEVICE=pbmraw
    CALIB=pbmtpg
fi

if ! [ -f $BINDIR/$DRIVER ] ; then
    echo "*** Error: $BINDIR/$DRIVER was not found"
    echo " is it correctly installed?"
    exit 0
fi

if ! $(eval "which $DRIVER | grep -e $BINDIR/$DRIVER >/dev/null" ) ; then
    echo "*** WARNING: the PPA driver  $DRIVER found in your path "
    echo "    is not $BINDIR/$DRIVER; \"which $DRIVER\" reports:"
    which $DRIVER
    echo "    move or delete this other copy of $DRIVER (exit) "
    exit 0
fi

if  [ -f $page ] ; then
    echo " *** Do you wish to print a  test page ? [Y | N] (N):"   
    read response
    case $response in y | Y | yes | Yes )
	eval "cat $page | gs -q \
	-sDEVICE=$GSDEVICE -dNOPAUSE -r600 -sPAPERSIZE=$paper \
	-sOutputFile=- -|  $BINDIR/$DRIVER -s $paper -v $printer \
	 - - > $port"
	;;
    esac
fi

echo " *** Do you wish to print the calibration  page ? [Y | N] (N):"   
read response
case $response in y | Y | yes | Yes )
    if ! [ -f $BINDIR/$CALIB ] ; then
	echo "*** Error: $BINDIR/$CALIB was not found"
	echo " is it correctly installed?"
	exit 0
    fi

    if ! $(eval "which $CALIB | grep -e $BINDIR/$CALIB >/dev/null" ) ; then
	echo "*** WARNING: the calibration program $CALIB found in your path "
	echo "    is not $BINDIR/$CALIB; \"which $CALIB\" reports:"
	which $CALIB
	echo "    move or delete this other copy of $CALIB (exit) "
	exit 0
    fi

    echo "   see the file CALIBRATION for details of its use"
    eval "$CALIB -$paper |  $BINDIR/$DRIVER -s $paper -v $printer \
    - -  > $port"
   ;;
esac


COLORCALIB=""
if [ "$DRIVER" = "pnm2ppa" ]  ; then
    echo " *** Do you wish to print the color calibration  page ? [Y | N] (N):"
    read response
    case $response in y | Y | yes | Yes )
	for file in /usr/doc/pnm2ppa*/pnm2ppa/test.ps ; do
	    if [ -f $file ] ; then
		COLORCALIB=$file
 	    fi
	done
	if [ "$COLORCALIB" = "" ] ; then
	    echo "*** color calibration test page test.ps not found"
	    echo "    Enter full path to directory where this file is found "
	    echo "    (or press return to cancel):"
	    read COLORPATH
	    COLORCALIB=${COLORPATH}/test.ps
	fi
	;;
    esac
fi 

if ! [ "$COLORCALIB" = "" ] ; then
    if [ -f $COLORCALIB ] ; then
        echo "   see the file CALIBRATION for details of its use"
	eval "cat $COLORCALIB | gs -q \
	-sDEVICE=$GSDEVICE -dNOPAUSE -r600 -sPAPERSIZE=$paper \
	-sOutputFile=- -|  $BINDIR/$DRIVER -s $paper -v $printer \
	 - - > $port  "
     else
	echo "*** Error: $COLORCALIB does not exist"
     fi
fi








