#################################
# Beginning HP tail script
#
# Print each file on command line
#################################

for i in $*
do
    #if [ -z "$title" ];then
        title="${request_id}"
    #fi
    ##########################
    # Do processing According to File Type
    ##########################

    if [ "$need_file_type_detection" -gt 0 ]
    then
        ##########################
        # Detect File Type
        ##########################
        PS_type=`file $i | grep -ic postscript`

        file $i | egrep -i 'ASCII|text|script|data' > /dev/null
        Text_type=`expr "$?" = 0`

####################################
#
# Update 01/20/2005
# PRN file: is_PRNFile >1
# PDF file: is_PDFFile=1
#
####################################

        is_PRNFile=`cat $i | grep -ic "%-12345X"`
        is_PDFFile=`cat $i | grep -ic "%PDF"`
    fi

    if [ "$PS_type" -gt 0 ]
    then
        ##########################
        # Check if the input file has %%EndSetup string in it
        ##########################
        is_DSC=`cat $i | grep -c "%EndSetup"`
        ##########################
        # Check if the input file has %%Title string in it
        ##########################
        is_TITLE=`grep "%%Title:" $i|sed 's/^%%Title:\(.*\)$/\1/'|tr -d ' \t'`
        ##########################
        # Check if is_DSC variable is empty
        ##########################
        case $is_DSC in
        1)
            if grep '^setup' $i > /dev/null
            then
                csplit -f $ps_tmp $i "/^setup/"
            else
                csplit -f $ps_tmp $i "/%%EndSetup/"
            fi
            cat $device_tmp >> $ps_tmp"00"

            printf "\033%%-12345X@PJL JOB\n" > $prtout_tmp
            if [ -z "${is_TITLE}" ];then
                echo "@PJL SET JOBNAME=\"${title}\"" >> $prtout_tmp
            fi
            echo "@PJL ENTER LANGUAGE = POSTSCRIPT" >> $prtout_tmp
            cat $ps_tmp"00" $ps_tmp"01" >> $prtout_tmp
            printf "\033%%-12345X@PJL EOJ" >> $prtout_tmp
            ;;
        *)
            printf "\033%%-12345X@PJL JOB\n" > $prtout_tmp
            if [ -z "${is_TITLE}" ];then
                echo "@PJL SET JOBNAME=\"${title}\"" >> $prtout_tmp
            fi
            echo "@PJL ENTER LANGUAGE = POSTSCRIPT" >> $prtout_tmp
            echo "%!PS-Adobe-3.0" >> $prtout_tmp
            echo "%%BeginSetup" >> $prtout_tmp
            cat $device_tmp >> $prtout_tmp
            echo "%%EndSetup" >> $prtout_tmp
            cat $i >> $prtout_tmp
            printf "\033%%-12345X@PJL EOJ" >> $prtout_tmp
            ;;
        esac

    elif [ "$Text_type" -gt 0 -a "$is_PRNFile" -eq 0 -a "$NoFilter_type" -eq 0 -a "$is_PDFFile" -eq 0 ]
    then
        ##########################
        # If text file, apply a filter to remove stair case effect
        ##########################
        if [ -s "$pjl_tmp" -o -s "$pcl_tmp" ]
        then
            printf "\033%%-12345X" > $prtout_tmp
            echo "@PJL JOB NAME=\"${title}\"" >> $prtout_tmp
            echo "@PJL SET JOBNAME=\"${title}\"" >> $prtout_tmp
            echo "@PJL SET TIME=\"$mytime\"" >> $prtout_tmp
            echo "@PJL SET DATE=\"$mydate\"" >> $prtout_tmp
            cat $pjl_tmp >> $prtout_tmp
            echo "@PJL ENTER LANGUAGE=PCL" >> $prtout_tmp
            printf "\033E" >> $prtout_tmp
            printf "\033&l%dA" $papersize >> $prtout_tmp

#################
#
# duplex_type of 2up print layout
#
##################
case "$duplex" in
off )
    duplex_type=0
    ;;
long )
    duplex_type=1
    ;;
short )
    duplex_type=2
    ;;
esac

###########################
#
#  2up print layout
#
###########################

            case $layout in
            0 )
                printf "\033&l%dO\033&k2G" $Orientation >> $prtout_tmp
                printf "\033(8U\033(s0p0s0b4099T" >> $prtout_tmp
                printf "\033(s%.2fH" $pitch >> $prtout_tmp
                cat $pcl_tmp >> $prtout_tmp


