cabal := $(shell find . -name '*.cabal' -print -quit)
package := $(notdir ./$(cabal:.cabal=))

all: build

build:
	stack $(STACK_FLAGS) build $(STACK_BUILD_FLAGS)

.PHONY: test
test:
	! grep -q '^Test-Suite\>' $(cabal) || \
	{ stack $(STACK_FLAGS) test $(STACK_TEST_FLAGS); }

GHCID_TEST_ARGS ?=
GHCID_TEST ?= ":main $(GHCID_TEST_ARGS)"
ghcid:
	ghcid \
	 --warnings \
	 --command "stack ghci \
	   $(package):lib \
	   $(package):$(package) \
	   --ghci-options=-fobject-code" \
	 --test $(GHCID_TEST) \
	 $(GHCID_FLAGS)

clean:
	stack $(STACK_FLAGS) clean $(STACK_CLEAN_FLAGS)
cleaner:
	stack $(STACK_FLAGS) clean --full $(STACK_CLEAN_FLAGS)

%/fast: override STACK_BUILD_FLAGS+=--fast
%/fast: override STACK_HADDOCK_FLAGS+=--fast
%/fast: override STACK_TEST_FLAGS+=--fast
%/fast: %
	

%/debug: override STACK_BUILD_FLAGS+=--flag $(cabal:.cabal=):debug
%/debug: override STACK_TEST_FLAGS+=--flag $(cabal:.cabal=):debug
%/debug: %
	

doc:
	stack $(STACK_FLAGS) haddock $(STACK_HADDOCK_FLAGS)
%.html: %.md
	markdown $*.md >$*.html
%.html/view: %.html
	sensible-browser $*.html

HLint.hs: $(shell find . -name '*.hs' -not -name 'HLint.hs')
	sed -i -e '/^-- BEGIN: generated hints/,/^-- END: Generated by hlint/d' HLint.hs
	echo '-- BEGIN: generated hints' >> HLint.hs
	hlint --find . | grep '^'infix | sort -u >> HLint.hs
	echo '-- END: generated hints' >> HLint.hs

lint: HLint.hs
	if hlint --quiet --report=hlint.html -XNoCPP \
	 $(shell cabal-cargs --format=ghc --only=default_extensions --sourcefile=$(cabal)) $(HLINT_FLAGS) .; \
	then rm -f hlint.html; \
	else sensible-browser ./hlint.html & fi

tag:
	name=$$(sed -ne 's/^name: *\(.*\)/\1/p' "$(cabal)"); \
	version=$$(sed -ne 's/^version: *\(.*\)/\1/p' "$(cabal)"); \
	git tag --merged | grep -Fqx "$$name-$$version" || \
	git tag -f -s -m "$$name v$$version" $$name-$$version

tar:
	stack $(STACK_FLAGS) sdist $(STACK_SDIST_FLAGS)
upload:
	LANG=C stack $(STACK_FLAGS) upload $(STACK_UPLOAD_FLAGS) .

.PHONY: stats
stats:
	gitstats . $@
stats/view: stats
	sensible-browser stats/index.html

#
##  prof
###
GHC_PROF_CATEGORIES:=hc hm hd hy hr hb
GHC_PROF_CPUS:=
GHC_PROF_PS_WIDTH:=11in
GHC_PROF_RATE:=0.1
GHC_PROF_CC_LENGTH:=50
STACK_ROOT:=$(shell stack path --local-install-root)

TCT_COMMANDS:=html5
TCT_INPUTS:=tct
TCT_PRINT_PROF=$(STACK_ROOT)/bin/tct-print

commit:=$(shell if which git >/dev/null; then git describe --long --always; else echo COMMIT; fi)
if_arg=$(if $2,$1 $2)
rmw=$(call if_arg,rm,$(wildcard $1))
rmdirw=$(call if_arg,rmdir -p --ignore-fail-on-non-empty,$(wildcard $1))

.PHONY: $(TCT_PRINT_PROF)
$(TCT_PRINT_PROF):
	stack $(STACK_FLAGS) build --profile --flag tct:prof $(STACK_BUILD_FLAGS)

.PHONY: prof
prof: $(addprefix prof/,$(TCT_COMMANDS))

