#!/usr/bin/sh

args=""
while [ $# -gt 0 ]; do
	if [ -n "$(echo -n "$1" | grep -- "-sOutputFile=")" ]; then
		outfile="$(echo "$1" | sed "s/-sOutputFile=//")"
		args="$args -sOutputFile=$(realpath -- "$outfile")"
	elif [ -z "$(echo -n "$1" | grep ".*\.pcl")" ]; then
		args="$args $1"
	else
		if [ -z "$(echo "$args" | grep -- "-sOutputFile=")" ]; then
			tmpfile=$(mktemp)
			args="$args -sDEVICE=ps2write -sOutputFile=$tmpfile"
		fi
		args="$args $(realpath -- "$1")"
	fi
	shift
done
cd /usr/libexec/ghostpcl
./gpcl6 $args
if [ -n "$tmpfile" ]; then
	gv $tmpfile
	rm -f $tmpfile
fi
