bins = holo holo-files
mans = holorc.5 holo-plugin-interface.7 holo-test.7 holo.8 holo-files.8

default: $(addprefix bin/,$(bins)) $(addprefix man/,$(mans))
.PHONY: default

GO_BUILDFLAGS =
GO_LDFLAGS    = -s -w
GO_TESTFLAGS  = -covermode=count

src/holo/version.go: FORCE
	printf 'package main\n\nconst version = "%s"\n' "$$( ./util/find_version.sh)" | util/write-ifchanged $@

$(addprefix %/,$(bins)): FORCE src/holo/version.go
	GOPATH=$(abspath .) go install $(GO_BUILDFLAGS) --ldflags '$(GO_LDFLAGS)' $(bins)
bin/%.test: bin/% src/main.go.test
	GOPATH=$(abspath .) go test -c -o $@ $(GO_TESTFLAGS) -coverpkg $*/... $*

man:
	@mkdir $@

# manpages are generated using pod2man (which comes with Perl and therefore
# should be readily available on almost every Unix system)
man/%: doc/%.pod | man
	pod2man --name="$(shell echo $* | cut -d. -f1)" --section=$(shell echo $* | cut -d. -f2) \
		--center="Configuration Management" --release="Holo $(VERSION)" \
		$< $@

test: check # just a synonym
check: default test/cov.html test/cov.func.txt
.PHONY: test check
test/cov.cov: clean-tests util/holo-test util/gocovcat.go $(foreach b,$(bins),bin/$b.test)
	@if s="$$(gofmt -l src 2>/dev/null)"                        && test -n "$$s"; then printf ' => %s\n%s\n' gofmt  "$$s"; false; fi
	@if s="$$(find src -type d -exec golint {} \; 2>/dev/null)" && test -n "$$s"; then printf ' => %s\n%s\n' golint "$$s"; false; fi
	@GOPATH=$(abspath .) go test $(GO_TESTFLAGS) -coverprofile=test/cov/holo-output.cov holo/impl
	@HOLO_BINARY=../../bin/holo.test HOLO_TEST_COVERDIR=$(abspath test/cov) util/holo-test holo $(sort $(wildcard test/??-*))
	util/gocovcat.go test/cov/*.cov > test/cov.cov
%.html: %.cov
	GOPATH=$(abspath .) go tool cover -html $< -o $@
%.func.txt: %.cov
	GOPATH=$(abspath .) go tool cover -func $< -o $@

DIST_IDS = $(shell [ -f /etc/os-release ] && source /etc/os-release || source /usr/lib/os-release; echo "$$ID $$ID_LIKE")

install: default conf/holorc conf/holorc.holo-files util/holo-test util/autocomplete.bash util/autocomplete.zsh
	install -d -m 0755 "$(DESTDIR)/var/lib/holo/files"
	install -d -m 0755 "$(DESTDIR)/var/lib/holo/files/base"
	install -d -m 0755 "$(DESTDIR)/var/lib/holo/files/provisioned"
	install -d -m 0755 "$(DESTDIR)/usr/share/holo"
	install -d -m 0755 "$(DESTDIR)/usr/share/holo/files"
	install -D -m 0644 conf/holorc            "$(DESTDIR)/etc/holorc"
	install -D -m 0644 conf/holorc.holo-files "$(DESTDIR)/etc/holorc.d/10-files"
	install -D -m 0755 bin/holo               "$(DESTDIR)/usr/bin/holo"
	install -D -m 0755 bin/holo-files         "$(DESTDIR)/usr/lib/holo/holo-files"
	install -D -m 0755 util/holo-test         "$(DESTDIR)/usr/bin/holo-test"
	install -D -m 0644 util/autocomplete.bash "$(DESTDIR)/usr/share/bash-completion/completions/holo"
	install -D -m 0644 util/autocomplete.zsh  "$(DESTDIR)/usr/share/zsh/site-functions/_holo"
	install -D -m 0644 man/holorc.5                "$(DESTDIR)/usr/share/man/man5/holorc.5"
	install -D -m 0644 man/holo.8                  "$(DESTDIR)/usr/share/man/man8/holo.8"
	install -D -m 0644 man/holo-files.8            "$(DESTDIR)/usr/share/man/man8/holo-files.8"
	install -D -m 0644 man/holo-test.7             "$(DESTDIR)/usr/share/man/man7/holo-test.7"
	install -D -m 0644 man/holo-plugin-interface.7 "$(DESTDIR)/usr/share/man/man7/holo-plugin-interface.7"
ifneq ($(filter arch,$(DIST_IDS)),)
	install -D -m 0644 distribution-integration/alpm.hook    "$(DESTDIR)/usr/share/libalpm/hooks/01-holo-resolve-pacnew.hook"
	install -D -m 0755 distribution-integration/alpm-hook.sh "$(DESTDIR)/usr/share/libalpm/scripts/holo-resolve-pacnew"
endif
.PHONY: install

clean: clean-tests
	rm -fr -- pkg bin man
	rm -f -- src/holo/version.go
clean-tests:
	rm -fr -- test/*/{target,tree,{colored-,}{apply,apply-force,diff,scan}-output}
	rm -f -- test/cov.* test/cov/*
.PHONY: clean clean-tests

.PHONY: FORCE
