override REPL_OPTIONS += -ignore-dot-ghci
override GHCID_OPTIONS += --no-height-limit --reverse-errors

cabal := $(wildcard *.cabal)
package := $(notdir ./$(cabal:.cabal=))
version := $(shell sed -ne 's/^version: *\(.*\)/\1/p' $(cabal))
project := $(patsubst %.cabal,%,$(cabal))

all: build
build:
	cabal build $(CABAL_BUILD_FLAGS)
clean c:
	cabal clean
repl:
	cabal repl $(CABAL_REPL_FLAGS) $(project)
ghcid:
	ghcid $(GHCID_OPTIONS) -c 'cabal repl $(CABAL_REPL_FLAGS) $(project) $(addprefix --repl-options ,$(REPL_OPTIONS))'

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

tag:
	git tag --merged | grep -Fqx "$(package)-$(version)" || \
	git tag -f -s -m "$(package) v$(version)" $(package)-$(version)

tar:
	reuse lint
	cabal sdist
	cabal haddock --haddock-for-hackage --enable-doc
upload: LANG=C
upload: tar
	cabal upload $(CABAL_UPLOAD_FLAGS) dist-newstyle/sdist/$(package)-$(version).tar.gz
	cabal upload $(CABAL_UPLOAD_FLAGS) --documentation dist-newstyle/$(package)-$(version)-docs.tar.gz
%/publish: CABAL_UPLOAD_FLAGS+=--publish
%/publish: %
	
publish: upload/publish

nix-build:
	nix -L build
nix-relock:
	nix flake update --recreate-lock-file
nix-repl:
	nix -L develop --command cabal repl $(CABAL_REPL_FLAGS)
nix-shell:
	nix -L develop