#!/bin/bash
#
# converts an EPS file to a PDF file.

if [ $# -ne 2 ]; then
    echo 1>&2 "Usage: $0 infile.eps outfile.pdf"
    exit 1
fi

if ! test -r $1; then
    echo 1>&2 "Error: cannot read file $1"
    exit 2
fi

infile=$1
outfile=$2

set -- `grep '%%BoundingBox' $infile 2>/dev/null | head -1`
if [ $# -ne 5 ]; then
    echo "Cannot determine BoundingBox of the input file."
    exit 3
fi

llx=$2
lly=$3
w=$(( $4 - $2 ))
h=$(( $5 - $3 ))

#echo llx=$llx, lly=$lly, w=$w, h=$h

( echo "-$llx -$lly translate "
  cat $infile 
) | ps2pdf -g${w}x${h} -r72x72 - $outfile
