#!/bin/sh

parse() {
if [ ! -r "$1" ]; then
	echo "${BASENAME}: Can't read config file '$1'!" >&2
	exit 1
fi
sed -e 's/#.*//;
	/^[[:space:]]*$/d;
	s/^[[:space:]]*//;
	s/[[:space:]]*$//;
	s/^\([^[:space:]=]*\)[[:space:]=]*\(.*\)/\1 \2/' "$1" |
while read var value
do
	case `echo ${var} | tr '[[:upper:]]' '[[:lower:]]'` in
	model)
		case "${value}" in
		EPL5[789]00L)
			echo "MODEL='${value}'"
			;;
		*)
			echo "${BASENAME}: Unknown model '${value}'!" >&2
			exit 1
			;;
		esac
		;;
	papersize)
		echo "PAPERSIZE='${value}'"
		;;
	dpi)
		case "${value}" in
		300|Class600|600|Class1200)
			echo "DPI='${value}'"
			;;
		*)
			echo "${BASENAME}: Unknown resolution '${value}'!" >&2
			exit 1
			;;
		esac
		;;
	ritech)
		case "${value}" in
		on|off)
			echo "RITECH='${value}'"
			;;
		*)
			echo "${BASENAME}: Unknown ritech setting '${value}'!" >&2
			exit 1
			;;
		esac
		;;
	density)
		case "${value}" in
		[12345])
			echo "DENSITY='${value}'"
			;;
		*)
			echo "${BASENAME}: Unknown density setting '${value}'!" >&2
			exit 1
			;;
		esac
		;;
	tonersave)
		case "${value}" in
		on|off)
			echo "TONERSAVE='${value}'"
			;;
		*)
			echo "${BASENAME}: Unknown tonersave setting '${value}'!" >&2
			exit 1
			;;
		esac
		;;
	*)
		echo "${BASENAME}: Unknown setting '${var}=${value}'!" >&2
		exit 1
		;;
	esac
done
}

BASENAME=`basename $0`
PREFIX=`dirname $0`
PREFIX=${PREFIX%/bin}
CONFIGFILE=${PREFIX}/etc/epsonepl.conf

case "$1" in
-f?*)
	CONFIGFILE=${1#-f}
	;;
esac

settings=`parse "${CONFIGFILE}"` || exit 1
eval "${settings}"

IJSPARMS=""

if [ -z "${MODEL}" ]; then
	echo "${BASENAME}: You must specify a model!" >&2
	exit 1
fi

  # possible values: EPL5700L, EPL5800L, EPL5900L
  # default: none, you have to specify a printer!

########################################### End of printer selection part #


# Start of paper size selection part ######################################

if [ -z "${PAPERSIZE}" ]; then
	echo "${BASENAME}: You must specify a papersize!" >&2
	exit 1
fi
  # possible values: a4, letter, legal ...
  #                  The full list is available in the Use.htm file of
  #                  the ghostscript documentation (see appendix).
  # default: none, you have to specify a paper size!

######################################## End of paper size selection part #


# Start of options part ###################################################

if [ -n "${DPI}" ]; then
  # possible values: 300, Class600, 600, Class1200
  # default: 600
  IJSPARMS="EplDpi=$DPI"
fi

if [ -n "${RITECH}" ]; then
  # possible values: on, off
  # default: on
  IJSPARMS="$IJSPARMS${IJSPARMS:+,}EplRitech=$RITECH"
fi

if [ -n "${DENSITY}" ]; then
  # possible values: 1, 2, 3, 4, 5
  # default: 3
  IJSPARMS="$IJSPARMS${IJSPARMS:+,}EplDensity=$DENSITY"
fi

if [ -n "${TONERSAVE}" ]; then
  # possible values: on, off
  # default: off
  IJSPARMS="$IJSPARMS${IJSPARMS:+,}EplTonerSave=$TONERSAVE"
fi

##################################################### End of options part #


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

while true
do
	case "$1" in
	-?*) OPTIONS="$OPTIONS $1" ;;
	*)  break ;;
	esac
	shift
done

if [ $# -lt 1 -o $# -gt 2 ]; then
	echo "Usage: ${BASENAME} [-f<config>] [options...] (input.[e]ps|-) [output.epl|-]" 1>&2
	exit 1
fi

infile="$1";

if [ $# -eq 1 ]
then
	case "${infile}" in
	  -)		outfile=- ;;
	  *.eps)	base=`basename "${infile}" .eps`; outfile="${base}.epl" ;;
	  *.ps)		base=`basename "${infile}" .ps`; outfile="${base}.epl" ;;
	  *)		base=`basename "${infile}"`; outfile="${base}.epl" ;;
	esac
else
	outfile="$2"
fi

exec gs -sPAPERSIZE=$PAPERSIZE -dFIXEDMEDIA \
-sProcessColorModel=DeviceGray -dBitsPerSample=1 \
-sDEVICE=ijs -sIjsServer=ijs_server_epsonepl \
-sDeviceManufacturer=Epson -sDeviceModel=$MODEL \
-sIjsParams="$IJSPARMS" \
-dIjsUseOutputFD \
-dQUIET -dNOPAUSE -dSAFER -dBATCH \
-sOutputFile="$outfile" "$infile"
