UNAME_S := $(shell uname -s)
IPA_DB_USE_DEVPACK ?= False
arcadia_root := $(shell ya dump root)
params := $(wordlist 2,100,$(MAKECMDGOALS))
devpack := IPA_DB_USE_DEVPACK=$(IPA_DB_USE_DEVPACK)
migrations_root := /tmp/ipa-dev-migrations-root
source_root := Y_PYTHON_SOURCE_ROOT=$(arcadia_root)
entry_point := Y_PYTHON_ENTRY_POINT=":main"
pytest_args := -m pytest -p no:warnings -sv


# Code style
check-isort:
	isort --recursive --check-only --diff ipa/

check-flake8:
	flake8 ipa/

check-style: check-flake8 check-isort

sort-imports:
	isort --recursive ipa/

mypy:
	MYPYPATH=$(arcadia_root) mypy -p ipa --show-traceback --disallow-incomplete-defs

cc: sort-imports check-style mypy

# Tests
dev-unit:
	$(devpack) $(source_root) $(entry_point) ./ipa/tests/unit/mail-ipa-ipa-tests-unit $(pytest_args) $(if $(params),$(params),ipa/tests/unit)

dev-functional:
	$(devpack) $(source_root) $(entry_point) ./ipa/tests/functional/mail-ipa-ipa-tests-functional $(pytest_args) $(if $(params),$(params),ipa/tests/functional)

test:
	$(devpack) ya make -tt --test-stdout $(params)

test-unit:
	$(devpack) ya make -tt --test-stdout $(params) ipa/tests/unit

test-functional:
	$(devpack) ya make -tt --test-stdout $(params) ipa/tests/functional

# Development
deps:
	ya make --checkout -j0 ipa && ya make --checkout -j0 ipa/tests

build_bin:
	ya make bin

runserver:
	$(source_root) ./bin/bin runserver

l_runserver:
	./run_local.sh make runserver

wl_runserver:
	./run_local.sh -w make runserver

runworkers:
	$(source_root) ./bin/bin runworkers

l_runworkers:
	./run_local.sh make runworkers

wl_runworkers:
	./run_local.sh -w make runworkers

shell: build_bin
	./bin/bin shell

l_shell:
	./run_local.sh make shell

tunnel:
	ssh -D 4444 dev -N

# Database
start_db:
ifeq ($(UNAME_S),Linux)
	# докер в убунте не может делать bind volume на директорию аркадии, смонтированной через arc
	rm -rf $(migrations_root) && mkdir -p $(migrations_root) && cp -r ./postgre $(migrations_root)
	IPA_MIGRATIONS_ROOT=$(migrations_root) docker-compose up -d ipa_db_migrated
else
	docker-compose up -d ipa_db_migrated
endif

pgcli:
	PGPASSWORD="P@ssw0rd" pgcli -h localhost -p 5200 -U ipa -d ipa_db

# Docker
build_docker: build_bin
	ya package package/package.json --docker --docker-repository mail/ipa --custom-version 0.0.0-$(shell date +"%s")
