#!/bin/sh

# makedepend for NeXT
#
# The working is intended to mimic the X makedepend program, but uses
# the cc -M option.
#
# This script is a modified version of g++dep, which came from the
# Berkeley script mkdep.
# Here is the original BSD header:
#	@(#)mkdep.sh	1.7	(Berkeley)	10/13/87
#

MAKE=Makefile			# default makefile name is "Makefile"
SED=""
string="# DO NOT DELETE THIS LINE -- make depend depends on it."
args=""
append=0
files=0
while [ -n "$1" ]; do
	case $1 in
	-usage)
		echo makedepend usage:
		echo "    -f<file>   send output to file"
		echo "    -a         append output"
		echo "    -p<string> make <string> the prefix for module."
		echo "    -o<string> make <string> the extension for object"
		echo "         file. Default is .o"
		echo "    -s<string> use <string> as separator in Makefile"
		exit 0 ;;
	-f*)
		MAKE=`echo $1 | /bin/sed -e "s/-f//"`
		shift ;;
	-p*)
		prefix=`echo $1 | /bin/sed -e "s/-p//"`
		SED=${SED}" -e s+^+${prefix}+"
		shift ;;
	-o*)
		suffix=`echo $1 | /bin/sed -e "s/-o//"`
		SED=${SED}" -e s+\.o+${suffix}+"
		shift ;;
	-a)
		append=1
		shift ;;
	-s*)
		string=`echo $1 | /bin/sed -e "s/-s//"`
		shift ;;
	--)
		shift ;;
	-*)  
		args=${args}" $1"
		shift ;;
	*)
		args=${args}" $1"
		files=1
		shift ;;
		
	esac
done

if [ ! -w $MAKE ]; then
	echo "g++dep: no writeable file \"$MAKE\""
	exit 1
fi

TMP=/tmp/mdep$$

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

cp $MAKE ${MAKE}.bak

if [ $append -eq 0 ]; then
	sed -e "/${string}/,\$d" < $MAKE > $TMP

	echo ${string} >> $TMP
else
	cp $MAKE $TMP
fi
echo " " >> $TMP

if [ $files -eq 1 ]; then
	cc -Wno-import -MM $args | /bin/sed -e "s; \./; ;g" -e "/^ /b" $SED | \
		awk ' { \
			if ($1 != prev) { \
				if (rec != "") \
					print rec; rec = $0; prev = $1; \
			} \
			else { \
				if (length(rec $2) > 75) { \
					print rec; rec = $0; \
				} else \
					rec = rec " " $2 \
			} \
		} \
		END { \
			print rec \
		} ' >> $TMP
fi

# copy to preserve permissions
cp $TMP $MAKE
/bin/rm -f $TMP
exit 0

