GOPATH  ?= $(HOME)/go
GOBIN ?= $(GOPATH)/bin
export MCMAKER := $(GOBIN)/mcmaker
export GOFLAGS ?= -mod=vendor

DST := ../source-crs/extra-manifest
CHK := .chk

GEN_DIRS := $(dir $(wildcard */build.sh))
GEN_TGTS := $(foreach role,master worker,$(patsubst %/,$(DST)/%-$(role).yaml,$(GEN_DIRS)))
CHK_TGTS := $(foreach role,master worker,$(patsubst %/,$(CHK)/%-$(role).diff,$(GEN_DIRS)))
TESTS := $(wildcard */test.sh)

.PHONY: all clean force cleanchk check test

all: test $(GEN_TGTS)

clean: cleanchk
	rm -rf $(GEN_TGTS)

force: clean all

cleanchk:
	rm -rf $(CHK)

check: cleanchk $(CHK_TGTS) test

define GENERATOR_TEMPLATE =
$(1)/%-$(2).yaml: %/build.sh %/*
	@$$(MAKE) $$(MCMAKER)
	@[[ -d $$(@D) ]] || mkdir -p $$(@D)
	@echo "# Automatically generated by extra-manifests-builder" >$$@
	@echo "# Do not make changes directly." >>$$@
	@(cd $$(<D); MCPROLE="$(2)" ./$$(<F)) >>$$@
endef

$(foreach gendir,$(CHK) $(DST),\
  $(foreach role,master worker,\
    $(eval \
      $(call GENERATOR_TEMPLATE,$(gendir),$(role))\
    )\
  )\
)

$(CHK)/%.diff: $(CHK)/%.yaml
	@echo -n "Checking in-source $(DST)/$(<F) for consistency ... "; \
	if diff -u $(DST)/$(<F) $< >$@; then \
	  echo "Passed"; \
	else \
	  echo "Failed"; \
	  sed 's/^/  /' $@; \
	  exit 1; \
	fi

test:
	@failures=0; for t in $(TESTS); do \
	  testdir=$$(dirname $$t); \
	  echo -n "Testing $$t ... "; \
	  pushd $$testdir >/dev/null; \
	  logfile=/tmp/$$$$-$$testdir.out; \
	  if ./test.sh &>$$logfile; then \
	    echo "Passed"; \
	  else \
	    echo "Failed"; \
	    (( failures += 1 )); \
	    sed 's/^/  /' $$logfile; \
	  fi; \
	  rm -f $$logfile; \
	  popd >/dev/null; \
	done; \
	exit $$failures

$(MCMAKER):
	go install github.com/lack/mcmaker
