#!/bin/sh
# handle fig file for latex, expects args %f %S
set -e			# die on any error
BASE=`basename $2 .lout`
IN=$2			# input file
TMP=$BASE.tmp		# temporary file
OUT=$BASE.eps		# output file

[ -f "$IN" ] || exit 1	# ensure our file exists
# make sure our temp file does not already exist
if [ -f "$TMP" ]; then
   echo "$0: $TMP is in the way" 1>&2
   exit 1
fi

cat >"$TMP" <<EOF
@SysInclude{fontdefs}
@SysInclude{langdefs}
@SysInclude{fig}
@SysInclude{eq}
@SysInclude{graph}
@SysInclude{dl}
extend @DocumentLayout
export @TextPlace @Text
def @PlainLayout
@Begin
def @TextPlace { @Galley }
def @Text into { @TextPlace&&preceding } right x 
{
 @InitialFont @Font @InitialBreak @Break @InitialColour @Colour
 @InitialLanguage @Language x
}
@End @PlainLayout
@Use{ @DocumentLayout }
@Use{ @PlainLayout }
@TextPlace
//
@Text @Begin

EOF

cat "$IN" >>"$TMP"

cat >>$TMP <<EOF

@End @Text

EOF

lout -EPS "$TMP" -o "$OUT"
rm -f "$TMP" "$SRC.ld" lout.li

echo \\includegraphics{$OUT}