#################
#
# add
# printf "\r" >> $prtout_tmp
#
##################
                printf "\r" >> $prtout_tmp

                cat $i | awk '{print $0, "\r"}' >> $prtout_tmp
                ;;
            * )
                case $Orientation in
                0 )
                    # <<<<<<<<<<<<<<<<< PORTRAIT >>>>>>>>>>>>>>>>
                    printf "\033&l1O\033&k2G" >> $prtout_tmp
                    printf "\033(8U\033(s0p0s0b4099T" >> $prtout_tmp
                    cat $pcl_tmp >> $prtout_tmp
                    cat $i | awk -v w=$width -v h=$height -v p=$pitch -v d=$duplex_type 'BEGIN {
                                {b=h+50} {hw=w/2} {vmi=36.0/p+1.99} {max=int(h/(6.25*vmi))-2} {lf=max*2}
                                {reg=int(-w*6/5)} {cpi=p*1.62} {m=int(hw*cpi/300)-3}
                                {printf "\033(s%.2fH\033&l0e%.2fc%dF\033&a1l%dM",cpi,vmi,max+4,m}} {
                                ##### To fix TAB problem, Top Offset Registration Command has been used!!! ####
                                if (nlines == 0) {
                                    {printf "\033&l0Z"}
                                    {printf "\033*p0x50Y\033*c%da1b0P\033*p0x%dY\033*c0P",w,b}
                                    {printf "\033*p0x50Y\033*c1a%db0P\033*p%dx50Y\033*c0P\033*p%dx50Y\033*c0P",h,w,hw}
                                    {printf "\033*px100Y\n"}}
                                if (nlines++ < max) {print $0} else {print $0}
                                if (nlines == max) {printf "\033&l%dZ\033*p100Y\n",reg}
                                if (nlines == lf && d == 2) {reg = -reg}                   #FOR DUPLEX=SHORT
                                if (nlines == lf) {{print "\f"} {nlines = 0}}
                    }' >> $prtout_tmp
                    ;;
                1 )
                    # <<<<<<<<<<<<<<<<< LANDSCAPE >>>>>>>>>>>>>>>
                    printf "\033&l0O\033&k2G" >> $prtout_tmp
                    printf "\033(8U\033(s0p0s0b4099T" >> $prtout_tmp
                    cat $pcl_tmp >> $prtout_tmp
                    cat $i | awk -v h=$width -v w=$height -v p=$pitch 'BEGIN {
                                {b=h+50} {hh=h/2+50} {vmi=44.0/p+2.18} {max=int(h/(12.5*vmi))-3}
                                {lf=max*2-1} {cpi=p*1.35} {m=int(w*cpi/300)-1}
                                {printf "\033(s%.2fH\033&l0e%.2fc%dF\033&a1l%dM",cpi,vmi,lf+10,m}} {
                                if (nlines == 0) {
                                    {printf "\033*p0x50Y\033*c1a%db0P\033*p%dx50Y\033*c0P",h,w}
                                    {printf "\033*p0x50Y\033*c%da1b0P\033*p0x%dY\033*c0P\033*p0x%dY\033*c0P",w,b,hh}
                                    {printf "\033*px100Y\n"}}
                                if (nlines++ < max) {print $0} else {print $0}
                                if (nlines == max) {printf "\033*p%dY",hh+90}
                                if (nlines == lf) {{print "\f"} {nlines = 0}}
                    }' >> $prtout_tmp
                    ;;
                 2 )
                    # <<<<<<<<<<<<<<<<< RPORTRAIT >>>>>>>>>>>>>>>>
                    printf "\033&l3O\033&k2G" >> $prtout_tmp
                    printf "\033(8U\033(s0p0s0b4099T" >> $prtout_tmp
                    cat $pcl_tmp >> $prtout_tmp
                    cat $i | awk -v w=$width -v h=$height -v p=$pitch -v d=$duplex_type 'BEGIN {
                                {b=h+50} {hw=w/2} {vmi=36.0/p+1.99} {max=int(h/(6.25*vmi))-2} {lf=max*2}
                                {reg=int(-w*6/5)} {cpi=p*1.62} {m=int(hw*cpi/300)-3}
                                {printf "\033(s%.2fH\033&l0e%.2fc%dF\033&a1l%dM",cpi,vmi,max+4,m}} {
                                ##### To fix TAB problem, Top Offset Registration Command has been used!!! ####
                                if (nlines == 0) {
                                    {printf "\033&l0Z"}
                                    {printf "\033*p0x50Y\033*c%da1b0P\033*p0x%dY\033*c0P",w,b}
                                    {printf "\033*p0x50Y\033*c1a%db0P\033*p%dx50Y\033*c0P\033*p%dx50Y\033*c0P",h,w,hw}
                                    {printf "\033*px100Y\n"}}
                                if (nlines++ < max) {print $0} else {print $0}
                                if (nlines == max) {printf "\033&l%dZ\033*p100Y\n",-reg}
                                if (nlines == lf && d == 2) {reg = -reg}                   #FOR DUPLEX=SHORT
                                if (nlines == lf) {{print "\f"} {nlines = 0}}
                    }' >> $prtout_tmp
                    ;;
                3 )
                    # <<<<<<<<<<<<<<<<< RLANDSCAPE >>>>>>>>>>>>>>>
                    printf "\033&l2O\033&k2G" >> $prtout_tmp
                    printf "\033(8U\033(s0p0s0b4099T" >> $prtout_tmp
                    cat $pcl_tmp >> $prtout_tmp
                    cat $i | awk -v h=$width -v w=$height -v p=$pitch 'BEGIN {
                                {b=h+50} {hh=h/2+50} {vmi=44.0/p+2.18} {max=int(h/(12.5*vmi))-3}
                                {lf=max*2-1} {cpi=p*1.35} {m=int(w*cpi/300)-1}
                                {printf "\033(s%.2fH\033&l0e%.2fc%dF\033&a1l%dM",cpi,vmi,lf+10,m}} {
                                if (nlines == 0) {
                                    {printf "\033*p0x50Y\033*c1a%db0P\033*p%dx50Y\033*c0P",h,w}
                                    {printf "\033*p0x50Y\033*c%da1b0P\033*p0x%dY\033*c0P\033*p0x%dY\033*c0P",w,b,hh}
                                    {printf "\033*px100Y\n"}}
                                if (nlines++ < max) {print $0} else {print $0}
                                if (nlines == max) {printf "\033*p%dY",hh+90}
                                if (nlines == lf) {{print "\f"} {nlines = 0}}
                    }' >> $prtout_tmp
                    ;;
                esac
                ;;
            esac
            printf "\033%%-12345X@PJL EOJ\n" >> $prtout_tmp
            printf "\033%%-12345X" >> $prtout_tmp
        fi
    else
    ##########################
    # If other file type, send to print directly
    ##########################

        cat $i > $prtout_tmp
    fi

    ##########################
    # Want to use Filter Banner Page?
    ##########################
    if [ "$Custom_Banner" -eq 0 ]
    then
    #cat $prtout_tmp | lp -d $REMOTE_PRINTER
