export SHELL=bash

export CLEAN_DEPENDENCIES ?= false
export CLEAN_IMAGES ?= false

export ENTRYPOINT ?= bash
export SRC

.PHONY: help
# HELP: print out this help message
help:
	docker compose run help

.PHONY: clean
# HELP: clean compilation caches
clean:
	$(MAKE) clean-frontend CLEAN_DEPENDENCIES=$(CLEAN_DEPENDENCIES) CLEAN_IMAGES=$(CLEAN_IMAGES) 
	$(MAKE) clean-backend CLEAN_DEPENDENCIES=$(CLEAN_DEPENDENCIES) CLEAN_IMAGES=$(CLEAN_IMAGES)
.PHONY: clean-all
# HELP: clean everything, including dependency and image caches
clean-all: CLEAN_DEPENDENCIES = true
clean-all: CLEAN_IMAGES = true
clean-all: clean ;

.PHONY: clean-%
# HELP(clean-$SERVICE): invalidate caches for a given service. Supported services: frontend, backend.
clean-%:
	$(MAKE) stop-$*
	@$(MAKE) -- --clean-$*
	@echo "Cleaned $* build files and binaries."
ifeq ("$(CLEAN_DEPENDENCIES)", "true")
	@$(MAKE) -- --clean-$*-deps
	@echo "Cleaned $* dependencies."
endif
ifeq ("$(CLEAN_IMAGES)", "true")
	$(MAKE) kill-$*
	docker compose rm --force --volumes
	docker compose down --rmi 'all' --volumes
	@echo "Cleaned $* image."
endif
--clean-frontend:
	-rm -rf assets/icons assets/favicons
	-rm -rf static well-known
--clean-frontend-deps:
	-rm -rf frontend/node_modules
	-rm -rf frontend/.npm
--clean-backend:
	-rm -rf backend/.stack-work
	-rm -rf bin/
--clean-backend-deps:
	-rf -rf backend/.stack


# TODO: only release when build and tests are passing!!!
.PHONY: release
# HELP: create, commit and push a new release
release:
	VERSION=`./utils/version.pl -changelog CHANGELOG.md -v` ; \
	git add CHANGELOG.md ; \
	git commit -m "chore(release): $${VERSION}" ; \
	git push ; \
	git tag $${VERSION} ; \
	git push origin $${VERSION}

.PHONY: compile
# HELP: perform full compilation (frontend and backend)
compile: compile-frontend compile-backend ;
.PHONY: compile-%
# HELP(compile-$SERVICE): compile a given service once
compile-%:
	docker compose run --remove-orphans --build --no-deps $* make compile

.PHONY: start
# HELP: start complete development environment with a fresh test database
start: start-postgres start-maildev start-memcached start-minio start-backend
	docker compose exec backend make start
.PHONY: start-%
# HELP(start-$SERVICE): start a given service
start-%:
	docker compose up -d --build $*

.PHONY: shell-%
# HELP(shell-$SERVICE): launch a (bash) shell inside a given service
shell-%:
	docker compose run --build --no-deps --entrypoint="$(ENTRYPOINT)" $*
.PHONY: ghci
# HELP: launch ghci instance. Use in combination with SRC to specify the modules to be loaded by ghci: make ghci SRC=src/SomeModule.hs
ghci: ENTRYPOINT=stack ghci $(SRC)
ghci: shell-backend ;

.PHONY: stop
# HELP: stop all services
stop:
	docker compose down
.PHONY: stop-%
# HELP(stop-$SERVICE): stop a given service
stop-%:
	docker compose down $*
.PHONY: kill-%
# HELP(kill-$SERVICE): kill a given service the hard way. Use this if the servive does not respond to stop.
kill-%:
	docker compose kill $*

.PHONY: status
# HELP: print an overview of currently running services and their health
status:
	docker compose ps
.PHONY: top
# HELP: print an overview of the ressource usage of the currently running services
top:
	docker compose stats
.PHONY: list-projects
# HELP: list all currently running projects on this machine
list-projects:
	docker compose ls

.PHONY: log-%
# HELP(log-$SERVICE): follow the output of a given service. Service must be running.
log-%:
	docker compose logs --follow --timestamps $*

.PHONY: --%
.SUFFIXES: # Delete all default suffixes
