DOCKER_REGISTRY=docker.senomas.com
SHELL=/bin/bash
COMPOSE_HTTP_TIMEOUT=900
.PHONY: FORCE

init:
	$(call healty, openldap)
	$(call healty, mwconfig)
	$(call healty, mwlog)
	$(call healty, mwapp)
	$(call healty, was)
	$(call exec, was, 'sh /home/was/script/install.sh')

down:
	docker compose down -v --remove-orphans

openldap-init:
	@make -C openldap
	
ace:
	docker compose up -d mwconfig mwlog

	@echo "\033[48;5;202;38;5;15mWaiting mwconfig mwlog to be healthy...\033[0m"
	@until [ $$(docker compose ps -q mwconfig | xargs docker inspect -f '{{.State.Health.Status}}') = "healthy" ] \
		&& [ $$(docker compose ps -q mwlog | xargs docker inspect -f '{{.State.Health.Status}}') = "healthy" ]; do \
		sleep 1; \
	done
	# docker compose logs mwconfig | grep -B 3 -A 2 'SQL0206N'
	docker compose up -d btn-middleware
	docker compose up -d ace_proxy
	# docker compose logs -f --no-log-prefix mwconfig
	docker logs -f btn-middleware

db2:
	docker compose up -d mwconfig mwlog

	@echo "\033[48;5;202;38;5;15mWaiting mwconfig mwlog to be healthy...\033[0m"
	@until [ $$(docker compose ps -q mwconfig | xargs docker inspect -f '{{.State.Health.Status}}') = "healthy" ] \
		&& [ $$(docker compose ps -q mwlog | xargs docker inspect -f '{{.State.Health.Status}}') = "healthy" ]; do \
		sleep 1; \
	done

ace-terminal:
	docker exec -it btn-middleware bash -l

ace-down:
	docker compose down mwconfig --remove-orphans
	docker compose down mwlog --remove-orphans
	docker compose down btn-middleware --remove-orphans
	docker compose down ace_proxy --remove-orphans

	

define healty
	@docker compose up -d $(1)
	@echo -e "\033[48;5;202;38;5;15mWaiting $(1) to be healthy...\033[0m"
	@until [ $$(docker compose ps -q $(1) \
		| xargs docker inspect -f '{{.State.Health.Status}}') = "healthy" ]; do \
		sleep 1; \
	done
endef

define exec
	@docker exec $(1) bash -c $(2)
endef
