#!/usr/bin/env bash

set -euo pipefail

script_directory="$(builtin cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
repo_root="$(builtin cd "$script_directory" && git rev-parse --show-toplevel)"
environment="${1:-production}"

cd "$repo_root"

bazel --nohome_rc test //ci/build_terminator/...

read -p "Deploy to $environment? [y/n] "
echo

if [[ $REPLY != 'y' ]]; then
  exit 1
fi

echo Pushing image to $environment...
bazel --nohome_rc run --define ci_build_terminator_image_tag="$environment" //ci/build_terminator:push_terminator_image

echo Applying kubernetes yaml...
kubectl apply --context dev-aws --namespace ci-cronjobs --filename ci/build_terminator/cron.yaml
