#!/bin/sh
# lint-rules.sh - generate make rules for lint dir
# usage: lint-rules.sh > rules.mk

if [ $# != 0 ]; then
    echo "$0: usage: $0" 1>&2
    exit 1
fi

SOURCESETS='tool main example'


# generate the lists of sources

for S in $SOURCESETS; do
    if [ ! -f $S.srcs ]; then
	echo "$0: $S.srcs missing - rerun make rules?"
	exit 1
    fi

    echo $S | tr a-z A-Z | awk '{ printf "%sSRCS=\\\n", $1 }'
    sed < $S.srcs 's/^/    /;s/$/ \\/;$s/ \\$//'
    echo
done

for S in $SOURCESETS; do 
    awk < $S.srcs '
    {
	file=$1;
	base=file;
	sub(".*/", "", base);
	base = set "-" base;
	ok = sprintf("$(TOP)/lint/ok/%s.ok", base)

	printf "%s.lint: $(TOP)/%s $(CHECK)\n", base, file;
	printf "\t@echo \"        [CHECK]   %s\"\n", base;
	printf "\t@$(CHECK) --%s \"$(TOP)\" %s > %s.lint\n", set, file, base;
	printf "\n";

	printf "%s.diff: %s.lint %s\n", base, base, ok;
	printf "\t@diff %s %s.lint | tee %s.diff\n", ok, base, base;
	printf "\n";

	printf "all: %s.diff\n", base;
	printf "\n";
    }
    ' "set=$S"
done
