#!/bin/sh
#	R<foo> [-cns] [dir]...
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#
# This is a recursive cleanup script.  Give it one or  more  directory #
# names,  and  it will do a "make <foo>" in all of them that contain a #
# makefile with a  "<foo>:"  entry.   It  does  the  job  recursively, #
# cleaning  out  subdirectories.   If  no  directories  are named, the #
# current directory (and its subdirectories) will  be  cleaned.   Note #
# that,  since  we  use find, we don't follow symbolic links.  This is #
# useful to prevent infinite recursion and  cleaning  up  a  directory #
# more  than  once, but it has its problems.  Note that we can make as #
# many different entries as are known in the first case statement. The #
# Rmake entry is a special kludge.                                     #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#
target=
case `basename $0` in
	Rmake)    target=all;;
	Rneat)    target=neat;;
	Rclean)   target=clean;;
	Rclobber) target=clobber;;
	Rnuke)    target=nuke;;
	Rshar)    target=shar;;
	Rstrip)   target=strip;;
esac
case "$1" in 
	-n*)	target=neat;	shift;;
	-c*)	target=clean;	shift;;
	-s*)	target=strip;	shift;;
esac
if [ $# -lt 1 ];then set .;fi
c=`pwd`
for d
do	find $d -type d -print | {
		while read dir
		do	# echo Dir : $dir
			if	[ -f $dir/Makefile -o -f $dir/makefile ];then
				if	grep "^$target[ 	]*.*:" $dir/[Mm]akefile >/dev/null ;then
					echo 'Make "'$target'" in '$dir' ...'
					(cd $dir;make $target)
				fi
			fi
		done
	}
done
