#!/bin/sh -
#
# Copyright (c) 1987 Regents of the University of California.
# All rights reserved.
#
# Redistribution and use in source and binary forms are permitted
# provided that this notice is preserved and that due credit is given
# to the University of California at Berkeley. The name of the University
# may not be used to endorse or promote products derived from this
# software without specific prior written permission. This software
# is provided ``as is'' without express or implied warranty.
#
#	@(#)mkdep.sh	5.9 (Berkeley) 1/1/88
# Mods:
#   Added -f, -P and -S flags.
#
#

PATH=/bin:/usr/bin:/usr/ucb:$PATH
export PATH

MAKE=Makefile			# By default edit "Makefile"
Mflag=-M
Prefix=

while :
	do case "$1" in
		# -f allows you to select a makefile name
		-f) MAKE=$2; shift 2 ;;

		# the -p flag produces "program: program.c" style dependencies
		# so .o's don't get produced
		-p)
			SED='s;\.o;;'
			shift ;;
		# the -P prefixstring flag prepends "prefixstring"
		# to every non-absolute pathname appearing as a dependency
		-P)
			Prefix="$2"; shift 2 ;;
		-S)
			Srctoo=1; shift ;;
		-MM)
			Mflag=-MM; shift ;;
		*)
			break ;;
	esac
done

if [ $# = 0 ] ; then
	echo \
'Usage: mkdep [-p] [-f make-depend-file] [-P prefix] [-S] file ...
Writes include-file dependencies to make-depend-file (default "Makefile").
 -p	    Produce  program: program.c  dependencies, avoiding .o'"'"'s.
 -f file    Edit (or create) dependencies into that file rather than Makefile
 -P prefix  Prepend "prefix" to all dependencies with non-absolute paths.
 -S	    For each source file add dependency basename-of-file.o: file.c' >&2
	exit 1
fi

if [ ! -w "$MAKE" ]; then
    if [ ! -f "$MAKE" ]; then
	touch "$MAKE"
    else
	mv -f "$MAKE" "$MAKE".bak
	cp "$MAKE".bak "$MAKE"
	rm -f "$MAKE".bak
    fi
    chmod a+w "$MAKE"
    if [ ! -w "$MAKE" ]; then
	echo "mkdep: no writeable file \"$MAKE\""
	exit 1
    fi
fi

TMP=/tmp/mkdep$$

trap 'rm -f $TMP ; exit 1' 1 2 3 13 15

cp $MAKE ${MAKE}.bak

sed -e '/DO NOT DELETE THIS LINE/,$d' < $MAKE > $TMP

cat << _EOF_ >> $TMP
# DO NOT DELETE THIS LINE -- mkdep uses it.
# DO NOT PUT ANYTHING AFTER THIS LINE, IT WILL GO AWAY.

_EOF_

#
# The sed script does the following:
#   squeezes multiple spaces & tabs to a single space
#   Ensures dependencies have the form <target>:<SP><item><SP><item>...
#   Applies -P prefix to relative pathnames of dependencies
#   Discards system include files: /usr/include/* and /NextDeveloper/Headers/*.
${CC} ${Mflag:-"-M"} $@ | \
  sed -e "
    s'[ 	][ 	]*' 'g
    s' *\\\\ *$''
    s' *: *': '
    s' \./' 'g
    s' \([^/]\)' ${Prefix}\1'g
    s' /usr/include/[^ ]*''g
    s' /usr/lib/gcc-lib/[^ ]*''g
    s' /usr/local/lib/gcc-lib/[^ ]*''g
    s' /NextDeveloper/Headers/[^ ]*''g
    $SED" | \
  awk '{
	if($1 ~ /:/) {
	    target = $1;
	    i = 2;
	    if (target != prev) {
		if (rec != "") print rec;
		rec = target;
		prev = target;
	    }
	} else {
	    i = 1;
	}
	while(i <= NF) {
	    nrec = rec " " $i;
	    if(length(nrec) > 78) {
		print rec " \\";
		nrec = "   " $i;
	    }
	    rec = nrec;
	    i++;
	}
}
END {
	print rec;
} ' >> $TMP

#
# Work around NeXT 3.0 cc anomaly: cc -M does not include the source file
# among the dependencies.  OOGL makefile layout needs this.
#
if [ "$Srctoo" != "" ]; then
    echo $@ | tr -s ' \11'  '\12\12' | \
    awk '
	/^[^-].*\.[cm]/ {
		src = $1;
		ns = split(src, basename, "/");
		obj = basename[ns];
		if(src !~ /^\//) src = "'"$Prefix"'" src;
		print substr(obj, 1, length(obj)-1) "o: " src;
	}' >>$TMP
fi
		
cat << _EOF_ >> $TMP

# IF YOU PUT ANYTHING HERE IT WILL GO AWAY
_EOF_

# copy to preserve permissions
cp $TMP $MAKE || mv $TMP $MAKE
rm -f ${MAKE}.bak $TMP
exit 0
