db ?= -cf
dockerized ?= 1

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

.PHONY: all
all:
	@echo "TODO"

.PHONY: clean
clean:
	rm -rf node_modules .npm assets/icons well-known .well-known-cache
	rm -rf .stack .stack-work .stack-work-build .stack-work-run .stack-work-test .stack-work-doc
	rm -rf bin/

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

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

.PHONY: dockerized-frontend-%
dockerized-frontend-%: FRADRIVE_DOCKERFILE=frontend
dockerized-frontend-%: --dockerized-frontend-%;

.PHONY: dockerized-backend-%
dockerized-backend-%: FRADRIVE_DOCKERFILE=backend
dockerized-backend-%: --dockerized-backend-%;

#.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

# TODO: dockerify
.PHONY: backend-%
backend-%: --backend-%-dev;

# TODO: dockerify
.PHONY: backend-%-prod # TODO deprecated in favor of bins; remove
backend-%-prod: --backend-%-prod;

# TODO: dockerify
# TODO: this creates more binaries than just bin/uniworx as side effect! Document or change behaviour?
bin/uniworx: --backend-build-prod
bin/uniworxdev: --backend-build-dev

# TODO replace with bin/uniworxdev
.PHONY: --backend-%-dev
--backend-%-dev: stackopts=--flag uniworx:dev
--backend-%-dev: --backend-%;

# TODO deprecated in favor of binary targets
.PHONY: --backend-%-prod
--backend-%-prod: stackopts=--flag uniworx:-dev
--backend-%-prod: --backend-%;

.PHONY: --backend-build
--backend-build:
	stack build --fast --profile --library-profiling --executable-profiling --flag uniworx:-library-only --copy-bins --local-bin-path $$(pwd)/bin $(stackopts)

.PHONY: --backend-lint
--backend-lint:
	stack build --test --fast --flag uniworx:library-only uniworx:test:hlint $(stackopts)

.PHONY: backend-dependencies
backend-dependencies:
	stack build --fast --only-dependencies $(stackopts)

.PHONY: --backend-test
--backend-test:
	stack build --test --coverage --fast --flag uniworx:library-only $(stackopts)

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

.PHONY: database
database: .stack-work.lock bin/uniworxdb
	export SERVER_SESSION_ACID_FALLBACK=$${SERVER_SESSION_ACID_FALLBACK:-true}
	export AVSPASS=$${AVSPASS:-nopasswordset}
	stack exec uniworxdb -- $(db)

# TODO: same behaviour as bin/uniworx! Merge or tweak targets!
bin/uniworxdb: .stack-work.lock
	stack build --fast --flag uniworx:-library-only --copy-bins\ --local-bin-path\ $$(pwd)/bin\ --flag\ uniworx:-dev

.PHONY: frontend-%
frontend-%: node_modules well-known --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: package.json
	npm ci --cache .npm --prefer-offline

assets/icons: node_modules
	./utils/renamer.pl node_modules/@fortawesome/fontawesome-free/svgs/solid utils/rename-fa.json assets/icons/fradrive
	./utils/renamer.pl node_modules/@fortawesome/fontawesome-free/svgs/regular utils/rename-fa.json assets/icons/fradrive

well-known: node_modules assets/icons well-known/.well-known
	npx webpack --progress
# mark well-known directory as clean after successful webpack run:
	touch well-known/.well-known

well-known/.well-known:
# no-op target

# TODO: remove, as no npmrc is needed anymore 
# .npmrc:
# 	@# Using @echo instead of echo to avoid printing secret token to stdout
# 	@echo '@fortawesome:registry=https://npm.fontawesome.com/' > .npmrc
# 	@echo "//npm.fontawesome.com/:_authToken=$(FONTAWESOME_NPM_AUTH_TOKEN)" >> .npmrc

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

.stack-work.lock:
	[ "${FLOCKER}" != "$0" ] && exec env FLOCKER="$0" flock -en .stack-work.lock "$0" "$@" || :

.develop.env:
	[ ! -z "$(CI)" -o ! -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"

--dockerized-%:
	docker build --rm -t $(FRADRIVE_DOCKERFILE) ./docker/$(FRADRIVE_DOCKERFILE)
	docker run --rm -it -v .:/fradrive --user `id -u`:`id -g` --env FRADRIVE_MAKE_TARGET=$* $(FRADRIVE_DOCKERFILE)