#!/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) )"
configuration_name="${1:-production}"

if [ "$configuration_name" == "production" ]; then
  config_path=ci/test/config.yaml
else
  config_path=ci/test/config-"${configuration_name}".yaml
fi
dest_path=s3://zoox-web/ci-config/"${configuration_name}"


cd "$repo_root"

function display_diff {
  local deployed_config; deployed_config=$(mktemp)

  aws s3 cp "$dest_path" "$deployed_config"

  echo DIFF:

  git diff --no-index "$deployed_config" "$config_path" || true
}

display_diff

read -p "Continue deploying? [y/n] "
echo

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

aws s3 cp "$config_path" "$dest_path"
