.POSIX:

.PHONY: all
all: um

.PHONY: test

# Remove `.gitignore`d files interactively.
.PHONY: clean
clean:
	git clean -Xi

URL = http://boundvariable.org
task.txt:
	curl -sSL $(URL)/task.shtml \
	| pandoc --from html --to plain --columns=79 \
	| awk ' \
	  /^An Urgent Appeal$$/           { body = 1;      } \
	  /^Carnegie Mellon University$$/ { print; exit;   } \
	  /^[[:punct:]]+$$/               { getline; next; } \
	  body; \
	' > $@
sha1sums.txt:
	curl -sSL $(URL)/task.shtml \
	| pandoc --from html --to plain --columns=79 \
	| awk ' \
	  $$1 == "[]"    { file = $$2; } \
	  $$1 == "SHA-1" { print $$3, "*" file; } \
	' > $@
codex-key.txt: task.txt
	awk 'key { print; exit; } /decryption key/ { key = 1; }' task.txt > $@
codex.umz sandmark.umz: sha1sums.txt
	curl -sSL $(URL)/$@ > $@
	awk '$$2 == "*$@"' sha1sums.txt | sha1sum -c
sandmark-output.txt um.um:
	curl -sSL $(URL)/$@ > $@
um-spec.txt:
	curl -sSL $(URL)/$@ | awk 'sub(/ *$$/, "") || 1;' > $@

um: um.cpp
	$(CXX) $(CXXFLAGS) $(CPPFLAGS) $(LDFLAGS) um.cpp $(LDLIBS) -o $@

test: um sandmark.umz sandmark-output.txt
	{ time ./um sandmark.umz; } \
	| tee /dev/tty \
	| diff -q sandmark-output.txt -