#!/usr/bin/env bash

set -euo pipefail

bamboo_proxy_binary_path=bazel-bin/ci/cinder/bamboo_proxy/bamboo_proxy.zip
main_binary_path=bazel-bin/ci/cinder/main.zip

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

function print-colored {
  echo -e "\033[0;35m$@\033[0m"
}

function print-progress {
  print-colored ================================================================================
  print-colored "$@"
  print-colored ================================================================================
}

cd "$repo_root"

print-progress Testing //ci/cinder/...

bazel --nohome_rc test --test_summary terse //ci/cinder/...

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

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

print-progress Building executable python zips...
./ci/cinder/secrets_/run.sh \
  bazel --nohome_rc build --build_python_zip \
  //ci/cinder/bamboo_proxy:bamboo_proxy \
  //ci/cinder:main

print-progress Uploading executable python zips...
aws s3 cp "$repo_root"/"$bamboo_proxy_binary_path" "s3://zoox-web/cinder/$environment/bamboo_proxy.zip"
aws s3 cp "$repo_root"/"$main_binary_path" "s3://zoox-web/cinder/$environment/main.zip"

print-colored Download binaries from https://cinder.zooxlabs.com/$environment

if [[ $environment == production ]]; then
  print-progress Pushing schedule image...
  ./ci/cinder/secrets_/run.sh \
    bazel --nohome_rc run \
    --define cinder_schedule_image_tag="$environment" \
    //ci/cinder:push_schedule_image

  print-progress Applying schedules kubernetes yaml...
  ./ci/cinder/secrets_/run.sh \
    bazel --nohome_rc run //ci/cinder/schedules:deploy
else
  print-colored Skipping schedule docker image and schedules kubernetes yaml \
    due to $environment environment...
fi

if [[ $environment == production ]]; then
  print-progress Pushing schedule_pull_requests image...
  ./ci/cinder/secrets_/run.sh \
    bazel --nohome_rc run \
    --define cinder_schedule_pull_requests_image_tag="$environment" \
    //ci/cinder/schedule_pull_requests:push_schedule_pull_requests_image

  print-progress Deploying schedule_pull_requests kubernetes schedules...
  ./ci/cinder/secrets_/run.sh \
    bazel --nohome_rc run //ci/cinder/schedule_pull_requests:deploy
else
  print-colored Skipping schedule_pull_requests docker image and \
    schedule_pull_requests kubernetes yaml due to $environment environment.
fi

if [[ $environment == production ]]; then
  # Use the current time (down to the second) to uniquely identify this image.
  tag=$(date "+%Y%m%dT%H%M%S")

  print-progress Pushing scheduling image "$tag"...
  ./ci/cinder/secrets_/run.sh \
    bazel --nohome_rc run \
    --define "cinder_scheduling_image_tag=$tag" \
    //ci/cinder/scheduling_service:push_server_image

  print-progress Applying scheduling terraform...
  (
    cd ci/cinder/scheduling_service/terraform
    terraform apply \
      -var "docker_tag=$tag" \
      -auto-approve
  )
else
  print-colored Skipping scheduling service docker image and \
    applying terraform due to $environment environment.
fi
