#!/bin/sh
# testprog-defs.sh - generate make rules for testprog.mk
# usage: testprog-defs.sh "tests" "diffonly-tests" > rules.mk

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

TESTS="$1"
DIFFONLY="$2"

(
    for T in $TESTS; do
	echo 1 "$T"
    done
    for T in $DIFFONLY; do
	echo 0 "$T";
    done
) | awk '
    {
	dorun=$1;
	file=$2;
	if (file ~ "/") {
	    srcdir = file;
	    sub("^.*/", "", file);
	    sub("/[^/]*$", "", srcdir);
	    srcdir = "$(TOP)/" srcdir;
	}
	else {
	    srcdir = "$(SRCDIR)"
	}
	base=file;
	sub("\\.[a-z][0-9]*$", "", base);
	out = base ".out";
	err = base ".err";
	diff = base ".diff";

	printf "run-tests: %s\n", diff;
	printf "%s: $(SRCDIR)/good/%s %s\n", diff, out, out;
	printf "%s: $(SRCDIR)/good/%s %s\n", diff, err, err;
	printf "\t@echo \"        [DIFF]    %s\"\n", file;
	printf "\t@diff -uN $(SRCDIR)/good/%s %s > $@ || true\n", out, out;
	printf "\t@diff -uN $(SRCDIR)/good/%s %s >> $@ || true\n", err, err;
	printf "\n";

	if (dorun) {
	    printf "%s %s: $(SRCDIR)/%s $(RUNPROG)\n", out, err, file;
	    printf "\t@echo \"        [TEST]    %s\"\n", file;
	    printf "\t@($(CROSSRUN) $(RUNPROG) %s/%s;", srcdir, file;
	    printf " echo Exit $$? 1>&2) \\\n"
	    printf "\t > %s 2> %s; \\\n", out, err;
	    printf "\n";
	}
    }
'

