
########## END OF Device Option PROCESS #########################
for i in $*
do
#################################
#
# Update 01/20/2005 Detect PRN/PDF file
# PDF file: is_PDFFile=1
#
#################################

    title=`basename "$files"`
is_PDFFile=`cat $i | grep -ic "%PDF"`

if [ "$is_PS" -eq 1 ]
then
	printf "\033%%-12345X@PJL JOB\n" >$prtout_tmp
	if [ -s "$ps_pjl_tmp" ]
	then
		cat $ps_pjl_tmp >> $prtout_tmp
		echo "@PJL ENTER LANGUAGE=POSTSCRIPT" >> $prtout_tmp
	fi

	##########################
	#
	# Check if the input file has %%EndSetup string in it
	#
	##########################

	is_DSC=` grep -c "%EndSetup" $ps_tmp `

	##########################
	#
	# Check if is_DSC variable is empty
	#
	##########################

	case $is_DSC in

	1)

		if grep '^setup' $ps_tmp > /dev/null
		then
			csplit -q -f $ps_tmp $ps_tmp "/^setup/"
		else
			csplit -q -f $ps_tmp $ps_tmp "/%%EndSetup/"
		fi
		if [ -s $device_tmp ]
		then
		cat $device_tmp >> $ps_tmp"00"
		fi
		cat $ps_tmp"00" >> $prtout_tmp
		cat $ps_tmp"01" >> $prtout_tmp

		;;
	*)
		echo "%!PS-Adobe-3.0" >> $prtout_tmp
		echo "%%BeginSetup" >> $prtout_tmp
		if [ -s $device_tmp ]
		then
		cat $device_tmp >> $prtout_tmp
		fi
		echo "%%EndSetup" >> $prtout_tmp
		cat $ps_tmp >> $prtout_tmp
		;;
	esac
	printf "\033%%-12345X@PJL EOJ\n" >>$prtout_tmp
	printf "\033%%-12345X" >> $prtout_tmp
##########################
#
# Update at 01/20/2005
#
##########################
elif [ "$is_TXT" -eq 1 -a "$is_PRNFile" -eq 0 -a "$NoFilter" -eq 0 -a "$is_PDFFile" -eq 0 ]
then

	##########################
	#
	# If text file, apply a filter to remove stair case effect
	# if (layout != 0), 2up printing process(no 4up).
	#	  awk unix tool has been used for layout printing.
	##########################

	#cat $* | awk '{print $0, "\r"}' > $prtout_tmp

	if [ -s "$pjl_tmp" -o -s "$pcl_tmp" ]
	then
		printf "\033%%-12345X" > $prtout_tmp
		echo "@PJL SET JOBNAME=\"${title}\"" >> $prtout_tmp
		cat $pjl_tmp >> $prtout_tmp
		printf "\n" >> $prtout_tmp
		echo "@PJL ENTER LANGUAGE=PCL" >> $prtout_tmp
  		printf "\033E" >> $prtout_tmp
		printf "\033&l%dA" $papersize >> $prtout_tmp
################
#
# 2up printing
#
################
		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 "\n" >> $prtout_tmp
#
##################
			printf "\r" >> $prtout_tmp
			cat $ps_tmp | gawk '{print $0}' >> $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 $ps_tmp | gawk -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 $ps_tmp | gawk -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 $ps_tmp | gawk -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 $ps_tmp | gawk -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" >> $prtout_tmp
	fi

else
	##########################
	#
	# If PCL file, send to print directly
	#
	##########################

	cat $ps_tmp > $prtout_tmp
fi
	##########################
	#
	# Job seperation: after
	#
	##########################

	#printf "\033%%-12345X" >> $prtout_tmp

    if [ "$Custom_Banner" -eq 0 ]
    then
	cat $prtout_tmp #| gawk '{print $0, "\r"}'
    else
	print_file_name=`basename $files`
	version=31.0.0
        generate_custom_banner

#################################
#
# Print Baner page and Files
#
################################ 
       if [ "$is_TXT" -eq 1 ]
       then	
		cat $banner_ps_tmp #| gawk '{print $0, "\r"}'
        	cat $prtout_tmp #| gawk '{print $0, "\r"}'
       else
        	cat $banner_ps_tmp $prtout_tmp #| gawk '{print $0, "\r"}'
       fi    
    fi
done
##########################
#
# Remove temporary files if DEBUG=empty
#
##########################

if test -z "$DEBUG"
then
        if test -f "$device_tmp"
        then
                /bin/rm $device_tmp
        fi

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

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

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

        if test -f $ps_tmp
        then
                /bin/rm $ps_tmp
        fi

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

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


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

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

	if test -f $banner_ps_tmp
	then
	    /bin/rm $banner_ps_tmp
	fi
	
	if test -f $banner_pcl_tmp
	then
	    /bin/rm $banner_pcl_tmp
	fi
	
	if test -f $banner_pjl_tmp
	then
	    /bin/rm $banner_pjl_tmp
	fi
	
	if test -f $banner_page_tmp
	then
	    /bin/rm $banner_page_tmp
	fi
fi
