#!/bin/sh
#####
# NAME
#	all-make - execute make command in all subdirectories
# VERSION
#	$Id$
# USAGE
#	all-make [options for make command]
# NOTE
#	This script uses "gmake" command if it is exist.
#

# Check the MAKE environment variable. If it is not defined,
# choose gmake or make

message(){
	echo "all-make: $1" >& 2 ;
}

error(){
	echo "all-make: [ERROR] $1" >& 2 ;
	exit 1 ;
}

if [ "X${MAKE}" = "X" ] ; then
	if which gmake > /dev/null ; then
		MAKE="gmake" ;
	elif which make > /dev/null ; then
		MAKE="make" ;
	else
		error "can not find gmake and make commands" ;
	fi
else
	if which ${MAKE} > /dev/null ; then
		echo "using ${MAKE} ..." > /dev/null ;
	else
		error "incorrent MAKE environment variable" ;
	fi
fi

#echo "make: $MAKE" ;
LIST=`find . -name "[mM]akefile" -print` ;

for MFILE in $LIST ; do
	DIR=`echo $MFILE | sed 's/[mM]akefile$//'` ;
	# echo "dir : $DIR" ;
	if (cd $DIR && $MAKE $*) ; then
		message "executing make $* in $DIR is succeeded" ;
	else
		message "executing make $* in $DIR is FAILED" ;
	fi
done

exit 0 ;

