##########################
#
# Auto-detect input file type here
#
# If is_PS=1, input file is PostScript
# If is_PS=0, input file is text
# is_PS is overwritten, if user specified
#	input file type with "-o text" or "-o ps" option
#
##########################

for i in $*
do

if [ -n "$PS" ]
then
	is_PS=1
	is_TXT=0
elif [ -n "$TXT" ]
then
	is_TXT=1
	is_PS=0
elif [ -n "$PCL" ]
then
	is_PS=0
	is_TXT=0
else
	OS_NAME=`uname -s`
	is_PS=`file $i | grep -c PostScript`
	is_TXT=`expr \`file $i|grep -c text\` - \`file $i|grep -c data\``
	is_PRNFile=`cat $i | grep -ic "%-12345X"`
fi

##########################

if [ "$is_PS" -eq 1 ]
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"
		cat $ps_tmp"00" $ps_tmp"01" > $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
		;;
	esac

elif [ "$is_TXT" -eq 1 -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 PCL file, send to print directly
	#
	##########################

	cat $i > $prtout_tmp
fi
##########################
#
# Loop for printing
#
##########################

if [ "$Custom_Banner" -eq 0 ]
then
	/usr/lib/lpd/rembak $OPTIONS $prtout_tmp
	exit_code=$?
	if [ $exit_code != 0 ] 
	then
		echo "exit_code=$exit_code"
		exit $exit_code
        fi
else
	print_file_name=`basename $i`
	version=11.0
        generate_custom_banner
	cat $banner_ps_tmp > /tmp/spool_tmp
        cat $prtout_tmp >> /tmp/spool_tmp	
	`/usr/lib/lpd/rembak $OPTIONS /tmp/spool_tmp` 
	exit_code=$?
 	if [ $exit_code != 0 ]
	then
		echo "exit_code=$exit_code"
		exit $exit_code
	fi
fi

done

##########################
#
# Remove temporary files if DEBUG=empty
#
##########################

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 /tmp/spool_tmp
	then
		/usr/bin/rm /tmp/spool_tmp
	fi
fi
exit_code=0 exit 0
########## END OF Device Option PROCESS #########################
