.PHONY: help

help:
	@grep -E -e '^[a-zA-Z0-9%_-]+:.*?## .*$$' -e '^###' $(MAKEFILE_LIST) \
		| awk '\
			BEGIN {FS = ":.*?## "} \
			/:.*?##/ {printf "\033[36m%-30s\033[0m %s\n", $$1, $$2} \
			/###/ {printf "\033[35m%-30s\033[0m\n", $$0} \
			'

build-%: ## Build image for component tractor-%
	docker-compose build --pull --force-rm tractor_$*

up-%: build-% ## Run containers for component tractor-%
	bash tractor_$*/get_secrets.sh
	docker-compose up tractor_$*

up-db: build-db ## Run containers for component tractor-db
	docker-compose up --detach tractor_db

down: ## Stop and remove containers
	docker-compose down -v --remove-orphans

stop: ## Stop containers
	docker-compose stop

DT := $(shell date "+%Y-%m-%d-%H-%M")
BUILDTAG := "registry.yandex.net/mail/tractor/tractor_disk:r$(DT)"
TMP := "/tmp/stage.yml"

release-disk-%: build-disk ## Push local image to remote docker registry and deploy for component tractor-disk (TODO generalize)
	docker tag tractor_disk:local ${BUILDTAG}
	docker push $(BUILDTAG)
	../../ya tool dctl get stage mail_tractor_disk_$* | sed 's/r[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]-[0-9][0-9]/AUTOMATIC/g' > $(TMP)
	sed -i 's#tag: AUTOMATIC#tag: 'r$(DT)'#g' $(TMP)
	../../ya tool dctl put stage $(TMP)

build-and-push-image: build-disk ## Build and push tractor disk image
	docker tag tractor_disk:local ${BUILDTAG}
	docker push $(BUILDTAG)

test-%: ## Run unit tests for compontent tractor-%
	cd tractor_$*/tests; \
	PYTHONPATH="../src:../../pylib" pytest

test-db: up-db ## Run unit tests for compontent tractor-db
	cd tractor_db_tests; \
	PYTHONPATH="../pylib" pytest
