SHELL=bash
# MAKE=make -d

export CONTAINER_COMMAND ?= podman
export CONTAINER_RUN ?= $(CONTAINER_COMMAND) run -it --network=host

export IN_CONTAINER ?= false
export IN_CI ?= false

export WATCH
export db ?= -cf

export DEV_PORT_HTTP
export DEV_PORT_HTTPS

export IMAGE_REGISTRY = docker.io
export MEMCACHED_IMAGE = $(IMAGE_REGISTRY)/memcached:latest
export MINIO_IMAGE = $(IMAGE_REGISTRY)/minio/minio:latest
export MAILDEV_IMAGE = $(IMAGE_REGISTRY)/maildev/maildev:latest

# HELP HEADER START
# To see the definition of all available targets, take a look into the Makefile.
# Targets starting with '--' are not meant to be directly called.
# If you want to do so anyway please use 'make -- [--target]' to not
# have '[--target]' to be treated as option to 'make'.
# 
# 
# Targets meant to be used by humans are:
# HELP HEADER END

###########################
##### GENERAL TARGETS #####

.PHONY: --%

.PHONY: help
# HELP: print out this help message
help:
	@if [ -z "$$(which perl 2>/dev/null)" ] ; then \
	$(CONTAINER_RUN) .:/mnt 'debian:12.5' '/mnt/utils/makehelp.pl' '/mnt/Makefile' ; \
	else \
	utils/makehelp.pl Makefile ; \
	fi

.PHONY: clean
# HELP: remove all compilation results in the directory but leave containers and images unharmed
clean:
	rm -rf node_modules .npm .cache assets/icons assets/favicons static well-known
	rm -rf .stack .stack-work .stack-work-build .stack-work-run .stack-work-test .stack-work-doc
	rm -rf bin/ .Dockerfile .dev-port-http .dev-port-https
.PHONY: clean-all
# HELP: like clean but with container and image prune
clean-all: clean
	$(CONTAINER_COMMAND) system prune --all --force --volumes
	$(CONTAINER_COMMAND) image prune --all --force
	$(CONTAINER_COMMAND) volume prune --force

.PHONY: release
# HELP: unfinished
release:
	./.gitlab-ci/version.pl -changelog CHANGELOG.md
	git add CHANGELOG.md
	VERSION=`.gitlab-ci/version.pl`
	git tag $${VERSION}
	git commit -m "chore(release): $${VERSION}"
	git push

.PHONY: %-shell
%-shell:
	MOUNT_DIR=/mnt/fradrive ; \
	FRADRIVE_SERVICE=$* ; \
	$(MAKE) -- --image-build FRADRIVE_SERVICE=$${FRADRIVE_SERVICE} ; \
	$(CONTAINER_RUN) -v $(PWD):$${MOUNT_DIR} --env IN_CONTAINER=true --entrypoint /bin/bash --name fradrive.$${FRADRIVE_SERVICE}.interactive.$$(date +'%Y-%m-%dT%H-%M-%S') fradrive/$${FRADRIVE_SERVICE}

##### GENERAL TARGETS #####
###########################


############################################
##### UNIFIED FRONTEND/BACKEND TARGETS #####

# TODO: rewrite all serve-targets to do the following:
#   - return immediately
#   - podman-run with -d (detached mode) and save the container id in a file
#   - provide commands (make-targets?) to access logs of running containers
#   - additionally provide targets for termination of running containers
.PHONY: serve
# HELP: serve frontend, backend, and database
serve:
	$(MAKE) serve-database &
	$(MAKE) serve-memcached &
	$(MAKE) serve-minio &
#	$(MAKE) serve-maildev & # TODO: needs different port than 1025 to avoid conflicts
	$(MAKE) serve-frontend &
	$(MAKE) serve-backend

.PHONY: compile
# HELP: compile frontend and backend
compile: compile-frontend compile-backend

.PHONY: lint
# HELP: lint frontend and backend
lint: lint-frontend lint-backend

.PHONY: test
# HELP: test frontend, backend, and check internationalization
test: test-frontend test-backend i18n-check

##### UNIFIED FRONTEND/BACKEND TARGETS #####
############################################


############################
##### FRONTEND TARGETS #####

.PHONY: %-frontend
%-frontend: FRADRIVE_SERVICE=frontend
%-frontend: --image-build --containerized---%-frontend;

--%-frontend: --containerized-static-frontend --containerized-well-known-frontend;

# HELP(compile-frontend): compile frontend
--compile-frontend: static well-known
	npm run build

.PHONY: serve-frontend
# HELP: serve frontend (watch file changes)
serve-frontend:
	$(MAKE) -- --containerized---compile-frontend WATCH=--watch

# --serve-frontend: WATCH=--watch
# --serve-frontend: --compile-frontend;

# HELP(lint-frontend): lint frontend
--lint-frontend: eslint.config.js
	npx -- eslint frontend/src $(FIX)
	@echo Hooray! There are no hints.

# HELP(test-frontend): test frontend
--test-frontend: karma.conf.cjs
	@echo Karma frontend tests are currently broken after npm update and have therefor been temporarily disabled.
# npx -- karma start --conf karma.conf.cjs $(WATCH)

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

package-lock.json: package.json
	npm install --cache .npm --prefer-offline

assets: assets/favicons assets/icons;
assets/favicons:
	./utils/faviconize.pl assets/favicon.svg long assets/favicons
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

static: node_modules esbuild.config.mjs assets
	npm run build
well-known: static;

##### FRONTEND TARGETS #####
############################


###########################
##### BACKEND TARGETS #####

.PHONY: %-backend
%-backend: FRADRIVE_SERVICE=backend
%-backend: --image-build --containerized---%-dev-backend;

