#!/usr/bin/env bash

set -euo pipefail

# Color constants
CYAN='\033[0;36m'
NC='\033[0m' # No Color

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

cd "$repo_root"

# Script inputs
aws_profile="${1:-default}" # This will not work unless infra specific credentials are passed
deploy_type="${2:-dev}"

dest_path=""
if [ $deploy_type = "prod" ]; then
  dest_path=s3://zoox-kraken-config/config
else
  dest_path=s3://zoox-kraken-config/dev-config
fi

config_path="$ZOOX_WORKSPACE_ROOT/kraken/config"

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

printf "${CYAN}[Uploading Kraken config]${NC}\n"
aws s3 cp ${config_path} ${dest_path} --profile ${aws_profile}
