#!/bin/zsh
: ${delim:=---}
: ${AWK:=awk}
: ${SED:=sed}

dst=$1
bin=$0:h:a/bin
[[ -d $dst ]] && rm -r $dst
mkdir -p $dst || exit $?

while IFS= read line; do
	[[ $line == $delim ]] && break
	printf '%s\n' $line >>$dst/test.aat || exit $?
done

cat >$dst/test.data || exit $?
cat >$dst/Makefile <<EOF  || exit $?
test.out: test.data test.qawk
	awk -f test.qawk DEBUG=\$(DEBUG) test.data >test.out

test.qawk: test.awk
	$bin:h/query.pl test.awk >test.qawk

# test.qawk: test.awk
# 	awk -f $bin/query.awk <test.awk >test.qawk

test.awk: test.mac
	sed -f $bin/aat_macros.sed <test.mac >test.awk

test.mac: test.aat
	AAT_DEP=test.dep awk -f $bin/aat.awk test.aat >test.mac

clean:
	rm -f test.out test.qawk test.awk test.mac
EOF

gmake -sC $dst && cat $dst/test.out