#! /bin/sh
#
# Construct a PostScript file from an SGF file using sgf2misc
#
# Author: Jan van der Steen (jansteen@cwi.nl)
# Date	: Sat Jun 12 22:32:41 MDT 1993
#

#
# Where does sgf2misc create the EPSF files?
#
TMP=/tmp

#
# The necessary options (don't touch)
#
SGFOPTS="-to pstex -tmp ${TMP} -text -coord -dia 0 -fig %50 -scale 180"

filename=""
gamename="stdin"

while [ "@$1@" != "@@" ]
do
    case ${1} in
	-*)	SGFOPTS="${SGFOPTS} ${1}";;
	 *)	filename="${1}";; # Previous gets overwritten for the moment
    esac
    shift
done

if [ -f ${filename} ]
then
    cp ${filename} ${TMP}/$$.sgf
    filename=${TMP}/$$.sgf
    gamename=$$
fi

#
# Set a suitable PATH
#
PATH=.:/bin:/usr/bin

#
# Where are the prolog and trailer files?
#
PROLOG=./prolog.ps
TRAILER=./trailer.ps

#
# Where is the sgf2misc program?
#
SGFMISC=/ufs/jansteen/bin.sgi/sgf2misc

#
# Set the cleanup trap
#
trap 'rm ${TMP}/${gamename}.0* ${filename} 2>/dev/null' 0 1 2 3 9

#
# Create the EPSF files (redirect stderr to /dev/null as well)
#
${SGFMISC} ${SGFOPTS} ${filename} > /dev/null 2>&1

#
# Create the PostScript file from the PROLOG, TRAILER and EPSF files
#
cat ${PROLOG}
echo "Liberty begin /epsf false def end"
for epsf in ${TMP}/${gamename}.0*
do
    DIAGRAM="`grep Title ${epsf} | sed 's/%%Title: //'`"
    echo ${DIAGRAM} 1>&2
    grep -v "^%" ${epsf}
    echo "Liberty begin"
    echo "(`echo ${DIAGRAM} | sed -e 's/(/\\\\(/' -e 's/)/\\\\)/'`) caption"
    echo "end"
done
cat ${TRAILER}

exit 0
