#!/bin/sh
#require cc
#require cflags
#phase library
#phase init
#after init_cflags
case $PHASE in
	library)
		disp "Loading dependency helper function"
		rm -f dependencies.log
		customdeplist() {
			for DEP in $1; do
				JUNK=`$ECHO "$BUILTDEPS" | grep "$DEP"`
				if test "$?" != "0"; then
					# haven't built this one yet
					BUILTDEPS="$BUILTDEPS $DEP"
					CNAME=`echo "$DEP" | sed 's/\\.o\$/.c/'`
					customdeps "$CNAME" "$DEP"
				fi
			done
		}
		customdeps() {
			if test "$CC_DEPENDENCY" = "n"; then
				return
			fi
			dispn "Generating customized build rule for $1..."
			$ECHO "Generating dependencies with \"$CC $FM_CFLAGS -M $1\" (errors print below):" >> dependencies.log
			$CC $FM_CFLAGS -M $1 2>>dependencies.log
			if test "$?" != "0"; then
				disp "unable to generate dependencies; this file probably won't build.  Aborting (check dependencies.log for details)"
				exit 1
			fi
			$ECHO >> dependencies.log
			$ECHO "	$CC $FM_CFLAGS -DCONFDIR=\"\\\"\$(CONFDIR)\\\"\" -DBINDIR=\"\\\"\$(BINDIR)\\\"\" -DSBINDIR=\"\\\"\$(SBINDIR)\\\"\" -DLIBDIR=\"\\\"\$(LIBDIR)\\\"\" -DMANDIR=\"\\\"\$(MANDIR)\\\"\" -c -o $2 $1"
			$ECHO
			disp "done"
		}
		;;
	init)
		dispn "Checking for compiler dependency support..."
		$ECHO "#include <stdio.h>
int main() {
	return 0;
}" > deptest.c
		$ECHO "Testing \"$CC $FM_CFLAGS -M deptest.c\":" >> dependencies.log
		DEPOUTPUT=`$CC $FM_CFLAGS -M deptest.c`
		RESULT="$?"
		$ECHO "Result is $RESULT" >> dependencies.log
		$ECHO "Output is:
$DEPOUTPUT

" >> dependencies.log
		if test "$RESULT" = "0"; then
			JUNK=`$ECHO "$DEPOUTPUT" | grep stdio`
			if test "$?" = "0"; then
				disp "found"
				CC_DEPENDENCY="y"
			else
				disp "found but broken, skipping"
				CC_DEPENDENCY="n"
			fi
		else
			disp "not found, skipping"
			CC_DEPENDENCY="n"
		fi
		rm -f deptest.c
		;;
esac
