#!/bin/sh

# Copyright (c) 2003 KONICA MINOLTA BUSINESS TECHNOLOGIES, INC. All rights reserved.


#
# Trap signals so we can clean up the temp file
#

trap "rm $PROLOGUE; exit 2" 1 2 3 15

#
# Default variable values
#

COPIES=1
INPUTSOURCE=0
INPUTTYPE=0
TSET=on
HSET=on
TRAILERSOURCE=0
TRAILERSET=0
HEADERSET=0
HEADERSOURCE=0

#
# Define the PROLOGUE file and open it with the standard beginning
#

PROLOGUE=/tmp/$$qprtmp
echo "%!" > $PROLOGUE

#
# Parse off the command line
#

for i
do	case $1 in
	-#)
	    COPIES=${2-$COPIES}
	    shift
	    ;;
	-#*)
	    COPIES=`echo $1 | sed "s/^..//"`
	    ;;
	-d)
	    DUPLEX=yes
	    ;;
	-t)
	    TUMBLE=yes
	    ;;
	-ls)
	    LANDSCAPE=yes
	    ;;
	-l)
	    if [ $# -lt 2 ]
	    then
	        echo "Usage: qpr [-P printer] [-# copies] [-d] [-t] [-m] [-l language] [-s] [-c] [-o bin] [-R page page] [-ls] [-p] [-M left top right bottom alternate] [-B width] [-r resolution] [-D argument] [-f name] [-G col row col_space row_space order] [-K numpaper fold inner-margin thickness] [-S pagesize] [-i input_bin] [-h on/off] [-H input_bin] [-tron/off] [-ts input_bin] [filename...]"

		rm $PROLOGUE
		exit 1
	    fi
	    LANGUAGE=$2
	    shift
	    ;;
	-l*)
	    LANGUAGE=`echo $1 | sed "s/^..//"`
	    ;;
	-P)
	    LP_FLAGS="$LP_FLAGS -d$2"
	    shift
	    ;;
	-P*)
            TMP=`echo $1 | sed "s/^..//"`
	    LP_FLAGS="$LP_FLAGS -d $TMP"
	    ;;
	-s)
	    LP_FLAGS="$LP_FLAGS -s"
	    ;;
	-m)
	    LP_FLAGS="$LP_FLAGS -m"
	    ;;
#
# New added features 
#
	-c) 
	    COLLATE=yes
	    ;;
	-o)
	    OUTPUT=yes
	    WHERE=$2
	    shift
	    ;;
	-o*)
	    OUTPUT=yes
	    WHERE=`echo $1 | sed "s/^..//"`
	    ;;

	-R)
	    RANGE=yes
	    FIRST=$2
	    LAST=$3
	    shift
	    shift
	    ;;
	-p)	
	    PORTRAIT=yes
	    ;;

	-M)
	    MARGINS=yes
	    LM=$2
	    TM=$3
	    RM=$4
	    BM=$5
	    ALTERNATE=$6
	    shift
	    shift
	    shift
	    shift
	    shift
	    ;;
	-B)
	    BORDER=yes
	    SETTING=$2
	    shift
	    ;;
	-B*)
	    BORDER=yes
	    SETTING=`echo $1 | sed "s/^..//"`
	    ;;

	-b)
	   DEBUG=yes
	   ;;

	-r)
	    RESOLUTION=yes
		RESVAL=$2
		shift
	    ;;

	-r*)
	    RESOLUTION=yes
	    RESVAL=`echo $1 | sed "s/^..//"`
	    ;;
	-D)
	    STRING=$2
	    shift
	    ;;

	-f)
	    HEADERNAME=yes
	    FILENAME=$2
	    shift
	    ;;
	-f*)
	    HEADERNAME=yes
	    FILENAME=`echo $1 | sed "s/^..//"`
	    ;;
	-G)
	    GRID=yes
	    COLS=$2
	    ROWS=$3
	    COL_SPACE=$4
	    ROW_SPACE=$5
	    ORDER=$6
	    shift
	    shift
	    shift
	    shift
	    shift
	    ;;
	-K)
	    BOOKLET=yes
	    NUMPAPER=$2
	    FOLD=$3
	    INNERMAR=$4
	    THICKNESS=$5
	    shift
	    shift
	    shift
	    shift;
	    ;;
	-S)
	    PAGESIZE=yes
	    NUMOPAGES=$2
	    shift
	    ;;
	-S*)
	    PAGESIZE=yes
	    NUMOPAGES=`echo $1 | sed "s/^..//"`
	    ;;

	-i)
	    INPUTTYPE=1
	    TYPE=$2 
	    shift
	    ;;
	-i*)
	    INPUTTYPE=1
	    TYPE=`echo $1 | sed "s/^..//"` 
            ;;

	-I)
	    INPUTSOURCE=1
	    SOURCES=$2
	    shift
	    ;;
	-I*)
	    INPUTSOURCE=1
	    SOURCES=`echo $1 | sed "s/^..//"`
	    ;;

	-h) 
	    HEADERSET=1
	    HSET=$2
	    shift
	    ;;
	-h*) 
	    HEADERSET=1
	    HSET=`echo $1 | sed "s/^..//"`
	    ;;
	-H)
	    HEADERSOURCE=1
	    HSOURCE=$2 
	    shift
	    ;;
	-H*)
	    HEADERSOURCE=1
	    HSOURCE=`echo $1 | sed "s/^..//"`
	    ;;

	-tr)
	    TRAILERSET=1
	    TSET=$2 
	    shift
	    ;;
	-tr*)
	    TRAILERSET=1
	    TSET=`echo $1 | sed "s/^...//"`
	    ;;

	-ts) 
	    TRAILERSOURCE=1
	    TSOURCE=$2
	    shift
	    ;;
	-ts*) 
	    TRAILERSOURCE=1
	    TSOURCE=`echo $1 | sed "s/^...//"`
	    ;;
	-F)
	    PAGEOFFSET=yes
	    LEFT=$2
	    TOP=$3
	    shift
	    shift
	    ;;
	-C)
	    SCALING=yes
	    TURN=$2
	    shift
	    ;;
	-C*)
	    SCALING=yes
	    TURN=`echo $1 | sed "s/^..//"`
	    ;;
	-g)
	    BACKGROUND=yes
	    fname=$2
	    cycle=$3
	    unit=$4
	    shift
	    shift
	    shift
	    ;;
