#!/bin/sh
#	R<foo> [-cns] [dir].. [target]..
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#
# This is a recursive "make" script.  Give it one  or  more  directory #
# and/or  target  names, and it will do a "make target" in all of them #
# that contain a makefile with a "target:" entry.   It  does  the  job #
# recursively, in all subdirectories.                                  #
#                                                                      #
# If  you  list  directories  on  the command line, Rmake will work in #
# those directories and all subdirectories; the default  directory  is #
# ".".   If you list targets, they will be made; the default target is #
# "all".  This script may also be linked  to  any  of  several  common #
# names,  and will make the corresponding target if there is no target #
# on the command line.                                                 #
#                                                                      #
# 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.                             #
#                                                                      #
# Bug:  Since "test -d" is used to  distinguish  directory  args  from #
# target args, you can't use this script to make directories.  This is #
# not a very useful capability, and it can't be fixed without breaking #
# the  very  useful  way that this script uses its command line, so it #
# will probably never be fixed.  Sorry if this is a problem.           #
#                                                                      #
# Author: John Chambers <jc@eddie.mit.edu> 1998                        #
# - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -#

P=`basename $0`
#echo $P

D=.
target=all
case $P in
	Rmake)    target=all;;
	Rneat)    target=neat;;
	Rclean)   target=clean;;
	Rclobber) target=clobber;;
	Rnuke)    target=nuke;;
	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 . $target;fi
c=`pwd`
for x
do	echo $P $x ...
	if [ -d "$x" ];then
		D="$D $x"
		echo "D=$D"
	else
		if [ -n "$D" ];then D=.;fi
		target="$x"
		find $D -type d -name '[A-Za-z0-9_]*' -print |  {
			while read dir
			do	# echo Dir : $dir
				if	[ -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
		}
	fi
done
