#!/bin/sh
#
# ps-to-hppj version 0.01
# Convert a PostScript file to a HP paintjet file.

dirname=`dirname $0`
progname=`basename $0`

device=setrgbhppjdevice
eps=0
xsize=1530
ysize=1980
dpi=180
copy=1
use_transparency=false
use_compression=true
isXLpaintjet=true

gps=$dirname/gps

while [ $# -gt 2 ]
do case "$1" in
    -eps)
	eps=1
	;;
    -size)
	shift; xsize=$1; ysize=$1
	;;
    -xsize)
	shift; xsize=$1;
	;;
    -ysize)
	shift; ysize=$1;
	;;
    -dpi)
	shift; dpi=$1;
	;;
    -copy)
	shift; copy=$1;
	;;
    -t | -transparency)
	use_transparency=true;
	;;
    -nc | -nocompression)
        use_compression=false
	;;
    -nxl | -notXLpaintjet)
        isXLpaintjet=false
	;;
    esac
    shift
done

if [ $# != 2 ] ; then
	cat << __END__

$progname: you must specify an input file and an output file.
usage: $progname [-xsize s] [-ysize s] [-size s] [-dpi s]
                 [-copy n] [-eps] [-t] [-nc] [-nxl] file outfile
  -xsize s: Set the x-size of the raster to \`s'.
  -ysize s: Set the y-size of the raster to \`s'.
  -size s:  Set the x-size and y-size of the raster to \`s'.
  -dpi s:   Set the resolution of the raster to \`s' dpi.
  -copy n:  Set the number of copies to \`n'.
  -t:       Use HP Paint Jet transparency mode.
  -nc:      No compression: don't attempt to compress image data.
  -nxl:     Not HP paintjet XL (don't drop 5/8" off the top of the page).
  -eps:     Treat this file as an EPS file.  If \`file' has the extension
	    \`.eps', this is the default.
__END__
	exit 1
fi

infile=$1
outfile=$2

if [ ! -r $infile ] ; then
	echo "$progname: \`$infile' does not exist."
	exit 1
fi

if [ $eps -ne 0 -o `expr $infile : '.*\.eps$'` -ne 0 ] ; then
	run="($dirname/eps-to-ps -b $infile) (r) pipe cvx exec"
else
	run="($infile) run"
fi

exec $gps -no-init -no-memstats -batch -e "
    { 
	%userdict /#copies $copy put

	($outfile) $xsize $ysize $dpi $copy $use_transparency
	$use_compression $isXLpaintjet $device

	($dirname/../fonts) setfontpath
	setfoliofontdevice
	($dirname/fnmconvers.ps) run
	%(Imaging ) print ($infile) print ( . . . ) print
	$run
	%(done\n) print
    } stopped {
	handleerror
    } if
"
if test $status ! -eq 0
then    exit 23
