#! /bin/sh
#
# Network UPS Tools: drivers/gendb
#
# This is a hack to create a bunch of identical rules and dependencies
# for building the drivers without relying on either the % patterns of
# GNU Make or the for...endfor of BSD Make.
# They're both neat, but they're also not portable.  Ugh.
#

MFDB=Makefile.drvbuild

which gcc >/dev/null 2>&1
if [ $? != 0 ]; then
    echo "$0: gcc not found. Bailing out (and keeping $MFDB intact)."
    exit 1
fi

rm -f $MFDB

cat > $MFDB << EOF
# Network UPS Tools: drivers/$MFDB
#
# WARNING: This file is generated by the gendb script.  Any changes
#          to this file will be erased the next time it runs, i.e.,
#          the next time you do "make depend". 
#
EOF

for i in $*
do

	EXTRA=""
	EXTRAOBJ=""

	# tack on any per-driver portability/deps extras
	case $i in
		isbmex)
			EXTRA="-lm"
			;;
		tripplite)
			EXTRA="-lm"
			;;
		upscode2)
			EXTRA="-lm"
			;;
		bcmxcp)
			EXTRAOBJ="bcmxcp_ser.o"
			;;
		rhino)
			EXTRA="-lm"
			;;
	esac	

	echo "$i: $i.o main.o serial.o dstate.o ../common/state.o ../common/upsconf.o ../common/parseconf.o $EXTRAOBJ \$(LIBDEP)" >> $MFDB
	echo "	\$(CC) \$(CFLAGS) \$(LDFLAGS) -o $i $i.o main.o serial.o dstate.o ../common/state.o ../common/upsconf.o ../common/parseconf.o $EXTRAOBJ \$(LIBOBJ) $EXTRA" >> $MFDB
done

for i in *.c
do
	gcc -I../include -MG -MM "$i" >> $MFDB
done

exit 0

