#!/bin/bash


build(){
	export ENV=${1:-beta}; # if not specified, will only build and push for the git commit
	make build-backend-image SERVICE=auth &
	make build-backend-image SERVICE=catalog &
	make build-backend-image SERVICE=runner &
	make build-backend-image SERVICE=proxy-admin &
	make build-backend-image SERVICE=proxy-web &
	time wait;

	# for prod drop `beta prefix
	make build-client-image SERVICE=web-client &
	make build-client-image SERVICE=admin-client &
	time wait;

	kubectl rollout restart -n barretthousen-beta deployment runner-beta &
	kubectl rollout restart -n barretthousen-beta deployment catalog-beta &
	kubectl rollout restart -n barretthousen-beta deployment auth-beta &
	kubectl rollout restart -n barretthousen-beta deployment proxy-admin-beta &
	kubectl rollout restart -n barretthousen-beta deployment proxy-web-beta &
	kubectl rollout restart -n barretthousen-beta deployment web-client-beta &
	kubectl rollout restart -n barretthousen-beta deployment admin-client-beta &

	kubectl rollout status -n barretthousen-beta deployment runner-beta -w
	kubectl rollout status -n barretthousen-beta deployment catalog-beta  -w
	kubectl rollout status -n barretthousen-beta deployment auth-beta -w
	kubectl rollout status -n barretthousen-beta deployment proxy-admin-beta -w
	kubectl rollout status -n barretthousen-beta deployment proxy-web-beta -w
	kubectl rollout status -n barretthousen-beta deployment admin-client-beta -w
	kubectl rollout status -n barretthousen-beta deployment web-client-beta -w
}

promote() {
	make promote-backend-prod SERVICE=catalog &
	make promote-backend-prod SERVICE=runner &
	make promote-backend-prod SERVICE=auth &
	make promote-backend-prod SERVICE=proxy-admin &
	make promote-backend-prod SERVICE=proxy-web &

	make promote-client-prod SERVICE=web-client &
	make promote-client-prod SERVICE=admin-client &

	time wait;

	kubectl rollout restart -n barretthousen deployment runner &
	kubectl rollout restart -n barretthousen deployment catalog &
	kubectl rollout restart -n barretthousen deployment auth &
	kubectl rollout restart -n barretthousen deployment proxy-admin &
	kubectl rollout restart -n barretthousen deployment proxy-web &
	kubectl rollout restart -n barretthousen deployment web-client &
	kubectl rollout restart -n barretthousen deployment admin-client &

	kubectl rollout status -n barretthousen deployment runner -w
	kubectl rollout status -n barretthousen deployment catalog  -w
	kubectl rollout status -n barretthousen deployment auth -w
	kubectl rollout status -n barretthousen deployment proxy-admin -w
	kubectl rollout status -n barretthousen deployment proxy-web -w
	kubectl rollout status -n barretthousen deployment admin-client -w
	kubectl rollout status -n barretthousen deployment web-client -w
}

build $@
