#!/bin/sh
# pcl2pdf , and symlinks pcl2ps, pcl2fax, xps2pdf, xps2ps, xps2fax
# Convert PCL, PXL, or XPS to PDF, PS, or FAX
# brian@aljex.com
# - removed unnecessary externals and subprocesses like `basename ...`
# - stop pasting all opts twice just in case of -I
# - treat missing input or output filename same as "-", ie use stdin/stdout
# - add xps2*
# - combine *2* into one script

unset I O

case ${0##*/} in
	xps2*) ie=xps ex=gxps ;;
	*) ie=pcl ex=pcl6 ;;
esac

case ${0##*/} in
	*2fax) oe=fax od=tiffg3 O="-sPAPERSIZE=letter -dFIXEDMEDIA -r203.29x196" ;;
	*2ps) oe=ps od=ps2write ;;
	*) oe=pdf od=pdfwrite ;;
esac

usage () {
	echo "Usage: ${0##*/} [options...] [input.${ie}|-] [output.${oe}|-]" 1>&2
	exit 1
}

case "$1" in -h|--help) usage ;; esac

while : ; do
	case "$1" in
		-I?*) I=$1 ;;
		-?*) O+=" $1" ;;
		*)  break ;;
	esac
	shift
done

inp=$1
out=$2

case $# in
	0|2) : ;;
	1)
		case "$inp" in
			-|"") : ;;
			*.${ie}|*.pxl) out=${inp##*/} out=${out%%.}.$oe ;;
			*) out=${inp##*/}.$oe ;;
		esac
		;;
	*) usage ;;
esac

exec $ex $I -dQUIET -dSAFER -dNOPAUSE -dBATCH -sDEVICE=$od -sOutputFile=${out:--} $O ${inp:--}
