#!/usr/bin/env bash

set -o errexit
set -o nounset
set -o pipefail

ROOT=$(cd $(dirname "${BASH_SOURCE[0]}")/..; pwd -P)

OLD_REF=${1:-"origin/main"}
NEW_REF=${2:-"origin/main"}


function download_manifest() {
    local dir=$1

    if [[ -d $ROOT/$dir ]]; then
        echo "app-delivery has been cloned"
    fi

    git clone https://github.com/tidbcloud/app-delivery.git $ROOT/$dir
}


function fetch_config() {
    local dir=$1
    local ref=$2
    local dist=$3

    cd $ROOT/$dir
    git fetch origin
    git checkout $ref
    cp $ROOT/$dir/eks-provider/envs/stg-us-west-2/config.yaml $ROOT/$dist

}

download_manifest _cache/app-delivery
fetch_config _cache/app-delivery $OLD_REF config/old-config.yaml
fetch_config _cache/app-delivery $NEW_REF config/new-config.yaml

