#!/bin/bash

set -e

if [[ $(which ytt) == "" ]]; then
  echo "You will need to install ytt to repipe. https://carvel.dev/ytt/"
  exit 1
fi

target="${FLY_TARGET:-galoy}"
team="${FLY_TEAM:-cepler}"

REPO_ROOT_DIR="$(dirname $( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null && pwd ))"

TMPDIR=$(mktemp -d -t repipe.XXXXXX)
trap "rm -rf ${TMPDIR}" INT TERM QUIT EXIT

ytt -f ci/pipeline.yml -f ci/values.yml > ${TMPDIR}/pipeline.yml

echo "Updating pipeline @ ${target} (team: ${team})"

fly -t ${target} set-pipeline --team ${team} -p cepler -c ${TMPDIR}/pipeline.yml
fly -t ${target} unpause-pipeline --team ${team} -p cepler
