.PHONY: test

img := docker-registry.internal.justin.tv/admin-services/admin-panel:$(GIT_COMMIT)
http-redirect-img := docker-registry.internal.justin.tv/admin-services/admin-panel-http-redirect:$(GIT_COMMIT)

# docker compose for feature tests
feature-dc := APP_IMAGE=$(img) docker-compose \
	-f ./docker-compose.feature-tests.yml

build: clean build-manta build-image test-image push-image build-http-redirect
clean:
	rm -rf .manta
build-manta:
	manta -e AWS_ACCESS_KEY_ID -e AWS_SECRET_ACCESS_KEY -v -f build.json -X ignore-me -E GIT_BRANCH
build-image:
	docker build -t $(img) -f .manta/Dockerfile-admin-panel .manta/

test-image:
	docker run \
		-e AWS_SECRET_ACCESS_KEY \
		-e AWS_ACCESS_KEY_ID \
		-e CODECOV_URL="https://codecov.xarth.tv" \
		-e CODECOV_SLUG="admin-services/admin-panel" \
		-e CODECOV_TOKEN \
		-e JENKINS_URL \
		-e GIT_COMMIT \
		-e GIT_BRANCH \
		-e BUILD_NUMBER \
		-e WORKSPACE \
		-e BUILD_URL \
		-e ghprbPullId \
		--rm $(img) bash -c "bundle exec rubocop --parallel && bundle exec rspec"

define ada
	@which ada > /dev/null || (echo "install ada using option 2 from https://wiki.twitch.com/display/AD/Using+Isengard+from+the+command+line+or+programatically" && exit 1)
	@ada credentials update --once --profile $(1) --account $(2) --role dev 2> /dev/null || ((which mwinit && mwinit --aea && ada credentials update --once --profile $(1) --account $(2) --role dev 2> /dev/null) || (echo "===> ada failed: run mwinit <==" && exit 1))
endef

update_dev_creds:
	$(call ada, twitch-admin-panel-dev/dev, 219087926005)

# teleport remote proxy for local development
localdev-proxy:
	TC="twitch-admin-panel-staging-us-west-2" ssh -N \
		-D 127.0.0.1:1080 \
		-L 127.0.0.1:1636:ldap-usw2.internal.justin.tv:636 \
		jumpbox

quality:
	bundle exec rubocop --auto-correct --extra-details

test:
	bundle exec rspec

push-image:
	docker push $(img)

deploy: deploy-image

deploy-image:
	bash ./scripts/deploy.sh

await-deploy:
	bash ./scripts/await_deploy.sh

build-http-redirect:
	docker build -t $(http-redirect-img) ./dockerfiles/http-redirect
	docker push $(http-redirect-img)

twirp-gen:
	retool do protoc --twirp_ruby_out=. --ruby_out=. ./twirp/*/service.proto

test-features:
	APP_IMAGE=$(img) docker-compose -f ./docker-compose.feature-tests.yml pull
	. ./docker-env.sh && $(feature-dc) run -e GIT_BRANCH --rm app bash -c " if [[ X"$$GIT_BRANCH" = "Xdev" ]]; then bundle exec rake audits:session --trace; fi; bundle exec rspec -t type:feature --format progress --format html --out .manta/rspec-features.html"

clean-features: 
	$(feature-dc) down -v