define prof/command
tests:=$(wildcard test/$(command)/*.tct)
prof/clean: prof/$(command)/clean
prof/$(command)/clean:
	$$(call rmw,   prof/$(command)/$(commit)/*.out)
	$$(call rmdirw,prof/$(command)/$(commit))
prof/$(command)/%/clean: \
 $(foreach hC,$(GHC_PROF_CATEGORIES),prof/$(command)/%/$(hC)/clean)
	$$(call rmw,prof/$(command)/$$*)

prof/$(command)/$(commit):
	mkdir -p "$$@"

endef

define prof/command/hC
.PHONY: prof/$(command)
prof/$(command):       prof/$(command)/$(hC)
prof/$(command)/clean: prof/$(command)/$(hC)/clean

prof/$(command)/$(hC): $(patsubst test/%,prof/%/$(hC),$(tests))
.PRECIOUS: $(patsubst test/$(command)/%, \
                      prof/$(command)/$(commit)/%.$(hC).hp, \
                      $(tests))
.PRECIOUS: $(patsubst test/$(command)/%, \
                      prof/$(command)/$(commit)/%.$(hC).ps, \
                      $(tests))
prof/$(command)/$(hC)/clean:
	$$(call rmw, \
	 prof/$(command)/$(commit)/*.$(hC).aux \
	 prof/$(command)/$(commit)/*.$(hC).hp \
	 prof/$(command)/$(commit)/*.$(hC).prof \
	 prof/$(command)/$(commit)/*.$(hC).ps \
	 prof/$(command)/$(commit)/*.$(hC).stats )

prof/$(command)/%: prof/$(command)/%/$(hC) \
                   prof/$(command)/$(commit)/%.$(hC).hs
prof/$(command)/%/$(hC): prof/$(command)/$(commit)/%.$(hC).ps
	

prof/$(command)/$(commit)/%.$(hC).prof \
prof/$(command)/$(commit)/%.$(hC).hp \
prof/$(command)/%.$(command): \
 $$(TCT_PRINT_PROF) \
 test/$(command)/% \
 $(TCT_PRINT_PROF) \
 | prof/$(command)/$(commit)
	GHCRTS=' \
	 -$(hC)$$(GHC_PROF_$(hC)) \
	 -i$$(GHC_PROF_RATE) \
	 -L$$(GHC_PROF_CC_LENGTH) \
	 $$(if $$(GHC_PROF_CPUS),-N$$(GHC_PROF_CPUS)) \
	 -tprof/$(command)/$(commit)/$$*.$(hC).stats \
	 -p \
	 $$(GHCRTS)' \
	$$(TCT_PRINT_PROF) $$(TCT_FLAGS) \
	 $(command) $$(TCT_COMMAND_FLAGS) test/$(command)/$$* \
	 >prof/$(command)/$(commit)/$$*.out
	mv $(notdir $(TCT_PRINT_PROF)).hp   prof/$(command)/$(commit)/$$*.$(hC).hp
	mv $(notdir $(TCT_PRINT_PROF)).prof prof/$(command)/$(commit)/$$*.$(hC).prof

prof/$(command)/%/$(hC)/clean:
	$$(call rmw, \
	 prof/$(command)/$(commit)/$$*.$(hC).aux \
	 prof/$(command)/$(commit)/$$*.$(hC).hp \
	 prof/$(command)/$(commit)/$$*.$(hC).prof \
	 prof/$(command)/$(commit)/$$*.$(hC).ps \
	 prof/$(command)/$(commit)/$$*.$(hC).stats )

endef

$(foreach command,$(TCT_COMMANDS), \
 $(eval $(call prof/command)) \
 $(foreach hC,$(GHC_PROF_CATEGORIES), \
  $(eval $(call prof/command/hC)) ))

%.hC.hp: $(foreach hC,$(GHC_PROF_CATEGORIES),%.$(hC).hp)
	
%.hC.ps: $(foreach hC,$(GHC_PROF_CATEGORIES),%.$(hC).ps)
	
%.ps %.aux: %.hp
	(cd $(@D) && hp2ps -b -c -e$(GHC_PROF_PS_WIDTH) -g $(notdir $*.hp))