ARCH ?= bochs

BUILD_DIR      := build-${ARCH}
FAIL_BIN       ?= /home/fail/bin
FAIL_SERVER    ?= ${FAIL_BIN}/generic-experiment-server
FAIL_TRACE     ?= ${FAIL_BIN}/generic-tracing-client
FAIL_INJECT    ?= ${FAIL_BIN}/generic-experiment-client
FAIL_DUMP      ?= ${FAIL_BIN}/dump-trace
FAIL_IMPORT    ?= ${FAIL_BIN}/import-trace --enable-sanitychecks
FAIL_PRUNE     ?= ${FAIL_BIN}/prune-trace
BOCHS_RUNNER   ?= ${FAIL_BIN}/bochs-experiment-runner.py
RESULT_BROWSER ?= ${FAIL_BIN}/resultbrowser.py

EXPERIMENTS  := $(patsubst %.c,%,$(shell echo *.c))

include arch/${ARCH}.mk

$(foreach element,$(EXPERIMENTS),$(eval $(call arch-make-targets,$(element))))

all: help

help:
	@echo "Small Playground for FAIL* Injections"
	@echo "-------------------------------------"
	@echo "Current Configuartion"
	@echo "	ARCH=${ARCH}"


clean:
	rm -f *.wasm
	rm -f *.aot
	rm -f *_wasm.c
	rm -f *.d
	rm -f *.o
	rm -f embed/*_host.c
	rm -f *_host.elf
	rm -rf ${BUILD_DIR}

clean-%:
	rm -rf ${BUILD_DIR}/$(patsubst clean-%,%,$@)
	contrib/clean-db '${ARCH}/$(patsubst clean-%,%,$@)'


build-%:
	@echo "****************************************************************\n\
* The next step is to trace a golden run. The golden run executes the\n\
* system-under-test (SUT) within the emulator. A trace file is \n\
* produced and saved as: ${BUILD_DIR}/main/trace.pb\n\
*\n\
*    $ make trace-$(patsubst build-%,%,$@)\n\
****************************************************************"


trace-%:
	@echo "****************************************************************\n\
* The trace is now generated. It can be viewed with\n\
*\n\
*   $ make dump-$(patsubst trace-%,%,$@)\n\
*\n\
* Next, we have to import the trace into the database\n\
*\n\
*    $ make import-$(patsubst trace-%,%,$@)\n\
****************************************************************"

dump-%: ${BUILD_DIR}/%/trace.pb
	${FAIL_DUMP} $(shell dirname $<)/trace.pb

${HOME}/.my.cnf:
	@echo "[client]" > $@
	@echo "user=fail" >> $@
	@echo "database=fail" >> $@
	@echo "password=fail" >> $@
	@echo "host=db" >> $@
	@echo "port=3306" >> $@

import-%: import-arch-%
	@echo "****************************************************************\n\
* The golden run sits now within the MySQL database. If you are interested,\n\
* use the 'mysql' command to inspect the curent state of the DB. The tables\n\
* trace, fsppilot, and fspgroup are of special interest.\n\
*\n\
* Next, we have to run the campaign sever and the injection client\n\
*\n\
*   $ make server-$(patsubst import-%,%,$@) &\n\
*   $ make client-$(patsubst import-%,%,$@) \n\n\
* Afterwards, the results can be viewd with\n\
*   $ make result-$(subst import-,,$@)\n\
****************************************************************"

server-%:
	${FAIL_SERVER} -v ${ARCH}/$(subst server-,,$@) -b %

result-%:
	@echo "select variant, benchmark, resulttype, sum(t.time2 - t.time1 + 1) as faults\
			FROM variant v \
			JOIN trace t ON v.id = t.variant_id \
			JOIN fspgroup g ON g.variant_id = t.variant_id AND g.instr2 = t.instr2 AND g.data_address = t.data_address\
			JOIN result_GenericExperimentMessage r ON r.pilot_id = g.pilot_id  \
			JOIN fsppilot p ON r.pilot_id = p.id \
			WHERE v.variant = \"${ARCH}/$(patsubst result-%,%,$@)\"\
			GROUP BY v.id, resulttype \
			ORDER BY variant, benchmark, resulttype;"  |mysql -t

resultbrowser:
	${RESULT_BROWSER} --host=0.0.0.0 --port=5000


# Do never remove implicitly generated stuff
.SECONDARY:
