DOCKER_COMPOSE=docker compose -f docker-compose-local.yml

.DEFAULT_GOAL := help

.PHONY: init docker-up docker-up-tibi-dev docker-start docker-start-tibi-dev docker-down docker-ps docker-logs yarn-upgrade fix-permissions

include ./.env

help: ## show this help
	@echo MAKE TARGETS
	@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2}'

init: ## initialize required directories
	mkdir -p tmp/mongo-data tmp/.npm tmp/.yarn tmp/nonexistent

docker-up: init ## bring docker compose stack up in background
	$(DOCKER_COMPOSE) --profile tibi up -d

docker-up-tibi-dev: init ## bring docker compose stack up in background with tibi-dev
	$(DOCKER_COMPOSE) --profile tibi-dev up -d

docker-up-chisel: ## bring up chisel tunnel
	$(DOCKER_COMPOSE) --profile chisel up -d

docker-down: ## take docker compose stack down
	$(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel down

docker-start: init ## start docker compose stack in foreground and take it down after CTRL-C
	$(DOCKER_COMPOSE) --profile tibi up; $(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel down

docker-start-tibi-dev: init ## start docker compose stack in foreground and take it down after CTRL-C (with tibi-dev)
	$(DOCKER_COMPOSE) --profile tibi-dev up; $(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel down

docker-ps: ## show container state
	$(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel ps

docker-logs: ## show docker logs and follow
	$(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel logs -f --tail=100 || true

docker-pull: ## pull docker images
	$(DOCKER_COMPOSE) --profile tibi-dev --profile tibi --profile chisel pull

docker-%:
	$(DOCKER_COMPOSE) $*

yarn-upgrade: ## interactive yarn upgrade
	$(DOCKER_COMPOSE) run --rm yarnstart yarn upgrade-interactive
	$(DOCKER_COMPOSE) restart yarnstart

fix-permissions: ## set files/directories owner to UID:GID from .env
	sudo chown -R $(CODER_UID):$(CODER_GID) ./

mongo-sync-master-to-local: ## sync mongo from master to local
	$(DOCKER_COMPOSE) up mongo -d
	read -s -p "Enter chisel password: " CHISEL_PASSWORD; \
		chisel client --auth coder:$$CHISEL_PASSWORD http://$(PRODUCTION_SERVER):10987 27017:mongo:27017 &
	sleep 3
	mongodump --archive --gzip --db=$(PRODUCTION_TIBI_PREFIX)_$(TIBI_NAMESPACE) | $(DOCKER_COMPOSE) exec -T mongo mongorestore --archive --gzip --nsFrom='$(PRODUCTION_TIBI_PREFIX)_$(TIBI_NAMESPACE).*' --nsTo='tibi_$(TIBI_NAMESPACE).*' --nsInclude='$(PRODUCTION_TIBI_PREFIX)_$(TIBI_NAMESPACE).*' --drop
	sleep 3
	killall chisel

media-sync-master-to-local: ## sync attachments from master to local
	rsync -v -e "ssh -i ~/.ssh/id_deploy -p 22223 -l 10052300,33,$(PRODUCTION_PATH)/media" -az --info=progress2 ftp1.webmakers.de:/ media/

mongo-sync-local-to-staging: ## sync mongo from local to staging area
	$(DOCKER_COMPOSE) up mongo -d
	read -s -p "Enter chisel password: " CHISEL_PASSWORD; \
		chisel client --auth coder:$$CHISEL_PASSWORD https://chisel-dev-tibi.staging.testversion.online 27017:mongo:27017 &
	sleep 3

	$(DOCKER_COMPOSE) exec -T mongo mongodump --archive --gzip --db=$(TIBI_PREFIX)_$(TIBI_NAMESPACE) | mongorestore --archive --gzip --nsFrom='$(TIBI_PREFIX)_$(TIBI_NAMESPACE).*' --nsTo='$(TIBI_PREFIX)_$(TIBI_NAMESPACE).*' --nsInclude='$(TIBI_PREFIX)_$(TIBI_NAMESPACE).*' --drop
	sleep 3
	killall chisel

media-sync-local-to-staging: ## sync attachments from local to staging area
	sudo rsync -v -az --info=progress2 media $(STAGING_PATH)/
