#!/usr/bin/env bash

set -e
set -o pipefail

check_installed() {
  if ! command -v $1 > /dev/null 2>&1; then
    printf "$1 must be installed before running this script!"
    exit 1
  fi
}

configure_pipeline() {
  local name=$1
  local pipeline=$2

  printf "configuring the $name pipeline...\n"

  fly -t ci set-pipeline \
    -p $name \
    -c $pipeline \
    -l <(lpass show "Concourse Credentials" --notes) \
    -l <(lpass show "Shared-CLI/Concourse/Concourse Credentials" --notes) \
    -l <(lpass show "VAT Concourse Credentials" --notes)
}

configure_v7_infrastructure_pipeline() {
  local name=$1
  local creds=$2
  local potato=$3

  printf "configuring the $name pipeline...\n"

  fly -t ci set-pipeline \
    -p $name \
    -c $pipelines_path/infrastructure/v7-pipeline.yml \
    -l <(lpass show "Concourse Credentials" --notes) \
    -l <(lpass show "$creds" --notes) \
    -v pool-name=$potato
}


check_installed lpass
check_installed fly

# Make sure we're up to date and that we're logged in.
lpass sync

pipelines_path=$(cd $(dirname $0)/.. && pwd)

configure_pipeline V7 $pipelines_path/cli-v7/pipeline.yml

configure_v7_infrastructure_pipeline inf-delfina delfina-pipeline mashed-potato
configure_v7_infrastructure_pipeline inf-nopa nopa-pipeline mashed-potato
