#!/bin/bash
#
#   Create .makedep 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
#
#   .makedep -- 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)/build/buildConfig.make

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

ifeq (\$(BUILDING_CROSS),1)
    include \$(BLD_TOP)/build/make/make.\$(BLD_HOST_OS)
else
    include \$(BLD_TOP)/build/make/make.\$(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/build/bin -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 and bin directories"
    exit 255
fi


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