ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
PYTHON:=$(ROOT_DIR)/.venv/bin/python
SHELL:=/bin/bash

.PHONY: update
update: ## update the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all pull $(APP)

.PHONY: pull
pull: ## pull the latest image(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all pull $(APP)

.PHONY: up
up: ## starts the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all up -d $(APP) $(ARGS)

.PHONY: down
down: ## stop the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all down $(APP) $(ARGS)

.PHONY: stop
stop: # stop the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all stop $(APP) $(ARGS)

.PHONY: logs
logs: # show the log(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all logs $(APP) -ft $(ARGS)

.PHONY: restart
restart: # restart the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all restart $(APP) $(ARGS)

.PHONY: ps
ps: # show the status of the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all ps --format "table {{.Image}}\t{{.Status}}\t{{.Ports}}\t{{.Name}}"

.PHONY: config
config: # show the configration of the service(s)
	docker compose --project-directory "$(ROOT_DIR)" --profile all config $(APP) $(ARGS)

############################################################################################
# core services
############################################################################################

.PHONY: core-up
core-up: ## start just the core services (traefik, auth, etc).
	docker compose --project-directory "$(ROOT_DIR)" --profile core up -d

.PHONY: core-down
core-down: ## stop just the core services (traefik, auth, etc).
	docker compose --project-directory "$(ROOT_DIR)" --profile core down

.PHONY: core-logs
core-logs: ## stop just the core services (traefik, auth, etc).
	docker compose --project-directory "$(ROOT_DIR)" --profile core logs -ft

############################################################################################
# media services
############################################################################################

# TODO
# TODO
# TODO

############################################################################################
# configuration
############################################################################################

.PHONY: config-acme
config-acme: ## initializes the acme.json file
	mkdir -p appdata/traefik/acme/
	rm -f appdata/traefik/acme/acme.json
	touch appdata/traefik/acme/acme.json
	chmod 600 appdata/traefik/acme/acme.json

############################################################################################
# backup
############################################################################################

# TODO
# TODO
# TODO

############################################################################################
# development
############################################################################################

# TODO
# TODO
# TODO

############################################################################################
# general
############################################################################################

.PHONY: version
version: ## show the version of the project
	@git fetch --unshallow 2>/dev/null || true
	@git fetch --tags 2>/dev/null || true
	@echo "homelab $$(git describe --tags --abbrev=0)"