#! /bin/sh
# $Id: makedepend 474 2003-05-31 21:03:36Z eagle $
#
# makedepend -- Generate dependencies for makefiles.
#
# This shell script automates the process of updating the dependencies in
# Makefile.in.  It uses gcc -MM to do this, since only the maintainers should
# normally have to do this and using a compiler to parse include directives is
# more reliable than more ad hoc methods.  It takes compiler flags as its
# first argument and then a list of all source files to process.
#
# The Makefile is updated in place, and everything after "DO NOT DELETE THIS
# LINE" is removed and replaced by the dependencies.

# Find the value of top_srcdir and top_builddir from the makefile.
top_srcdir=`grep '^top_srcdir' Makefile | sed 's/.*= //'`
if [ -z "$top_srcdir" ] ; then
    top_srcdir=NOTFOUND
else
    top_srcdir=`echo "$top_srcdir" | sed 's/\./\\\\./g'`
fi
top_builddir=`grep '^top_builddir' Makefile | sed 's/.*= //'`
if [ -z "$top_builddir" ] ; then
    top_builddir=NOTFOUND
else
    top_builddir=`echo "$top_builddir" | sed -e 's/\./\\\\./g' -e 's%/$%%'`
fi

flags="$1"
shift
sed '1,/DO NOT DELETE THIS LINE/!d' < Makefile.in > .makefile.tmp
for source in "$@" ; do
    case $source in
    */*)
        base=`echo "$source" | sed 's/\..*//'`
        gcc -MM $flags "$source" | sed "s%^[^.: ][^.: ]*%$base%" \
            >> .makefile.tmp
        ;;
    *)
        gcc -MM $flags "$source" >> .makefile.tmp
        ;;
    esac
    if [ $? != 0 ] ; then
        rm .makefile.tmp
        exit
    fi
done
sed -e "s%$top_builddir/config\\.h%\$(top_builddir)/config\\.h%g" \
    -e "s%$top_srcdir/%\$(top_srcdir)/%g" .makefile.tmp > Makefile.in
rm .makefile.tmp
