#!/bin/bash
#
#	Create make.dep files for each directory
#

makeDep() {
	local file top dir

	file="${1}"
	top="$2"

	dir=`dirname $file`
	dir="${dir#./}"
	if [ "$dir" != "." ] ; then
		dir=`echo $dir | sed 's/[^/][^/]*/../g'`
		top="$top/$dir"
	fi
	top=${top#./}

	cat >"${file}" <<!EOF
#
#   make.dep -- Initial Makefile dependencies. Generated by makedep.
#               This will be replaced by genDepend when "make depend" is run.
#

all: compile

BLD_TOP := ${top:-.}

#
#	Read the build configuration settings and makevariable definitions.
#
include \$(BLD_TOP)/buildConfig.make

#
#	Read the Makefile rules
#
include \$(BLD_TOP)/build/make/make.rules

ifeq (\$(BUILDING_CROSS),1)
	include \$(BLD_TOP)/build/make/make.os.\$(BLD_HOST_OS)
else
	include \$(BLD_TOP)/build/make/make.os.\$(BLD_BUILD_OS)
endif
!EOF
}


#
#	Find the top level directory
#
TOP=.
level=0
while [ $level -lt 30 ] 
do
	if [ -d $TOP/build -a -d $TOP/tools -a -d $TOP/bin ] ; then
		break
	fi
	TOP=$TOP/..
	level=$((level + 1))
done
TOP=${TOP#./}

if [ $level -ge 30 ] ; then
	echo "Can't find top level directory with build, tools and bin directories"
	exit 255
fi


#
#	Create make.dep files from here and below
#
find . -name Makefile -print | egrep -v "/pkgtmp/" | sed 's/Makefile/make.dep/' | while read file
do
	makeDep $file $TOP
done
