cabal := $(wildcard *.cabal)
package := $(notdir ./$(cabal:.cabal=))
version := $(shell sed -ne 's/^version: *\(.*\)/\1/p' $(cabal))
project := $(patsubst %.cabal,%,$(cabal))
cabal_builddir ?= dist-newstyle
sourceDirs := $(wildcard src tests)

override REPL_OPTIONS += -ignore-dot-ghci
override GHCID_OPTIONS += --no-height-limit --reverse-errors --color=always --restart $(cabal) --warning

all: build
build:
	cabal build $(CABAL_BUILD_FLAGS)
clean c:
	cabal clean
repl:
	cabal repl $(CABAL_REPL_FLAGS) $(project)
ghcid:
	ghcid $(GHCID_OPTIONS) --command 'cabal repl -fno-code $(CABAL_REPL_FLAGS) $(project) $(addprefix --repl-options ,$(REPL_OPTIONS))'
ghciwatch:
	ghciwatch --watch src

.PHONY: tests
t tests:
	cabal test $(CABAL_TEST_FLAGS) \
	 --test-show-details always --test-options "$(TEST_OPTIONS)"
t/repl tests/repl:
	cabal repl -fno-code $(CABAL_REPL_FLAGS) $(CABAL_TEST_FLAGS) $(project)-tests
t/ghcid tests/ghcid:
	ghcid $(GHCID_OPTIONS) --command 'cabal repl -fno-code $(CABAL_REPL_FLAGS) $(project) $(addprefix --repl-options ,$(REPL_OPTIONS))' \
	  --run=':! ghcid $(GHCID_OPTIONS) --command "cabal repl -fno-code $(CABAL_REPL_FLAGS) $(CABAL_TEST_FLAGS) $(project)-tests" --test ":main $(TEST_OPTIONS)"'

%/accept: TEST_OPTIONS += --golden-start
%/accept: %
	

%/reset: TEST_OPTIONS += --golden-reset
%/reset: %
	

doc:
	cabal haddock --haddock-css ocean --haddock-hyperlink-source

.PHONY: ChangeLog.md
ChangeLog.md:
	! git tag --merged | grep -Fqx $(package)-$(version)
	git diff --exit-code
	git tag -f $(package)-$(version)
	git-chglog --output $@.new --tag-filter-pattern '$(package)-.*' $(package)-$(version)
	touch $@
	cat $@ >>$@.new
	mv -f $@.new $@
	git tag -d $(package)-$(version)
	git add '$@'
	git commit -m 'doc: update `$@`'
tag: build ChangeLog.md
	git tag -s -m $(package)-$(version) $(package)-$(version)

tar:
	git diff --exit-code
	reuse lint
	cabal sdist
	cabal haddock --haddock-for-hackage --enable-doc
upload: LANG=C
upload: tar
	git tag --merged | grep -Fqx $(package)-$(version)
	git push --follow-tags $(GIT_PUSH_FLAGS)
	cabal upload $(CABAL_UPLOAD_FLAGS) "$(cabal_builddir)"/sdist/$(package)-$(version).tar.gz
	cabal upload $(CABAL_UPLOAD_FLAGS) --documentation "$(cabal_builddir)"/$(package)-$(version)-docs.tar.gz
%/publish: CABAL_UPLOAD_FLAGS+=--publish
%/publish: %
	
publish: upload/publish

.PHONY: .hlint.yaml
.hlint.yaml:
	sed -i -e '/^# BEGIN: generated hints/,/^# END: generated hints/d' $@
	echo >>$@ '# BEGIN: generated hints'
	find $(sourceDirs) -name "*.hs" | xargs -P $(shell nproc) -I {} \
	  hlint --find {} | grep -- '- fixity:' | sort -u >>$@
	echo >>$@ '# END: generated hints'
lint: .hlint.yaml
	if hlint --quiet --report=hlint.html $(HLINT_FLAGS) $(sourceDirs); \
	then rm -f hlint.html; \
	else sensible-browser ./hlint.html & fi
lint/apply: .hlint.yaml
	find $(sourceDirs) -name "*.hs" | xargs -P $(shell nproc) -I {} \
	  hlint --refactor-options="-i" --refactor {}

style:
	find $(sourceDirs) -name "*.hs" | xargs -P $(shell nproc) -I {} \
	  fourmolu -q -o -XImportQualifiedPost --mode inplace {}
	cabal-fmt -i *.cabal