SHELL := /usr/bin/env bash

GOOD_FEATURE_FILES = $(shell find ../testdata/good -name "*.feature")
BAD_FEATURE_FILES  = $(shell find ../testdata/bad -name "*.feature")

TOKENS   = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.tokens,$(GOOD_FEATURE_FILES))
ASTS     = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.ast.ndjson,$(GOOD_FEATURE_FILES))
PICKLES  = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.pickles.ndjson,$(GOOD_FEATURE_FILES))
SOURCES  = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.source.ndjson,$(GOOD_FEATURE_FILES))
ERRORS   = $(patsubst ../testdata/%.feature,acceptance/testdata/%.feature.errors.ndjson,$(BAD_FEATURE_FILES))

SRC_FILES= $(shell find src -name "*.[ch]*")

ifeq ($(CC),i686-w64-mingw32-gcc)
	GHERKIN=bin/gherkin.exe
	RUN_GHERKIN=wine $(GHERKIN)
	GHERKIN_GENERATE_TOKENS=bin/gherkin_generate_tokens.exe
	RUN_GHERKIN_GENERATE_TOKENS=wine $(GHERKIN_GENERATE_TOKENS)
else
	GHERKIN=bin/gherkin
	RUN_GHERKIN=$(GHERKIN)
	GHERKIN_GENERATE_TOKENS=bin/gherkin_generate_tokens
	RUN_GHERKIN_GENERATE_TOKENS=$(GHERKIN_GENERATE_TOKENS)
endif

.DELETE_ON_ERROR:

default: .compared
.PHONY: default

acceptance: .compared ## Build acceptance test dir and compare results with reference

.compared: .built $(TOKENS) $(ASTS) $(PICKLES) $(ERRORS) $(SOURCES) .run
	touch $@

generate: ./include/rule_type.h src/parser.c ## Generate gherkin parser files

clean-generate: ## Remove generated Gherkin parser files ## Generate gherkin parser files
	rm -f ./include/rule_type.h src/parser.c
.PHONY: clean-generate

copy-gherkin-languages: src/dialect.c ## Copy gherkin-languages.json and/or generate derived files
	echo "Nothing to do"

clean-gherkin-languages: ## Remove gherkin-languages.json and any derived files
	rm -f src/dialect.c

.built: generate $(SRC_FILES) src/Makefile
	$(CC) --version
	cd src; $(MAKE)
	touch $@

clean:
	rm -rf .compared .built .run acceptance
	cd src; $(MAKE) $@
.PHONY: clean

cli: ./include/rule_type.h src/parser.c src/dialect.c $(SRC_FILES) src/Makefile
	cd src; $(MAKE) CC=$(CC) $@
.PHONY: libs

libs: ./include/rule_type.h src/parser.c src/dialect.c $(SRC_FILES) src/Makefile
	cd src; $(MAKE) CC=$(CC) $@
.PHONY: libs

libs_so: ./include/rule_type.h src/parser.c src/dialect.c $(SRC_FILES) src/Makefile
	cd src; $(MAKE) CC=$(CC) $@
.PHONY: libs_so

.run: cli $(GHERKIN) $(GOOD_FEATURE_FILES)
	$(RUN_GHERKIN) $(GOOD_FEATURE_FILES) | jq . > /dev/null
	touch $@

define berp-generate-parser =
berp -g ../gherkin.berp -t $< -o $@ --noBOM
endef

./include/rule_type.h: gherkin-c-rule-type.razor gherkin.berp
	$(berp-generate-parser)

src/parser.c: gherkin-c-parser.razor gherkin.berp
	$(berp-generate-parser)

src/dialect.c: ../gherkin-languages.json dialect.c.jq
	cat $< | jq -f dialect.c.jq -r -c > $@

acceptance/testdata/%.feature.tokens: ../testdata/%.feature ../testdata/%.feature.tokens $(GHERKIN_GENERATE_TOKENS)
	mkdir -p $(@D)
	echo $(RUN_GHERKIN_GENERATE_TOKENS)
	$(RUN_GHERKIN_GENERATE_TOKENS) $< > $@
	diff --strip-trailing-cr --unified $<.tokens $@

acceptance/testdata/%.feature.ast.ndjson: ../testdata/%.feature ../testdata/%.feature.ast.ndjson $(GHERKIN)
	mkdir -p $(@D)
	$(RUN_GHERKIN) --no-source --no-pickles $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq "." $<.ast.ndjson) <(jq "." $@)

acceptance/testdata/%.feature.errors.ndjson: ../testdata/%.feature ../testdata/%.feature.errors.ndjson $(GHERKIN)
	mkdir -p $(@D)
	$(RUN_GHERKIN) --no-source --no-pickles $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq "." $<.errors.ndjson) <(jq "." $@)

acceptance/testdata/%.feature.pickles.ndjson: ../testdata/%.feature ../testdata/%.feature.pickles.ndjson $(GHERKIN)
	mkdir -p $(@D)
	$(RUN_GHERKIN) --no-source --no-ast $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq "." $<.pickles.ndjson) <(jq "." $@)

acceptance/testdata/%.feature.source.ndjson: ../testdata/%.feature ../testdata/%.feature.source.ndjson .built
	mkdir -p $(@D)
	$(RUN_GHERKIN) --no-ast --no-pickles $< | jq --sort-keys --compact-output "." > $@
	diff --unified <(jq "." $<.source.ndjson) <(jq "." $@)
