#!/usr/bin/env bash

# Deploys the pipedream proxy to Artifactory for immediate consumption by CI.
# NOTE: You'll have to follow the Artifactory setup guide first:
# https://confluence.zooxlabs.com/confluence/pages/viewpage.action?pageId=122994069.

set -euo pipefail

if ! hash jfrog > /dev/null 2>&1; then
  echo ERROR: jfrog CLI not found. Install it following these instructions:
  echo https://confluence.zooxlabs.com/confluence/pages/viewpage.action?pageId=122994069
  exit 1
fi

# The user can specify the environment as the first argument which is defaulted
# to production if unspecified.
environment=${1:-production}

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

cd "$repo_root"

tmp_dir=$(mktemp --directory)
diff_dir=$(mktemp --directory)
tar_path=${tmp_dir}/pipedream_proxy.tar.gz
tmp_script=${tmp_dir}__extract__${RANDOM}
final_script=${tmp_dir}/pipedream_proxy
deployed_pipedream_proxy=${tmp_dir}/deployed_pipedream_proxy

function cleanup {
  rm --recursive --force "$tmp_dir" "$diff_dir"
}
trap cleanup EXIT

if [[ "$environment" == 'production' ]]; then
  deployed_url=https://artifactory.zooxlabs.com/artifactory/pipedream-generic/pipedream_proxy
  s3_path=s3://zoox-web/pipedream_proxy/pipedream_proxy
  artifactory_artifact=pipedream-generic/pipedream_proxy
else
  deployed_url="https://artifactory.zooxlabs.com/artifactory/pipedream-generic/pipedream_proxy_$environment"
  s3_path="s3://zoox-web/pipedream_proxy/pipedream_proxy_$environment"
  artifactory_artifact="pipedream-generic/pipedream_proxy_$environment"
fi

# Make the tar.
function build_pipedream_proxy {
  python pipedream/common/archive/build_bazel_archive.py \
    --target=//ci/pipedream:pipedream_proxy \
    --archive-dest="${tar_path}" \
    --compress

  printf "#!/bin/bash
set -euo pipefail
PAYLOAD_LINE=\`awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' \$0\`
tail --lines=+\$PAYLOAD_LINE \$0 | tar --extract --gzip
./pipedream_proxy \"\$@\"
exit 0
__PAYLOAD_BELOW__\n" > "${tmp_script}"

  cat "$tmp_script" "$tar_path" > "$final_script"
  chmod +x "$final_script"

  echo Created $final_script
}

function extract_proxy {
  path="$1"

  local payload_line; payload_line=$(awk '/^__PAYLOAD_BELOW__/ {print NR + 1; exit 0; }' "$path")
  tail "--lines=+$payload_line" "$path" | tar --extract --gzip --directory "$diff_dir"
}

function display_diff {
  (
    cd "$diff_dir"

    # Download the deployed version.
    if ! curl --fail --show-error --retry 5 "$deployed_url" > "$deployed_pipedream_proxy"; then
      echo Deployed version of $environment not found!
      return 0
    fi
    extract_proxy "$deployed_pipedream_proxy"

    # Initialize a git repo for diffing.
    git init
    git add .
    git commit --message 'Upstream'

    # Delete all files.
    git ls-files | xargs rm

    extract_proxy "$final_script"

    git add .

    echo DIFF:

    git diff --cached || true
    git diff --cached --stat || true
  )
}

build_pipedream_proxy
display_diff

echo Git directory for further diffing: $diff_dir
read -p "Continue deploying to $environment? [y/n] "
echo

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

echo Deploying to artifactory...
jfrog rt upload "$final_script" "$artifactory_artifact"

echo Deploying to ZWeb...
aws s3 cp "$final_script" "$s3_path"
