#!/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
#	-debug	Print debugging information
#
# 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.
# Sat Jan 19 15:50:54 EST 2002 - William C. Cheng <bill.cheng@acm.org>
#	Handles the "-debug" command line argument.
# Wed Sep  1 19:00:00 CDT 2010 - Carlo U. Segre <segre@iit.edu>
#	Reject ./file.eps as a valid input file name.  Patch from
#	Ryo Furue <furue@hawaii.edu> in Debian bug #451540

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

########################## Edit these variables #####################
GS='gs'
PSTOPPM='/usr/local/libdata/X11/gs/pstoppm.ps'
PBMTOEPSI='pbmtoepsi'
RASTTOPNM='/mm/bin/rasttopnm'
INTERP='gs'
STRIP='false'
HIRES='false'
PREVIEWBITMAP='true'
DEBUG='false'
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
            ;;
    -debug) DEBUG='true'
            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

case "$1" in
    *.ps);;
    *) echo $USAGE 1>&2
    exit 1
esac

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"
    if [ "" != "$ALADDIN" ] ; then
        if [ "3" = "$GSVERSION" -o "4" = "$GSVERSION" ] ; then
            STYLE="pstoppm"
            STYLE="ppm"
        fi
        if [ "5" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
        if [ "6" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
    else
        if [ "2" = "$GSVERSION" ] ; then
            STYLE="pstoppm"
        fi
        if [ "3" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
        if [ "4" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
        if [ "5" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
        if [ "6" = "$GSVERSION" ] ; then
            STYLE="ppm"
        fi
    fi

    if [ "true" = "$DEBUG" ] ; then
        echo ""
        echo "*******************************"
        echo "* Begin Debugging Information *"
        echo "*******************************"
        echo ""
        echo "GSCOPYRIGHT=$GSCOPYRIGHT"
        echo "ALADDIN=$ALADDIN"
        echo "GSVERSION=$GSVERSION"
        echo "STYLE=$STYLE"
        echo "GS=$GS"
        if [ "pstoppm" = "$STYLE" ] ; then
            echo "PSTOPPM=$PSTOPPM"
            echo "    (Note: PSTOPPM must be valid for this program to work)"
        fi
        if [ "ppm" = "$STYLE" ] ; then
            echo "PBMTOEPSI=$PBMTOEPSI"
            echo "    (Note: PBMTOEPSI must be valid for this program to work)"
        fi
        echo ""
        echo "*******************************"
        echo "*  End Debugging Information  *"
        echo "*******************************"
        echo ""
    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 -q -dNOPAUSE -sDEVICE=pbm -sOutputFile=${BASE}.ppm -- "$1"
        else
            $GS -q -dNOPAUSE -r144x144 -sDEVICE=pbm -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/01/19 21:13:12 william Exp william $
#
