.PHONY: dev build validate seed_db refresh_auth clone_repos setup_repos wait_ready

dev:
	DOCKER_BUILDKIT=1 docker compose up

build: refresh_auth .env clone_repos setup_repos
	DOCKER_BUILDKIT=1 docker compose up --build

validate:
	docker compose config --quiet

seed_db:
	docker compose up abacus-contract-seeder

refresh_auth:
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 437795906767.dkr.ecr.us-east-1.amazonaws.com
	aws ecr get-login-password --region us-east-1 | docker login --username AWS --password-stdin 086679231553.dkr.ecr.us-east-1.amazonaws.com

wait_ready:
	nc -l 9999 && zsh test-connectivity.sh

.env:
	@if [ -z "$$GITHUB_NPM_TOKEN" ]; then \
	  echo "GITHUB_NPM_TOKEN is not set in the environment"; \
	  exit 1; \
	fi; \
	cp .env.shadow .env
	sed -i.bak "s/^GITHUB_NPM_TOKEN=\".*\"/GITHUB_NPM_TOKEN=\"$$GITHUB_NPM_TOKEN\"/" .env && rm -f .env.bak

#	Clone Abacus repos
clone_repos: ../frontend-royalties ../graphql-router ../graphql-abacus ../ows-abacus-contract ../ows-abacus-account

../frontend-royalties:
	git clone git@github.com:theorchard/frontend-royalties.git ../frontend-royalties

../graphql-router:
	git clone git@github.com:theorchard/graphql-router.git ../graphql-router

../graphql-abacus:
	git clone git@github.com:theorchard/graphql-abacus.git ../graphql-abacus

../ows-abacus-contract:
	git clone git@github.com:theorchard/ows-abacus-contract.git ../ows-abacus-contract

../ows-abacus-account:
	git clone git@github.com:theorchard/ows-abacus-account.git ../ows-abacus-account

# Make necessary local files as described in each repos README
setup_repos: ../frontend-royalties/.env ../graphql-router/config-local.yaml ../graphql-abacus/.env ../ows-abacus-contract/.env ../ows-abacus-contract/.split ../ows-abacus-account/.env ../ows-abacus-account/.split

../frontend-royalties/.env:
	cd ../frontend-royalties && cp .env.shadow .env && yarn

../graphql-router/config-local.yaml:
	cp graphql-router-config.yaml ../graphql-router/config-local.yaml

../graphql-abacus/.env:
	cd ../graphql-abacus && cp .env.shadow .env && yarn

../ows-abacus-contract/.env:
	cd ../ows-abacus-contract && cp .env.shadow .env
../ows-abacus-contract/.split:
	cd ../ows-abacus-contract && cp .split.shadow .split

../ows-abacus-account/.env:
	cd ../ows-abacus-account && cp .env.shadow .env
../ows-abacus-account/.split:
	cd ../ows-abacus-account && cp .split.shadow .split