.PHONY: %-prod-backend
%-prod-backend: FRADRIVE_SERVICE=backend
%-prod-backend: --image-build --containerized---%-prod-backend;

--%-dev-backend: FRADRIVE_SERVICE=backend
--%-dev-backend: stackopts=--flag uniworx:dev
--%-dev-backend: --image-build --containerized---%-backend;

--%-prod-backend: FRADRIVE_SERVICE=backend
--%-prod-backend: stackopts=--flag uniworx:-dev
--%-prod-backend: --image-build --containerized-%-backend;

.PHONY: serve-backend
# HELP: serve backend
serve-backend:
	DEV_PORT_HTTP=`docker/backend/dev_port.pl 3000 | tee .dev-port-http`; \
	DEV_PORT_HTTPS=`docker/backend/dev_port.pl 3443 | tee .dev-port-https`; \
	$(MAKE) -- --containerized---serve-dev-backend DEV_PORT_HTTP=$${DEV_PORT_HTTP} DEV_PORT_HTTPS=$${DEV_PORT_HTTPS}
--serve-dev-backend: start.sh
	DEV_PORT_HTTP=`cat .dev-port-http`; \
	DEV_PORT_HTTPS=`cat .dev-port-https`; \
	./start.sh

# HELP(compile-backend): compile backend
--compile-backend:
	stack build --fast --profile --library-profiling --executable-profiling --flag uniworx:-library-only --local-bin-path $$(pwd)/bin $(stackopts)

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

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

# HELP(db-cf-backend): clear and fill database. requires running postgres
# TODO (db-m-$MIGRATION-backend): apply migration (see src/Model/Migration/Definition.hs for list of available migrations)
PHONY: --db-%-backend
--db-%-dev-backend: .stack
	SERVER_SESSION_ACID_FALLBACK=${SERVER_SESSION_ACID_FALLBACK:-true} ; \
	AVSPASS=${AVSPASS:-nopasswordset} ; \
	stack exec uniworxdb -- -$*

.stack: stack.yaml stack.yaml.lock
.stack:
	$(MAKE) -- --image-run---.stack
--.stack: stack.yaml stack.yaml.lock
	stack build --fast --only-dependencies $(stackopts)

##### BACKEND TARGETS #####
###########################


############################
##### DATABASE TARGETS #####

.PHONY: serve-database
# HELP: serve database
serve-database: --containerized-database;

--containerized-database: FRADRIVE_SERVICE=database
# port forwarding is disabled in --network=host mode; nevertheless it is stated here for documentation reasons
--containerized-database: docker/database/initdb.sh docker/database/pg_hba.conf docker/database/postgresql.conf docker/database/schema.sql --image-build
	if [ "$(IN_CONTAINER)" == "false" ] ; then \
	  $(CONTAINER_RUN) --name fradrive.$(FRADRIVE_SERVICE).$$(date +'%Y-%m-%dT%H-%M-%S') fradrive/$(FRADRIVE_SERVICE) ; \
	fi

.PHONY: serve-memcached
serve-memcached:
	$(CONTAINER_RUN) $(MEMCACHED_IMAGE)

.PHONY: serve-minio
serve-minio:
	$(CONTAINER_RUN) $(MINIO_IMAGE) -- server $$(mktemp)

##### DATABASE TARGETS #####
############################


#############################
##### CONTAINER TARGETS #####

.PHONY: --containerized-%-frontend
--containerized-%-frontend: FRADRIVE_SERVICE=frontend
--containerized-%-frontend: --image-build
	$(MAKE) -- --image-run-$*-frontend

--containerized-%-backend: FRADRIVE_SERVICE=backend
--containerized-%-backend: --image-build
	$(MAKE) -- --image-run-$*-backend 

.PHONY: image-rebuild
# HELP: rebuild the stated docker image (frontend, backend, database instead of %)
image-rebuild-%:
	$(MAKE) -- --image-build FRADRIVE_SERVICE=$* NO_CACHE=--no-cache
--image-build:
	rm -f .Dockerfile
	ln -s docker/$(FRADRIVE_SERVICE)/Dockerfile .Dockerfile
	MOUNT_DIR=/mnt/fradrive; \
	PROJECT_DIR=/mnt/fradrive; \
	if [ "$(IN_CI)" == "true" ] ; then \
	  PROJECT_DIR=/fradrive; \
	fi; \
	if [ "$(IN_CONTAINER)" == "false" ] ; then \
	  $(CONTAINER_COMMAND) build $(NO_CACHE) -v $(PWD):$${MOUNT_DIR} --env IN_CONTAINER=true --build-arg MOUNT_DIR=$${MOUNT_DIR} --build-arg PROJECT_DIR=$${PROJECT_DIR} --tag fradrive/$(FRADRIVE_SERVICE) --file $(PWD)/.Dockerfile ; \
	fi

--image-run-%:
	MOUNT_DIR=/mnt/fradrive; \
	if [ "$(IN_CONTAINER)" == "true" ] ; then \
	  $(MAKE) -- $* ; \
	else \
	  $(CONTAINER_RUN) -v $(PWD):$${MOUNT_DIR} --env IN_CONTAINER=true --env FRADRIVE_MAKE_TARGET=$* --env WATCH=$(WATCH) --name fradrive.$(FRADRIVE_SERVICE).$$(date +'%Y-%m-%dT%H-%M-%S') localhost/fradrive/$(FRADRIVE_SERVICE) ; \
	fi

##### CONTAINER TARGETS #####
#############################


.PHONY: i18n-check
# HELP: check internationalization
i18n-check: --image-run---i18n-check
--i18n-check:
	./missing-translations.sh
	@echo No missing translations.

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