#! /bin/sh
# @(#)ps2bits 1.2 89/12/12
# ps2bits - PostScript to Rasterfile converter.
#

USAGE="Usage: `basename ${0}` [-in s] [-out s] [-dpi x y] [-size w h] [-color]"
OUT="pspage"
IN="%stdin"
DPIX=72
DPIY=72
XSIZE=8.5
YSIZE=11
DEPTH=1

export OPENWINHOME XNEWSHOME LD_LIBRARY_PATH

OPENWINHOME="${OPENWINHOME-/usr/local/OpenWindows}"
XNEWSHOME=${OPENWINHOME}
LD_LIBRARY_PATH=${OPENWINHOME}/lib:/lib

if [ ! -f $OPENWINHOME/etc/NeWS/redbook.ps ]; then
    echo "`basename $0`: xnews is not installed correctly in $OPENWINHOME" 1>&2
    echo "	(set \$OPENWINHOME to where it is installed...)" 1>&2
    exit 1
fi

while [ ${#} -gt 0 ]; do
    case "${1}" in
	-color)	shift;
		DEPTH=8
	;;
	-out)	shift;
		OUT=${1} shift
	;;
	-in)	shift;
		IN=${1} shift
	;;
	-size)	shift;
		XSIZE=${1} shift;
		YSIZE=${1} shift
	;;
	-dpi)	shift;
		DPIX=${1} shift;
		DPIY=${1} shift
	;;
	*)	echo ${USAGE};
		exit 0
	;;
    esac
done

$OPENWINHOME/bin/xnews -init "
    /currentpacking false def
    /setpacking { pop } def
    (NeWS/basics.ps) (r) file cvx exec
    (NeWS/redbook.ps) (r) file cvx exec

    500 dict begin	% start userdict
	false setautobind
	/bind {} def

	/showpage { copypage erasepage initgraphics } def
	/_pageno 0 def
	/copypage {
	    /_pageno _pageno 1 add store
	    (${OUT})
	    clippath writecanvas
	} def

	${DPIX} ${XSIZE} mul ${DPIY} ${YSIZE} mul ${DEPTH}
	[ ${DPIX} 72 div 0 0 ${DPIY} 72 div neg 0 7 index ]
	null buildimage setcanvas
	erasepage initgraphics

	% hack for bug in folio initialization code.
	/Courier findfont 10 scalefont setfont () stringwidth pop pop

	(${IN}) (r) file cvx exec
    shutdownserver
"

