######################################################################
# Test using each artemis raw data capture (NOT PCAPS!):
# Input files (captures/artemis-*[cs].xz),
IFILES := $(wildcard captures/artemis-*[cs].xz)

# Output files (captures/artemis-*[cs].pass):
OFILES := $(sort $(IFILES:%.xz=%.pass))

# Other files we will generate:
EXTRAS := TODO STATS ALL.js html versions.txt 
EXTRAS += files.html led-artemis.pl packet-types.js snt.txt snt.html

# Set to latest Artemis version:
VERSION := 2.8.0
HTML    := ~/shared/templates/html5.html

######################################################################
# default Makefile target: Make the output files, a TODO list, STATS,
# some html, then dump the TODO list OR the STATS. If any files
# failed, provide info about those failures too.
tests : $(OFILES) $(EXTRAS)
	######################################################################
	@ [ -s TODO ] && head -20 TODO \
	|| { egrep '"' STATS ; egrep -v '"' STATS; }
	@- ls captures/*.fail 2>/dev/null && echo "FAILURES to be fixed  :-(" \
	|| echo "### NO tests failed! NICE!"

######################################################################
# Lies, damned lise, and exceptions - some files aren't EXPECTED to
# pass there-and-back-again for various reasons, unless we fiddle a
# bit:
# Somehow didn't capture Version packet (from mid-game tcpdump?)
captures/artemis-2015-11-07.0s.pass                       : PROTOVERSION=2.2
# Starry's ObjEOL were short, and some UTF16s corrupt (since fixed):
captures/artemis-2018-01-20.1111.STARRY_MINIMAL.01s.pass  : TESTARGS=--norun
captures/artemis-2018-01-20.1126.STARRY_MINIMAL.01s.pass  : TESTARGS=--norun
captures/artemis-2018-01-20.1131.STARRY_MINIMAL.01s.pass  : TESTARGS=--norun
captures/artemis-2018-01-20.1139.STARRY_MINIMAL.01s.pass  : TESTARGS=--norun
captures/artemis-2018-01-20.1213.STARRY_MINIMAL.01s.pass  : TESTARGS=--norun
captures/artemis-2018-03-15.2029.STARRY_MINIMAL.01s.pass  : TESTARGS=--objswitch
captures/artemis-2018-04-23-1253.StarryTest.01s.pass      : TESTARGS=--objswitch
captures/artemis-2018-05-11.1954.StarrySpecial.01s.pass   : TESTARGS=--objswitch
captures/artemis-2018-05-30.2146.local.170.%c.pass        : PROTOVERSION=1.7
######################################################################
# generic rule to create FOO.pass from captures/FOO.xz

%.pass : %.xz # parser.pl tester.sh
	######################################################################
	@- $(if $(PROTOVERSION),PROTOVERSION=$(PROTOVERSION)) \
	  ./tester.sh $(TESTARGS) $< 2>&1 | tee $*.tmp
	@- sed -e '1,/DIFF/ d' $*.tmp | grep -q . \
	  && mv -v $*.tmp $*.fail && rm -vf $*.pass 2>/dev/null || true
	@- mv -v $*.tmp $*.pass 2>/dev/null && rm -vf $*.fail 2>/dev/null

######################################################################
# Hot to make STATS and ALL.js for all games

STATS : totals.pl $(OFILES)
	# rebuilding $@ ...
	@ cat captures/artemis-*.pass | grep '^ *[0-9][0-9]*' | ./totals.pl > $@.tmp
	@ mv $@.tmp $@

ALL.js : $(OFILES)
	# rebuilding $@ ...
	@ echo 'ARTEMIS_DAT=`' > $@.tmp
	@ sed -e '/^# STAT:/d;  s|\\x{\(.*\)}|\\u\1|g' captures/artemis-*.pass >> $@.tmp
	@ echo '`;' >> $@.tmp
	@ mv $@.tmp $@

versions.txt : $(OFILES)
	# rebuilding $@ ...
	@ for X in captures/*.pass captures/*.fail; do echo "$$X : $$(egrep ^Version $$X)"; done \
	  | sed -e 's|\.pass |\.xz |' > $@.tmp
	@ mv $@.tmp $@


######################################################################
# Make a TODO list - any parsers that are out of order,
# and any TODO/BAD packet decodes

TODO : STATS parser.pl
	# rebuilding $@ ...
	@ grep '^\$$parsers' parser.pl | egrep -v 'parsers\{0xdeadbeef\}|\{_name\}' > /tmp/artemis.parsers
	@ sort /tmp/artemis.parsers | diff -u /tmp/artemis.parsers - > $@.tmp || true
	@ egrep 'TODO|BAD' STATS >> $@.tmp || true
	@ mv $@.tmp $@

######################################################################
# Merge some groups of .pass files into an html summary table -
# useful for (EG) connecting a load of different consoles, logging all
# traffic, and comparing which stations get which packets
# (it turns out ALL stations get MOST packets, the diffs are << 10%)

PASS_SET = $(filter artemis-$(*:%.html=%).%, $(OFILES))
artemis-%.html : ./counts2html.pl $(PASS_SET)
	echo $(PASS_SET) | xargs -n1 | sort --field-separator=. --key=4 | xargs ./counts2html.pl > $@.tmp
	@ mv $@.tmp $@

html: artemis-2017-12-01.1402.html artemis-2017-11-25.2109.html

led-artemis.pl : /home/leddf/LEDDF/led-artemis.pl
	@ cp /home/leddf/LEDDF/led-artemis.pl led-artemis.pl || true
/home/leddf/LEDDF/led-artemis.pl :
	@ :

snt.txt : snt-dec.pl ArtemisVer/$(VERSION)/dat/*.snt
	./snt-dec.pl ArtemisVer/$(VERSION)/dat/*.snt > $@.tmp
	mv $@.tmp $@

snt.html : snt-dec.pl       $(HTML) ArtemisVer/$(VERSION)/dat/*.snt
	./snt-dec.pl --html $(HTML) ArtemisVer/$(VERSION)/dat/*.snt > $@.tmp
	mv $@.tmp $@

files.html : files.pl ArtemisVer/$(VERSION) $(HTML)
	./$^ > $@.tmp
	mv $@.tmp $@

packet-types.js : packet-types-js.pl parser.pl
	./packet-types-js.pl > $@.tmp
	mv $@.tmp $@

######################################################################
# Other non-default targets

# Syncronise / backup captures up to my server
sync:
	~/projects/backup/std_rsync.sh captures/ noseynick.net:Downloads/artemis/captures/

# TODO tool looks for "++++ TODO: de-grombulise the fnaglehorns" lines:
todo:
	TODO | egrep -v 'cloud-init-output|/bot.op:'

# DON'T remove, but SHOW HOW TO remove any captures that weren't
# actual "games" - typically connecting to a server that's shut down
# soon after without playing a game.
# Doesn't remove CLIENT captures - usually captured for a reason, even
# if near-empty.
clean:
	# When the server sent no objectBitStream, there was no real game:
	egrep --files-without-match 'objectBitStream|GameOver' captures/*s.pass \
	| sed -e 's|\(.*\)\.pass|& \1.xz|' \
	| xargs rm -vf
	rm -fv $(EXTRAS)
