db ?= -cf

.PHONY: help
help:
	@echo "Nothing to see here, go away"

.PHONY: build
build: frontend-build backend-build

#.PHONY: start
#start: frontend-build
#	$(MAKE) --jobs=2 frontend-watch backend-start

.PHONY: lint
lint: frontend-lint backend-lint

.PHONY: test
test: frontend-test backend-test i18n-check

.PHONY: backend-%
backend-%: .develop.env .stack-work.lock # --stack-work-$*
	$(MAKE) -- --backend-$*
	#( $(MAKE) -- --backend-$* || $(MAKE) -- --stack-krow-$* ) && ( $(MAKE) -- --stack-krow-$* )

.PHONY: --backend-build
--backend-build: .develop.env
	stack build --fast --profile --library-profiling --executable-profiling --flag uniworx:dev --flag uniworx:-library-only

#.PHONY: --backend-start
#--backend-start: .develop.env
#	./start.sh

.PHONY: backend-lint
backend-lint: .develop.env
	stack build --test --fast --flag uniworx:dev --flag uniworx:library-only uniworx:test:hlint

.PHONY: --backend-test
--backend-test: .develop.env
	stack build --test --coverage --fast --flag uniworx:dev --flag uniworx:library-only

.PHONY: i18n-check
i18n-check:
	./missing-translations.sh
	@echo No missing translations.

.PHONY: database
database: .develop.env .stack-work.lock # --stack-work-build
	# $(MAKE) -- --database
	( $(MAKE) -- --database || $(MAKE) -- --stack-krow-build ) && ( $(MAKE) -- --stack-krow-build )

.PHONY: --database
--database: .develop.env
	stack build --fast --flag uniworx:-library-only --flag uniworx:dev
	export SERVER_SESSION_ACID_FALLBACK=$${SERVER_SESSION_ACID_FALLBACK:-true}
	stack exec uniworxdb -- $(db)

.PHONY: frontend-%
frontend-%: node_modules
	$(MAKE) -- --frontend-$*

.PHONY: --frontend-build
--frontend-build:
	npx -- webpack --progress $(WATCH)

.PHONY: --frontend-watch
--frontend-watch: WATCH=--watch
--frontend-watch: --frontend-build

.PHONY: --frontend-lint
--frontend-lint: .eslintrc.json
	npx -- eslint frontend/src $(FIX)
	@echo Hooray! There are no hints.

.PHONY: --frontend-test
--frontend-test: karma.conf.js
	npx -- karma start --conf karma.conf.js $(WATCH)

.PHONY: --frontend-test-watch
--frontend-test-watch: WATCH=--single-run false
--frontend-test-watch: --frontend-test

node_modules: .npmrc package.json
	npm ci --cache .npm --prefer-offline

.npmrc:
	command gup .npmrc

.PHONY: clean
clean:
	rm -rf node_modules
	rm -rf .stack-work .stack-work-build .stack-work-run .stack-work-test .stack-work-doc

.PHONY: %.lock
%.lock:
	[ -e $@ ] || touch $@
	flock -en $@ true

.PHONY: .develop.env
.develop.env:
	[[ ! -z "$(CI)" || ! -z "$$PORT_OFFSET" ]] || ( echo "Not in develop!" 2>&1 ; exit 1 )

# .PHONY: --stack-work-%
# --stack-work-%: .stack-work.lock
# 	[[ -d .stack-work && -d .stack-work-$* ]] && (echo ".stack-work collides with .stack-work-$*, please resolve manually" 1>&2; exit 1) || :
# 	[[ ! -d .stack-work-$* ]] || mv .stack-work-$* .stack-work
# 
# .PHONY: --stack-krow-%
# --stack-krow-%:
# 	[[ ! -d .stack-work-$* ]] || ( echo ".stack-work-$* already exists, please resolve manually" 1>&2 ; exit 1 )
# 	mv .stack-work .stack-work-$*
# 	flock -u .stack-work.lock true

# watch:
# 	while true; do \
# 
# start: frontend-build frontend-build-watch yesod-start
# 	run-s frontend:build --parallel "frontend:build:watch" "yesod:start"