# End of new stuff
#

      
	-*)
	    echo "Usage: qpr [-P printer] [-# copies] [-d] [-t] [-m] [-l language] [-s] [-c] [-o bin] [-R page page] [-ls] [-p] [-M left top right bottom alternate] [-B width] [-r resolution] [-D argument] [-f name] [-G col row col_space row_space order] [-K numpaper fold inner-margin thickness] [-S pagesize] [-i input_bin] [-h on/off] [-H input_bin] [-tron/off] [-ts input_bin] [filename...]"
	    rm $PROLOGUE
	    exit 1
	    ;;
	*)
	    FILES=$*
	    break
	    ;;
	esac
	if test $# -gt 0
	then shift
	fi
done

#
# Add the necessary DCL to the prologue if needed
#

if [ "$COPIES" -gt 1 ]
then
    echo "%%IncludeFeature: numcopies ( $COPIES )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if [ "$DUPLEX" ]
then
    echo "%%IncludeFeature: duplex" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if [ "$TUMBLE" ]
then
    echo "%%IncludeFeature: duplex ( tumble )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi


if [ "$LANGUAGE" ]
then
    echo "%%IncludeFeature: emulation ( $LANGUAGE )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
#
#
# New Stuff added here
#
#
if [ "$COLLATE" ]
then
    echo "%%IncludeFeature: collate" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$OUTPUT" ]
then
    echo "%%IncludeFeature: output ( $WHERE )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$RANGE" ]
then
    echo "%%IncludeFeature: pagerange ( $FIRST $LAST )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$PORTRAIT" ]
then
    echo "%%IncludeFeature: orientation ( Portrait )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$LANDSCAPE" ]
then
    echo "%%IncludeFeature: orientation ( Landscape )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$MARGINS" ]
then
    echo "%%IncludeFeature: margins ( $LM $TM $RM $BM $ALTERNATE )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if [ "$BORDER" ]
then
    echo "%%IncludeFeature: border ( $SETTING )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$RESOLUTION" ]
then
    echo "%%IncludeFeature: resolution ( $RESVAL )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$PAGEOFFSET" ]
then
    echo "%%IncludeFeature: pageoffsets ( $LEFT $TOP )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$SCALING" ] 
then
    echo "%%IncludeFeature: pagescaling ( $TURN )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$BACKGROUND" ] 
then
    echo "%%IncludeFeature: background ( $fname $cycle $unit  )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$GRID" ]
then
    echo "%%IncludeFeature: pagegrid ( $COLS $ROWS $COL_SPACE $ROW_SPACE $ORDER)" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$BOOKLET" ]
then
    echo "%%IncludeFeature: booklet ( $NUMPAPER $FOLD $INNERMAR $THICKNESS ) " >> $PROLOGUE
    NEEDPROLOGUE=yes
fi
if [ "$PAGESIZE" ]
then
    echo "%%IncludeFeature: pagesize ( $NUMOPAGES )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if  [ $INPUTSOURCE -eq 1 -o $INPUTTYPE -eq 1 ]
 then
       echo "%%IncludeFeature: input ( $TYPE $SOURCES )" >> $PROLOGUE
       NEEDPROLOGUE=yes
fi

if [ $HEADERSET -eq 1 -o $HEADERSOURCE -eq 1 ]
then
    echo "%%IncludeFeature: header ( $HSET $HSOURCE )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if [ $TRAILERSOURCE -eq 1 -o $TRAILERSET -eq 1 ]
then
    echo "%%IncludeFeature: trailer ( $TSET $TSOURCE )" >> $PROLOGUE
    NEEDPROLOGUE=yes
fi

if [ "$STRING" ]
then
    echo $STRING >> $PROLOGUE
    NEEDPROLOGUE=yes
fi


#
#If the debug switch is thrown then output the prologue file to the screen.
#

if [ "$DEBUG" ] 
then
	echo "Debug mode..."
else

#
# If the files are specified on the command line, pipe each one to lpr
# with the appropriate "Title" and any other "PROLOGUE"
#
    if [ "$FILES" ]
    then
     for i in $FILES
     do
        if test -f $i
        then
	       if [ "$HEADERNAME" ]
	       then
	         echo "%%Title: $FILENAME
%%For: $USER 
%%EndComments" | cat $PROLOGUE - $i | lp $LP_FLAGS
	       else
	         echo "%%Title: $i
%%For: $USER 
%%EndComments" | cat $PROLOGUE - $i | lp $LP_FLAGS
	       fi
        else
           echo "File \"$i\" not found"
        fi
     done
    else
#
# Pipe the stdin to lpr with any necessary "PROLOGUE"
#
      if [ "$NEEDPROLOGUE" ]
      then
	  cat $PROLOGUE - | lp $LP_FLAGS
      else
	  cat - | lp $LP_FLAGS
      fi
     fi
fi

#
# Output for Debug mode..
#

if [ "$DEBUG" ]
then
	cat $PROLOGUE
	echo "lpr $LP_FLAGS"
fi

#
# Remove the PROLOGUE file and exit
#

 rm $PROLOGUE

exit 0


