#!/bin/sh

##==============================================================================
#$ PCL        - PCL based filter script                        Updated: 03/21/01
##------------------------------------------------------------------------------
##
## OPTIONS RECOGNIZED: ( all must be preceded by a "-" )                 
##                                                                       
## Page Orientation: (Default portrait)	                                 
##       -orientation= 'portrait' or 'landscape'                       
##                                                                       
## Horizontal Pitch Selection                                            
##       -pitch=       numeric cpi value                 
##       -point=       numeric cpi value                 
##                                                                       
## Vertical Pitch Selection                                              
##       -lpi=         numeric lines per inch setting                  
##       -vsi=         Vertical Space Increment # in 1/48's of an inch 
##                                                                       
## Page Layout Selection                                                 
##	-lpp=	       set text length of page to nnn lines             
##                                                                       
##==============================================================================

# The basic way this script works is that it parses for our standard
# command line arguments and stores those values in some variables.
# Any arguments that aren't our standard ones are ignored and not used.
# Then, we look at the values we parsed out and convert those to PCL
# escape codes and echo them to STDOUT.  Once that is done, we route STDIN
# to STDOUT. We are basically just translating out standard command line
# arguments into PCL escape codes and sending them to TDOUT ahead of
# the report text to format the report.

#
# function to send escape codes and log them to the log file
#

SEND_CODE ()
{
    $ECHO $1
    echo "        Sending: $2" >> $log
}

# set up redirection of stderr

if [ -z "$LOGFILE" ]
then
    UMASK=`umask`; umask 00             # save and set the umask
    log="/tmp/PCL_filter.last"
    rm -rf $log
    echo "" > $log
    umask $UMASK                        # restore original umask                
else
    log=$LOGFILE
fi

exec 2>>$log

# do some logging

echo "" >> $log
echo "==========[ Starting filter ]================================================" >> $log
echo "`grep ^[#][$] $0|cut -b4-`" >> $log
echo "=============================================================================" >> $log
echo "" >> $log
echo "Filter Script Pathname: $0" >> $log
echo "Command line arguments: $*" >> $log
echo "" >> $log

# Determine OS related settings

echo "Determining what version of 'echo' is needed to send escape codes..." >> $log
if [ "`echo '\033\c'|wc -c`" -eq "1" ]
then
    ECHO="echo"
elif [ "`echo -e '\033\c'|wc -c`" -eq "1" ]
then
    ECHO="echo -e"
else
    echo "*** Error, can't determine how to get 'echo' to honor escape codes!" >> $log
    echo "==========[ Exiting filter ]================================================" >> $log
    exit 1
fi

echo "    Using '$ECHO' to send escape codes to the printer" >> $log
echo "" >> $log

# Initialize option variables to default values

orientation="def"
pagelen="def"
pagelen="def"
pitch="def"
lpi="def"
vsi="def"

# set up some defaults

defaultPortPitch="10"
defaultLandPitch="13"

defaultPortLpp="60"
defaultLandLpp="60"

defaultPortLpi="6"
defaultLandLpi="8"

# Determine which options have been invoked

count=$#

while [ $count -gt 0 ]
do
    case "$1" in
        #
        # Help
        #
        -[?]|--help)
                grep ^[#][#$] $0|cut -b3-
                exit 0
                ;;
        #
        # Version
        #
        --version)
                grep ^[#][$] $0|cut -b4-
                exit 0
                ;;
        #
        # Revision History
        #
        --history)
                grep ^[#][+] $0|cut -b4-
                exit 0
                ;;
	# 
	# Check for Page Orientation
	#
	-orientation=*) # select landscape orientation
	        orientation=`echo $1 | cut -f 2 -d = | tr "[A-Z]" "[a-z]"`
                ;;
        # 
        # Check for Pitch in Characters Per Inch
        #
        -pitch=* | -point=* | -cpi=*)
	        pitch=`echo $1 | cut -f 2 -d =`
                ;;
        #
        # Check for vertical line spacing
        #
	-lpi=*)	# lines per inch
	        lpi=`echo $1 | cut -f 2 -d =`
                ;;
	-vsi=*)	# 1/48th's of an inchs
	        vsi=`echo $1 | cut -f 2 -d =`
                ;;
        #
        # Check page layout settings
        #
	-lpp=*) # text lines per page	
		pagelen=`echo $1 | cut -f 2 -d =`
                ;;
	*)  #catch any non-processed arguments
                args="$args $1"
                ;;
    esac
    
    shift
    count=$(($count - 1))
