#! /bin/sh
# @(#)ps2bits 1.5 90/08/22
# ps2bits - PostScript to Rasterfile converter.
#

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

if [ "$OPENWINHOME" = "" ]; then
	OPENWINHOME=/usr/openwin
fi

if [ "$XPRESSFAX_HOME" = "" ]; then
	XPRESSFAX_HOME=`dirname $0`/..
	if [ "$XPRESSFAX_HOME" = "./.." ]; then
		XPRESSFAX_HOME=`pwd`/..
	fi
fi

RAS2TIFF=$XPRESSFAX_HOME/bin/xpressfax_ras2tiff

if [ "$LD_LIBRARY_PATH" = "" ]; then
	LD_LIBRARY_PATH=$OPENWINHOME/lib:$XPRESSFAX_HOME/lib
else
	LD_LIBRARY_PATH=$OPENWINHOME/lib:$XPRESSFAX_HOME/lib:$LD_LIBRARY_PATH
fi

PATH=/usr/bin:/usr/ucb:/bin:$OPENWINHOME/bin:$XPRESSFAX_HOME/bin

export OPENWINHOME LD_LIBRARY_PATH XPRESSFAX_HOME

while [ ${#} -gt 0 ]; do
    case "${1}" in
    -color) shift;
        DEPTH=8
    ;;
    -out)   shift;
        OUT=${1} shift
    ;;
    -merge) shift;
        MERGE=1
    ;;
    -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

#
# Comment the next three lines out if you would rather use the OpenWindows
# xnews software to convert your PostScript Images to faxes...
#
if [ -f $XPRESSFAX_HOME/utils/gsdir/pstofax.sh ]; then
	exec $XPRESSFAX_HOME/utils/gsdir/pstofax.sh ${IN} ${OUT}
fi

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

if [ "$MERGE" != "1" ]; then
	/bin/rm -rf ${OUT}
fi
# 
# Determine if we're running xnews OpenWin 2 or xnews OpenWin 3
#
VERSION=`/usr/ucb/strings $OPENWINHOME/bin/xnews | /bin/grep nodev | /usr/ucb/wc -l`
if [ $VERSION -eq 0 ]; then
$OPENWINHOME/bin/xnews -init "
    /currentpacking false def
    /setpacking { pop } def
    (NeWS/basics.ps) (r) file cvx exec
    (NeWS/redbook.ps) (r) file cvx exec
    (NeWS/statdict.ps) (r) file cvx exec

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

        /showpage { copypage erasepage initgraphics } def
        /copypage {
            (%stdout) (w) file
            newpath writecanvas flush
        } 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
" | ${RAS2TIFF} ${OUT}

else
$OPENWINHOME/bin/xnews -nodev -init "
    /beginpackage where {
    pop
    currentshared
    true setshared

    /NeWSops 50000 dict dup begin

        /PackageType    1 def
        /PackageName    /NeWSops def
        /MajorVersion   3 def
        /MinorVersion   0 def

        initNeWSoperators
        systemdict /initNeWSoperators undef

        /random { rand -16 bitshift 16#7fff div } def

        /FatalError { % string => (doesn't return)
        console exch writestring
        console flushfile
        shutdownserver
        } def

        /currentpacking false def
        /setpacking { pop } def
        (${OPENWINHOME}/etc/NeWS/basics.ps)   (r) file cvx exec
        (${OPENWINHOME}/etc/NeWS/redbook.ps)  (r) file cvx exec
        (${OPENWINHOME}/etc/NeWS/misc.ps)     (r) file cvx exec
        (${OPENWINHOME}/etc/NeWS/statdict.ps) (r) file cvx exec
    end definepackage beginpackage
    setshared
    } if

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

    /showpage { copypage erasepage initgraphics } def
    /copypage {
        (%stdout) (w) file
        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 2 scalefont setfont () stringwidth pop pop

    (${IN}) (r) file cvx exec
    shutdownserver
" | ${RAS2TIFF} ${OUT}
fi
exit 0
