helptext='
# usage: epco files
#
# epco: post optimization of programs generated by estra, which do not
#       consider the costs of function calls for subtrees within directives
#       to compute the cost of directives
#
'
: set -x
sysid=.
text="usage: epco files"

if test $# -eq 0
then
  echo "$helptext"
  exit 0
fi

trap 'rm -f epco[12]$$ /tmp/yyCost*; exit 1' 1 2 3 15

files="$*"

for file in $files
do
  if test ! -f $file
  then
    echo "epco: file $file does not exist" 1>&2
    rm -f epco[12]$$ /tmp/yyCost*
    exit 1
  fi
  if sed -f $sysid/epco.sed <$file >epco1$$
  then
    if sed \
	-e '/^      (\* \$1 \*)$/r /tmp/yyCostType' \
	-e '/^    (\* \$2 \*)$/r /tmp/yyCostInfo' \
       <epco1$$ >epco2$$
    then
      trap '' 1 2 3 15
      mv -f epco2$$ $file
      trap 'rm -f epco[12]$$ /tmp/yyCost*; exit 1' 1 2 3 15
    else
      rm -f epco[12]$$ /tmp/yyCost*
      exit 1
    fi
  else
    rm -f epco[12]$$ /tmp/yyCost*
    exit 1
  fi
done

rm -f epco[12]$$ /tmp/yyCost*
