#!/bin/sh
#
# pstojpeg:
# Turn postscript files into jpeg files for the web.
#

RESOLUTION=150
FACTOR=0.5	# $FACTOR * $RESOLUTION = nominal screen resolution
BORDER=4	# border to (try to) ensure that antialiasing doesn't break at the edge of the image

while test $# -ge 1 ; do
    FILE=$1
    
    if [ -e $FILE ] ; then
        # figure out name of the output jpeg file
        JPEG=`echo $FILE | sed 's/\.[^.]\+$/.jpg/'`

        echo -n converting $FILE into $JPEG... > /dev/stderr

        if [ -e $JPEG ] ; then
            echo output file $JPEG already exists
        else
            ( cat $FILE ; echo showpage ) | \
                ghostscript -q -r$RESOLUTION -sDEVICE=ppmraw -sOutputFile=- -dNOPAUSE -sPAPERSIZE=a2 - |\
                pnmcrop 2> /dev/null	| \
                pnmmargin $BORDER 	| \
                pnmscale $FACTOR	| \
                cjpeg > $JPEG
        fi

        echo done. > /dev/stderr
        else
        echo $FILE: does not exist > /dev/stderr
    fi

    shift 1
done
