#!/bin/sh
# Script to convert an arbitrary PostScript image to a cropped GIF image
# suitable for incorporation into HTML documents as inlined images to be
# viewed with Xmosaic.
#
# This is a modified version of the pstoepsi script 
# by Doug Crabill dgc@cs.purdue.edu
#
# Note in the USAGE line below, the source PostScript file must end
# in a .ps extention.  This is a GhostScript requirement, not mine...
#
# This software is provided without any guarantee.
#
# Nikos Drakos, nikos@cbl.leeds.ac.uk
# Computer Based Learning Unit, University of Leeds.
#
# Tue Jun 8 13:11:53 BST 1993

USAGE="Usage: $0 <file>.ps <file>.gif"

########################## Edit these variables #####################
GS='/usr/bin/X11/gs'
PSTOPPM='pstoppm.ps'
PBMPLUSLIB='/usr/cblsunb/rodw/src/pbmplus'
######################################################################

BASE=`basename "$1" .ps`

if [ $# -ne 2 -o ! -f "$1" -o "$1" = "$BASE" ] ; then
	echo $USAGE 1>&2
	exit 1
fi

trap 'rm -f ${BASE}.ppm; exit' 1 2 3 4 13 15

# This also scales up the postscript by about 20%
$GS -q -dNODISPLAY $PSTOPPM << ND
100 100	 ppmsetdensity
($BASE) ppm1run
ND

if test -f ${BASE}.ppm 
	then $PBMPLUSLIB/pnm/pnmcrop ${BASE}.ppm | $PBMPLUSLIB/ppm/ppmtogif - > $2
else for i in `ls ${BASE}.[1-9]*ppm`
	do $PBMPLUSLIB/pnm/pnmcrop $i | $PBMPLUSLIB/ppm/ppmtogif - > `echo $i |sed 's/\.\(.*\)ppm/\1\.gif/'`;
	echo "Writing `echo $i |sed 's/\.\(.*\)ppm/\1\.gif/'`"
     done
fi

rm -f ${BASE}.ppm
rm -f ${BASE}.[1-9]*ppm

exit 0
