#!/bin/sh
# delete all auxiliary files of all Curry programs in a directory

RECURSIVE=no
if [ "xx$1" = "xx-r" ] ; then
  RECURSIVE=yes
  shift
fi

ALL=no
if [ "xx$1" = "xx-a" ] ; then
  ALL=yes
  shift
fi

if [ $# != 0 ]
then
  echo "Usage: $0 [-r] [-a]" >&2
  echo "-r: apply this command recursively to all subdirectories" >&2
  echo "-a: remove all auxiliary Curry files (even those without a source file)" >&2
  exit 1
fi

for F in *.curry *.lcurry
do
  if [ "$F" != "*.curry" -a "$F" != "*.lcurry" ] ; then
    F=`expr $F : '\(.*\)\.lcurry' \| $F`
    F=`expr $F : '\(.*\)\.curry' \| $F`
    rm -f $F.ast $F.cint $F.fl $F.def $F.pizza $F.acy $F.uacy $F.flc $F.fint $F.fcy $F.pl $F.po $F.pl.main $F.state $F.profile "$F"_flat.xml $F.icurry
    rm -f -r COOSYLOGS
    rm -f -r $F.classes
  fi
done
rm -f prelude.pizza

if [ $ALL = yes ] ; then
  for i in *.fcy # look for fcy files not deleted in the first step
  do
    F=`expr $i : '\(.*\).fcy'`
    if [ "$F" != "*" ] ; then
      rm -f $F.fcy $F.pl $F.po $F.pl.main $F.state $F.profile "$F"_flat.xml
    fi
  done
fi
 
# delete also .curry files if there is a corresponding .lcurry file:
for F in *.lcurry
do
  if [ "$F" != "*.lcurry" ] ; then
    F=`expr $F : '\(.*\)\.lcurry' \| $F`
    rm -f $F.curry
  fi
done

if [ $RECURSIVE = yes ]
then
  PATHNAME=`(cd \`dirname $0\` > /dev/null ; pwd)`
  for i in *
  do
    if test -d $i
    then
      (cd $i ; $PATHNAME/`basename $0` -r)
    fi
  done
fi
