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

.DEFAULT_GOAL := help

.PHONY: docker-up docker-up-tibi-dev docker-start docker-start-tibi-dev docker-down docker-ps docker-logs yarn-upgrade fix-permissions mongo-sync-dev-to-local mongo-sync-master-to-local

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}'

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

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


docker-up-admin-dev: ## bring docker compose stack up in background with tibi-dev
	$(DOCKER_COMPOSE) --profile admin-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 admin-dev  --profile tibi-dev --profile tibi --profile chisel down

docker-start: ## 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: ## 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-dev-to-local: ## sync mongo from dev to local
	$(DOCKER_COMPOSE) up mongo -d
	read -s -p "Enter chisel password: " CHISEL_PASSWORD; \
		chisel client --auth coder:$$CHISEL_PASSWORD http://dock8.basehosts.de:10987 27017:mongo:27017 &
	sleep 3
	mongodump --archive --gzip --db=wmbasic_renz_shop_dev | $(DOCKER_COMPOSE) exec -T mongo mongorestore --archive --gzip --nsFrom='wmbasic_renz_shop_dev.*' --nsTo='wmbasic_renz_shop.*' --nsInclude='wmbasic_renz_shop_dev.*' --drop
	sleep 3
	killall chisel

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://dock8.basehosts.de:10987 27017:mongo:27017 &
	sleep 3
	mongodump --archive --gzip --db=wmbasic_renz_shop | $(DOCKER_COMPOSE) exec -T mongo mongorestore --archive --gzip --nsFrom='wmbasic_renz_shop.*' --nsTo='wmbasic_renz_shop.*' --nsInclude='wmbasic_renz_shop.*' --drop
	sleep 3
	killall chisel

media-sync-master-to-local: ## sync media from master to local
	rsync -v -e "ssh -p 22222" -az --info=progress2 --exclude=contact_form renz_shop_media_master@dock1.basehosts.de:/ media/

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

media-sync-local-to-early: ## sync media from local to early
	rsync -v -e "ssh -p 22222" -az --info=progress2 --exclude=contact_form media/ renz_shop_media_early@dock1.basehosts.de:/