##########################################
#
# copy data to spool file $i
#
#########################################
        cp -f $prtout_tmp $i
        else
        print_file_name=`basename $i`
        version=26.0.0
        generate_custom_banner

###################################
#
# This section for HP banner page printing
# Banner Page buffer uses $banner_text_tmp
#
###################################
        printf "\033%%-12345X" > $banner_page_tmp
        echo "@PJL JOB NAME=\"FilterBanner\"" >> $banner_page_tmp
        echo "@PJL SET JOBNAME=\"FilterBanner\"" >> $banner_page_tmp
        cat $banner_pjl_tmp >> $banner_page_tmp
        
        echo "@PJL ENTER LANGUAGE=PCL" >> $banner_page_tmp
	printf "\033E" >> $banner_page_tmp
	printf "\033&l%dA" $papersize >> $banner_page_tmp
	cat $banner_pcl_tmp >> $banner_page_tmp
	printf "\033&l%dO" $Orientation >> $banner_page_tmp
	printf "\n" >> $banner_page_tmp
        
        cat $banner_text_tmp >> $banner_page_tmp
        printf "\033%%-12345X@PJL EOJ NAME=\"FilterBanner\"\n" >> $banner_page_tmp
        printf "\033%%-12345X" >> $banner_page_tmp
	cat $prtout_tmp >> $banner_page_tmp
	cp -f $banner_page_tmp $i
    fi
done
#####################################
#
# HP rlp command. Send print job
#
####################################
    /usr/sbin/rlp -I$request_id $i

##########################
# Remove temporary files if used DEBUG option
##########################
if test -z "$DEBUG"
then
    if test -f "$device_tmp"
    then
        /usr/bin/rm $device_tmp
    fi

    if test -f "$prtout_tmp"
    then
        /usr/bin/rm $prtout_tmp
    fi

    if test -f $ps_tmp"00"
    then
        /usr/bin/rm $ps_tmp"00"
    fi

    if test -f $ps_tmp"01"
    then
        /usr/bin/rm $ps_tmp"01"
    fi

    if test -f $pjl_tmp
    then
        /usr/bin/rm $pjl_tmp
    fi

    if test -f $pcl_tmp
    then
        /usr/bin/rm $pcl_tmp
    fi

    if test -f $banner_text_tmp
    then
        /usr/bin/rm $banner_text_tmp
    fi

    if test -f $banner_ps_tmp
    then
        /usr/bin/rm $banner_ps_tmp
    fi
    if test -f $banner_ps_pt_tmp
    then
        /usr/bin/rm $banner_ps_pt_tmp
    fi
    if test -f $banner_ps_ed_tmp
    then
        /usr/bin/rm $banner_ps_ed_tmp
    fi

    if test -f $banner_pcl_tmp
    then
        /usr/bin/rm $banner_pcl_tmp
    fi

    if test -f $banner_pjl_tmp
    then
        /usr/bin/rm $banner_pjl_tmp
    fi

    if test -f $banner_page_tmp
    then
        /usr/bin/rm $banner_page_tmp
    fi
fi

exit_code=0 exit 0
########## END OF Device Option PROCESS #########################
