#! /bin/sh
#
# Generate SOURCES and OBJECTS defines in a file.
#
# Usage:
#       define Makefile
#

# set -x
# set -v

if test $# -ne 1
then
        echo "Usage: define filename"
        exit 1
fi

if test ! -f $1
then
        echo "define: $1 does not exist"
        exit 1
fi

temp=/tmp/define.$$
temp2=/tmp/define2.$$
trap 'rm -f $temp $temp2; exit 1' 1 2 3 15

# Do the sources. We want at least two source filenames to appear,
# for grep's sake.
ls *.c >$temp
num=`wc -l <$temp`
if test $num -eq 0
then
        echo "define: no source files"
        exit 1
elif test $num -eq 1
then
        ls *.c >>$temp
fi
awk ' BEGIN { rec = "SRCS = "} \
      { if (length(rec $$0) > 75) \
                { print rec " \\"; rec = "           " $$0; } \
        else rec = rec " " $$0 } \
      END { print rec } ' < $temp > $temp2

# Now for the objects.
ls *.c >$temp
ed - $temp <<'END'
1,$s/\.c/.o/
w
END
awk ' BEGIN { rec = "OBJS = "} \
      { if (length(rec $$0) > 75) \
                { print rec " \\"; rec = "           " $$0; } \
        else rec = rec " " $$0 } \
      END { print rec } ' < $temp >>$temp2

# Fix up the Makefile.
cp Makefile Makefile.BAK
ed - Makefile <<END
/^SRCS/,/^# DO NOT DELETE THIS LINE MAKE DEFINE NEEDS IT/-1d
.-1r $temp2
w
END

rm -f $temp $temp2
exit 0
