#!/bin/bash

# Generates golden results by running another Curry system.  This script
# outputs all program values, one per line.  It exits with status 0 if the
# program succeeds, whether or not any values are produced, otherwise non-zero.
# I/O actions are not allowed.

if [ $# != 2 ]; then
  echo 1>&2 "USAGE: `basename $0` currymodule goal"
  exit 1
fi

set +e
module="$1"
goal="$2"
if [ -n "$SPRITE_HOME" -a -e "$SPRITE_HOME/tools/pakcs" ]; then
  pakcs="$SPRITE_HOME/tools/pakcs"
else
  pakcs=pakcs
fi
{ stderr=$( { { $pakcs --nocypm -q :set safe :load "$module" :eval "$goal" :quit ; } 1>&3 ; } 2>&1 ) ; } 3>&1

# PAKCS returns 2 when there is no value.
if [ $? == 0 -o $? == 2 ]; then
  exit 0
else
  rv=$?
  echo 1>&2 "$stderr"
  exit $rv
fi
