##########################
# LNX_TUB_E
#
# SCO_tail Script
# Print each file on command line
#################################
for i in $*
do
    ##########################
    # Do processing According to File Type
    ##########################

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

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

	is_PRNFile=`cat $i | grep -ic "%-12345X"`
    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 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" > $prtout_tmp
	    cat $ps_tmp"00" $ps_tmp"01" >> $prtout_tmp
	    printf "\033%%-12345X" >> $prtout_tmp
	    ;;
	*)
	    printf "\033%%-12345X" > $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" >> $prtout_tmp
	    ;;
	esac

    elif [ "$Text_type" -gt 0 -a "$is_PRNFile" -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 SET JOB NAME=\"$title\"" >> $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

###########################
#
#  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
			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
				;;
			* )
				# <<<<<<<<<<<<<<<<< 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
				;;
			esac
			;;
		esac
	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 
    else
	print_file_name=`basename $i`
	version=11.00
        generate_custom_banner
	cat $banner_ps_tmp $prtout_tmp | lp -d $REMOTE_PRINTER 
    fi
done

##########################
# 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
fi

exit_code=0 exit 0 
##############################
#
# End of SCO_tail Script
#
##############################