done

echo "Current settings to use when formatting, "def" means Default." >> $log
echo "    Orientation: $orientation"   >> $log
echo "            Lpp: $pagelen"       >> $log
echo "          Pitch: $pitch"         >> $log
echo "            Lpi: $lpi"           >> $log
echo "            Vsi: $vsi"           >> $log
echo "Extra Arguments: $args"          >> $log
echo ""                                >> $log

#
# Set up printer default modes
#

echo "Printing Report ..." >> $log

SEND_CODE "\033E\c"    "Reset Printer"
SEND_CODE "\033)0B\c"  "Set Secondary character set to Linedraw"
SEND_CODE "\033&k2G\c" "Set line termination to CR=CR, LF=CR+LF, FF=CR+FF"

#
# Now convert the arguments into PCL escape codes
#

case "$orientation" in
    def)        # Do nothing, it wasn't set
                ;;
    port*)	SEND_CODE "\033&l0O\c" "Set printer to Portrait mode"

                if [ "$pagelen" = "def" -a "$lpi" = "def" ]
                then
                    pagelen="$defaultPortLpp"
                    lpi="$defaultPortLpi"
                fi

                if [ "$pitch" = "def" ]
                then
                    pitch="$defaultPortPitch"
                fi
                ;;

    land*)	SEND_CODE "\033&l1O\c" "Set printer to Landscape mode"

                if [ "$pagelen" = "def" -a "$lpi" = "def" ]
                then
                    pagelen="$defaultLandLpp"
                    lpi="$defaultLandLpi"
                fi

                if [ "$pitch" = "def" ]
                then
                    pitch="$defaultLandPitch"
                fi
                ;;
esac

case "$pitch" in 
    def)        # Do nothing, it wasn't set
                ;;
    *)          SEND_CODE "\033(s${pitch}H\c" "Set printer Pitch to $pitch"
                ;;
esac

case "$lpi" in
    def)        # Do nothing, it wasn't set
                ;;
    *)          SEND_CODE "\033&l${lpi}D\c" "Set printer Lpi to $lpi"
                ;;
esac

case "$vsi" in
    def)        # Do nothing, it wasn't set
                ;;
    *)          SEND_CODE "\033&l${vsi}C\c" "Set printer Vsi to $vsi"
                ;;
esac

case "$pagelen" in
    def)        # if $length has been set, use it.
#                if [ "$length" != "def" ]
#                then
#                    SEND_CODE "\033&l${length}F\c" "Set printer Page Length to $length"
#                fi
                ;;
    *)	        SEND_CODE "\033&l${pagelen}F\c" "Set printer Page Length to $pagelen"
                ;;
esac

# Now pass the print file data on through

echo "        Sending:  ** report file text to the printer **" >> $log

cat -

# wrap up with some printer reset options

SEND_CODE "\033E\c" "Reset Printer"

# all done

echo "" >> $log
echo "Report printing complete!" >> $log
echo "" >> $log

echo "==========[ Exiting filter ]================================================" >> $log

exit 0

#+###########################################################################
#+ Modification History
#+
#+ DATE      BY   Description
#+ --------  ---  ---------------------------------------------------------
#+ 03/21/01  PHB  Initial Creation
#+ 11/08/04  PHB  Added -point= option
#+###########################################################################
