#! /bin/sh
LATEX=latex
DVIPS=dvips
GS=gs
RESOLUTION=72 # in dots per inch. Chosen to be compatible with previous imagen.
PNMCROP="pnmcrop"
PNMEXTRA=""
PNMMARGIN="| pnmmargin -white 1"
PPMQUANT=""
PPMTOEXT="| pnmtopng -transparent '#ffffff'"	# default here is PNG
EXT=png
RM="/bin/rm -f"
MV="/bin/mv -f"

while true
do
    case $1 in
     -gif)
	PPMTOEXT="| ppmtogif -transparent '#ffffff'"
        EXT=gif
        ;;
     -png)
        PPMTOEXT="| pnmtopng -transparent '#ffffff'"
        EXT=png
        ;;
     -pnm)
        PPMTOEXT=""
        EXT=pnm
        ;;
     -quant)
        shift
        PPMQUANT="| ppmquant $1"
        ;;
    -extra)
        shift
        PNMEXTRA="| $1"
        ;;
    -mag)
	# compatibility with previous version of imagen
        shift
	RESOLUTION="$( expr '(' "$1" '*' 72 '+' 999 ')' '/' 1000)"
        ;;
    -res)
        shift
        RESOLUTION="$1"
        ;;
    *)
        break
        ;;
    esac
    shift
done

#always quantize for gifs (ppmtogif ouputs non valid files when colors >= 256)
if [ "$EXT" = "gif" -a -z "$PPMQUANT" ]
then
  PPMQUANT="| ppmquant 255"
fi

case $1 in
  '')
   BASE=image
   ;;
  *)
   BASE=$1
  ;;
esac
NAME=${BASE}.image

DVIPSOPTS="-Ppdf -D$RESOLUTION" # -Ppdf enables vector fonts
GSOPTS="-q -dNOPAUSE -dBATCH -DNOPROMPT -sDEVICE=ppmraw \
	-r$RESOLUTION -dTextAlphaBits=4"

test -f  ${NAME}.tex ||\
{ echo "Failure: no ${NAME}.tex file!" 2>&1 ; exit 2 ; }
${LATEX} ${NAME}.tex
${DVIPS} ${DVIPSOPTS} -o - ${NAME}.dvi |
${GS} ${GSOPTS} -sOutputFile="|\
    ${PNMCROP} ${PNMEXTRA} ${PNMMARGIN} ${PPMQUANT} ${PPMTOEXT} \
    > ${NAME}.${EXT} && ${MV} ${NAME}.${EXT} ${BASE}%03d.${EXT}" -
    #
    # Note: ${MV} ensures atomicity of picture update.
    # Please don't "optimize" it away with direct garbling of picture.
    #
    # BUG: the basename BASE may not contain spaces or any characters
    # that has a special meaning to the shell. If this is a problem,
    # replace the last line with the two following lines:
    #	> imagen.out && ${MV} imagen.out \
    #	$(perl -e 'print(quotemeta($ARGV[0]))' -- "${BASE}")%03d.${EXT}" -
    # That'll fix the current bug, but then you'll have a bigger bug
    # (in the opinion of many, including the author): you'll be using perl.
    # A nicer solution would be to include with hevea a utility "quotemeta"
    # written in ocaml that would by default do the equivalent of
    # perl -e 'print(join(" ",map(quotemeta,@ARGV)))' -- $@
    # that quotemeta utility could be latter extended with options to
    # select the "meta" according to which to quote, including features to
    # handle non-printable characters by producing $(echo -e ...), etc.
    # Hence, you'd invoke it with
    # $(${QUOTE} ${QUOTE_OPTIONS} -- "${BASE})
    #
    # Security warning: you don't want to build a server where imagen
    # could be invoked with malicious arguments: filename (unless quoted),
    # options (*not* just -extra), and (of course) the underlying TeX code
    # are all potential backdoors.

# Clean up our mess
${RM} ${NAME}.dvi ${NAME}.log ${NAME}.aux ${NAME}.out head.tmp body.tmp



