#!/bin/sh

# try to be really safe...
echo $PWD | grep codeextract > /dev/null 2>&1
if [ $? -ne 0 ]
then
    echo "cannot execute in this directory"
    exit 1
fi

echo -n "working in '$PWD'.  okay? "
read answer
if [ "$answer" != "y" ]
then
    echo "quitting"
    exit 1
fi

find						\
	. 					\
	\(					\
	   -name '*.master'			\
	-o -name 'Makefile'			\
	-o -name '.emacs_[0-9]*'		\
	-o -name '*~'				\
	-o -name '#*'				\
	-o -name 'z*'				\
	\)					\
	-ok /bin/rm {} \;

# no need to include these...
/bin/rm -rf Common README \
            SIndent SIndent.icn SPatterns SPatterns.README SRemove

# now make sure not to give away solutions
# in any directory that contains a Given, delete all but Given

# remember directory in which began
HERE=$PWD

# find all the Given.
# pipe through "sed" to remove leading and trailing noise.
# read the results to drive a loop.

find . -name Given -print | sort |
sed -e 's!\./!!' -e 's!/Given!!' |
while read subd
do
    # change to where began (since previous iteration does cd)
    cd $HERE

    # echo subdirectory and go there.
    echo "$subd:"
    cd $subd

    ## echo removing in $subd
    x=`ls -1 | grep -v Given`
    ## echo $x
    /bin/rm -rf $x shhhhMaKeNoNoise

done  # <|find
