#!/bin/sh
trap '/bin/rm -f ${inFile} ${inFileHeader} > /dev/null 2>&1' 0

######################################################################
#
# Copyright 2008, 2009, 2010  Oki Data Corporation
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#
######################################################################

PATH=/bin:/usr/sbin:/usr/bin
export PATH

CAT="/bin/cat"
CUT="/usr/bin/cut"
ECHO="/bin/echo"
GREP="/bin/grep"
SED="/bin/sed"
TR="/usr/bin/tr"

PASSWD="/etc/passwd"

GSCRIPT="/usr/bin/gs"
RASTERTOHP="/usr/lib/cups/filter/rastertohp"

GSCRIPT_LIB="${CUPS_FONTPATH:=/usr/share/cups/fonts}"
export GSCRIPT_LIB

if [ ! -x "${RASTERTOHP}" ] ; then
	${ECHO} "ERROR: ${RASTERTOHP} not executable." 1>&2
	exit 1
fi

tmp_name="rastertookimonochrome"
inFile="/tmp/${tmp_name}.$$"

inFileHeader="${inFile}.header"

_jobid="${1}"
_user="${2}"
_title="${3}"
_options="${5}"
mediaType=""
manual_feed="@PJL SET MANUALFEED=OFF"

tmp_id=`${GREP} "${_user}" ${PASSWD}| ${CUT} -d":" -f3 2> /dev/null`
_id=`${ECHO} ${tmp_id:-999999988} | ${SED} -e "s/.*\([0-9]\{9\}\)/\1/"`

OKIJOB_ACCT="@PJL OKIJOBACCOUNTJOB JOBACCOUNTID=${_id} USERID=\"${_user}\" JOBNAME=\"${_title}\""

${CAT} - > ${inFile}

GS_OPTIONS="-dQUIET -dDEBUG -dPARANOIDSAFER -dNOPAUSE -dBATCH"
GS_OPTIONS="${GS_OPTIONS} -dNOMEDIAATTRS -sDEVICE=cups -sstdout=%stderr"

${ECHO} INFO: Starting Ghostscript `${GSCRIPT} --version`... 1>&2
${ECHO} DEBUG: Running ${GSCRIPT} $GS_OPTIONS -sOUTPUTFILE="%stdout" "${inFile}" 1>&2

${ECHO} "INFO: options = [${_options}] " 1>&2

## user options (* -oName=Value *) ##
for i in `${ECHO} ${_options} | ${TR} '[:lower:]' '[:upper:]'`
do
	case ${i} in
		PAGESIZE=*)
			media=$(${ECHO} ${i} | ${CUT} -d"=" -f2| ${SED} 's/ENV//' 2> /dev/null)
			PAPER="@PJL SET PAPER=${media}"

		;;
		OKMEDIATYPE=*)
			mediaType=`${ECHO} ${i} | ${CUT} -d"=" -f2 2> /dev/null`
		;;
		*)
			${ECHO} "INFO: invalid options = [${i}] " 1>&2
		;;
	esac
done

if [ -f "${PPD}" ] ; then

	InputSlot=$(${GREP} '^*DefaultInputSlot:' ${PPD} | ${CUT} -d" " -f2  2> /dev/null)
	
	case "${InputSlot}" in
		EnvFeed) InputSlotKey=1 ;;
		Upper)   InputSlotKey=2 ;;
		Lower)   InputSlotKey=3 ;;
		*) InputSlotKey=1 ;;
	esac


	if [  "${InputSlotKey}" = 1 ] ; then
		manual_feed="@PJL SET MANUALFEED=ON"
	fi

	if [ -z "${media}" ] ; then
		media=$(${GREP} '^*DefaultPageSize:' ${PPD} | ${CUT} -d" " -f2 | ${SED} 's/Env//' | ${TR} '[:lower:]' '[:upper:]' 2> /dev/null)
		PAPER="@PJL SET PAPER=${media}"
	fi
	
	if [ -z "${mediaType}" ] ; then
		mediaType=$(${GREP} '^*DefaultOKMediaType' ${PPD} | ${CUT} -d" " -f2 | ${TR} '[:lower:]' '[:upper:]' 2> /dev/null)
	fi

fi

mediaType="${mediaType:-PRINTERDEFAULT}"
mediaTypeKey="1Wd"

case "${mediaType}" in
	LIGHT|MEDIUM*|HEAVY) mediaTypeKey="6WdPlain" ;;
	LABEL*) mediaTypeKey="7WdLabels" ;;
	TRANSPA*) mediaTypeKey="13WdTransparency" ;;
	*) mediaTypeKey="1Wd" ;;
esac


mediaTypeSequence="`${ECHO} -e "\033"`\&n${mediaTypeKey}"

${CAT} ${inFile} | ${SED} "/@PJL ENTER/ i\
${OKIJOB_ACCT}
/@PJL ENTER/ i\
${manual_feed}
/@PJL ENTER/ i\
${PAPER}
s/INTRAY_XX/INTRAY${InputSlotKey}/ 
/LANGUAGE/q
"  >  ${inFileHeader}

if [ ! -s "${inFileHeader}" ] ; then
	${CAT} ${inFile} | ${SED} "/INTRAY_XX/d
/@PJL ENTER/ i\
${OKIJOB_ACCT}
/LANGUAGE/q
"  >  ${inFileHeader}
fi

${CAT} ${inFileHeader}

exec ${GSCRIPT} ${GS_OPTIONS} -sOUTPUTFILE="%stdout" "${inFile}" | ${RASTERTOHP} "${1}" "${2}" "${3}" "${4}" "${5}" |${SED} "s/&l\([0-9]*\)P/${mediaTypeSequence}/"

${ECHO}
${ECHO} -e "\033%-12345"

exit 0
