#!/bin/sh
# Script to convert an arbitrary PostScript image to an encapsulated
# PostScript image with a bitmap, suitable for incorporation into
# FrameMaker, LaTeX, troff, etc.
#
# Options:
#	-gs	Use ghostscript to convert to EPSI
#	-news	Use Sun's X/NeWS (OpenWindows) to convert to EPSI
#	-strip	Strip all %% directives from the source file -- useful
#		when dealing with Adobe Illustrator and others who
#		use directives incompatible with this coversion.
#	-2x	Use 2x resolution
#	-noprev	Do not include the preview bitmap
#
# Note in the USAGE line below, the source PostScript file must end
# in a .ps extention.  This is a GhostScript requirement, not mine...
#
# I am providing this without any guarantee.
#
# Thu Nov 29 10:57:05 EST 1990 - Doug Crabill dgc@cs.purdue.edu
#	Original release.
#
# Sun Jan 11 02:47:26 EST 1998 - William C. Cheng <bill.cheng@acm.org>
#	Handles Aladdin Ghostscript 5.x (no pstoppm.ps).
# Thu May 20 11:29:12 EDT 1999 - William C. Cheng <bill.cheng@acm.org>
#	Handles the "-2x" command line argument (needs netpbm-20may1999).
# Fri Sep  1 14:32:15 EDT 2000 - William C. Cheng <bill.cheng@acm.org>
#	Make the default STYLE to be "ppm".
# Fri Mar 23 11:18:17 PST 2001 - William C. Cheng <bill.cheng@acm.org>
#	Handles the "-noprev" command line argument.

USAGE="Usage: $0 [ -gs | -news ] [ -strip ] [ -2x ] [-noprev] \
<file>.ps <file>.epsi"

########################## Edit these variables #####################
GS='/usr/local/bin/gs'
PSTOPPM='/usr/local/libdata/X11/gs/pstoppm.ps'
PBMTOEPSI='/usr/local/bin/pbmtoepsi'
RASTTOPNM='/usr/local/bin/rasttopnm'
INTERP='gs'
STRIP='false'
HIRES='false'
PREVIEWBITMAP='true'
PSTORAST='./pstorast'
######################################################################

GSCOPYRIGHT=`$GS -help | grep Ghostscript`
ALADDIN=`echo $GSCOPYRIGHT | grep Aladdin`
GSVERSION=`echo $GSCOPYRIGHT | cut -d" " -f3 | cut -d"." -f1`

for A do
    case $A in
    -gs)    INTERP='gs'
            shift
            ;;
    -news)  INTERP='news'
            shift
            ;;
    -strip) STRIP='true'
            shift
            ;;
    -2x)    HIRES='true'
            shift
            ;;
    -noprev) PREVIEWBITMAP='false'
            shift
            ;;
    *)      break
            ;;
    esac
done

BASE=`basename "$1" .ps`

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

TMP1="/tmp/$USER.1.$$"
TMP2="/tmp/$USER.2.$$"
TMP3="/tmp/$USER.3.$$"
trap 'rm -f $TMP1 $TMP2 ${BASE}.ppm; exit' 1 2 3 4 13 15

if [ "true" = "$STRIP" ] ; then
    awk '/^%%/ { next } {print}' < $1 > $TMP3
else
    TMP3="$1"
fi

if [ "gs" = "$INTERP" ] ; then
    STYLE="none"
    STYLE="ppm"
    GSFLAGS="-q -dBATCH -dNOPAUSE -sDEVICE=pbm"
    if [ "" != "$ALADDIN" ] ; then
        if [ "3" = "$GSVERSION" -o "4" = "$GSVERSION" ] ; then
            STYLE="pstoppm"
            STYLE="ppm"
            GSFLAGS="-q -dNOPAUSE -sDEVICE=pbm"
        fi
        if [ "5" = "$GSVERSION" ] ; then
            STYLE="ppm"
            GSFLAGS="-q -dNOPAUSE -sDEVICE=pbm"
        fi
        if [ "6" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
    else
        if [ "2" = "$GSVERSION" ] ; then
            STYLE="pstoppm"
            GSFLAGS="-q -dNOPAUSE -sDEVICE=pbm"
        fi
        #############################################
        # if [ "$GSVERSION" >= 3] set STYLE = "ppm" #
        #############################################
    fi
    if [ "none" = "$STYLE" ] ; then
        echo Does not know how to handle "$GSCOPYRIGHT".
        exit 1
    fi
    if [ "pstoppm" = "$STYLE" ] ; then
        $GS -q -dNODISPLAY $PSTOPPM << DGC
        ($BASE) ppm1run
DGC
    fi
    if [ "ppm" = "$STYLE" ] ; then
        if [ "false" = "$HIRES" ] ; then
            $GS ${GSFLAGS} -sOutputFile=${BASE}.ppm -- "$1"
        else
            $GS ${GSFLAGS} -r144x144 -sOutputFile=${BASE}.ppm -- "$1"
        fi
    fi
    if [ "false" = "$HIRES" ] ; then
        $PBMTOEPSI ${BASE}.ppm > $TMP1
    else
        $PBMTOEPSI -scale 0.5 ${BASE}.ppm > $TMP1
    fi
    if [ "true" = "$PREVIEWBITMAP" ] ; then
        cat $TMP1 $TMP3 > $2
    else
        head -2 $TMP1 > $2
        cat $TMP3 >> $2
    fi
else
    $PSTORAST -in $TMP3 -out $TMP1
    $RASTTOPNM < $TMP1 | $PBMTOEPSI > $TMP2
    cat $TMP2 $TMP3 > $2
fi

if [ "$1" != "$TMP3" ] ; then
    rm -f $TMP3
fi
rm -f $TMP1 $TMP2 ${BASE}.ppm

exit 0

#
# @(#)$Id: pstoepsi,v 1.4 2002/07/11 17:38:09 william Exp $
#
