#!/bin/sh -
#
# calc <expr ... spaces allowed>: runs the Haskell system, "hugs",
# on the given expression.
#
progname=`basename $0`


#
#	argument checking
#
if [ $# -eq 0 ]; then
	echo "usage: $progname <expression ... spaces allowed>" >&2
	exit 1
fi


#
#	First, remove everything up to and including the pattern
#	'^Type :? for help$',
#	which (since we're running this without a script to give errors)
#	we can assume *will* appear, and will act as a boundary marker
#	between the preliminary loading and parsing waffle and the output
#	we really want.
#
#	Next, delete any lines starting with the Haskell system prompt
#	and ending with '[Leaving Hugs]'.
#	(There will usually be one such line, namely the final one.)
#
#	Finally, replace any other occurrences of the Haskell system prompt
#	by the null string.
#
#	(Note: we set the Haskell system prompt to a special distinctive value,
#	to make it unlikely that there will be any confusion with genuine
#	Haskell system result outputs.)
#
#	Note: the Haskell system has tcsh-style filename completion!
#	Therefore, the indentation in the here-document below is spaces
#	rather than a tab.
#
hugs -p"HASKELL_SYSTEM_PROMPT>" << here | addnl | sed -e '1,/^Type :? for help$/d' -e '/^HASKELL_SYSTEM_PROMPT>.*\[Leaving Hugs\]$/d' -e 's/HASKELL_SYSTEM_PROMPT>//g'
  $*
here

exit $?
