#!/bin/sh

# to use this script, cd to the utils source directory and sh
# gendepend.

gdep=`pwd`/g++dep.sh

cd ..
popdir=`pwd`

gxx_include=${popdir}/g\+\+-include
io_dir=${popdir}/iostream
src_dir=${popdir}/src
gxx_flags="-I. -I${gxx_include} -I${io_dir}"


for i in `find . -name depend -print` ; do
	down=`dirname $i`
	up=`echo $down | sed -e "s:/[-a-zA-Z0-9]*:/..:g" -e "s:^\./::"`

	cd ${popdir}/${down}

	echo "" > depend.tmp
	${gdep} -f depend.tmp ${gxx_flags} *.[cC]*
# --- Generate depend rules ---

# The sed script below attempts to make the depend output portable by
# making the output use the same macros used elsewhere in the Makefile:
# - It replaces double // by a single /.
# - It replaces include files that match part of the GXX_INCLUDE_DIRS
#   by names defined in terms of the macros used to define GXX_INCLUDE_DIRS.
#   However, files in g++-include are ignored.
# - It removes any absolute include file names that remain.
# - then remove lines, which contain only `\'
#

#	  -e 's|$(srcdir)|$$(srcdir)|g'
#	  -e 's|: *\$$(srcdir)/\(.*\.[cC]*\)|: \1|'

	sed < depend.tmp \
	  -e 's|//|/|g' \
	  -e "s|${io_dir}|\$(srcdir)/${up}/\$(IO_DIR)|g" \
	  -e "s| ${gxx_include}/[^ ]*[.]h||g" \
	  -e "s|${src_dir}|\$(srcdir)/${up}/src_dir|g" \
	  -e 's| /[^ ]*[.]h||g' \
	  -e '/^[ 	]*\\$/d' -e 's/^[ 	]*$//' \
	| awk 'BEGIN   { prev = "" } \
	      /^( )*$/ { if (prev ~ /\\$/) \
			 { prev = substr(prev,1,length(prev)-1); next } \
		       } \
                       { print prev; prev = $0 } \
	       END { if (prev !~ /^( )*$/) print prev }' \
	> depend
	rm -f depend.tmp
done

exit 0
