

##########################
#
# 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\``
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 ]
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
		cat $pcl_tmp >> $prtout_tmp; echo >> $prtout_tmp
		cat $i | awk '{print $0, "\r"}' >> $prtout_tmp
		printf "\033%%-12345X" >> $prtout_tmp
	else
		cat $i | awk '{print $0, "\r"}' > $prtout_tmp
	fi

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

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

#cat $prtout_tmp

cat $prtout_tmp | /usr/bin/qprt -P${RMT_PRINTER_NAME}

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

fi

exit_code=0 exit 0
