#!/bin/sh

#
# make condensed lint library
#

ppflags=
ccflags="-V"
cpp="cc68"
lint="lint68 -n"
machine=m68k
cparser="/usr/local/lib/cparse"
lib=V

while :
do
	case $1 in
	-Dm68k)		cpp="cc68";
			machine=m68k;
			lint="lint68 -xV -n";
			shift;;
	-Dvax)		cpp="ccVAX";
			lint="lintvax -xV -n";
			machine=vax;
			shift;;
	-C?*)		lib=`expr $1 : '-C\(.*\)'`;
			shift;;
	-[IDU]*)	ppflags="$ppflags $1";
			shift;;
	-v*)		ccflags="$ccflags $1";
			shift;;
	*)		break;;
	esac
done

dirname=`pwd | sed "s%\(.*/[^/]*\)/$machine%\1%"`

# subshell re-directed to llib-lV
(
echo "/* LINTLIBRARY */"

# condense the list of include files down to one master list
grep "^# *include" $* /dev/null \
 | sed -e 's/.*:# *include[ 	]*[<"]\([^[>"]*\)[>"].*/\1/' \
 | awk -f ../../unique.awk \
 | sed -e 's/^/#include "/' -e 's/$/"/'

echo "#include \"../../$machine/Vlint.h\""
echo "#define _LINE_ID(line, file)# line file"

for i
do
	echo $i: >&2
	$cpp -C -E $ppflags $ccflags $i | $cparser -L
done \
 | sed	-e 's%^/\* \# \([0-9]*\) "\([^"]*\)".*%_LINE_ID(\1,"\2")%' \
	-e "s%,\"\([^\.]\)%,\"$dirname/$machine/\1%" \
	-e "s%,\"\.\./%,\"$dirname/%"
) \
 > llib-l$lib 2> llib-errors
