#!/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)
}

# configure_infrastructure_pipeline sets a pipeline for deploying an integration CF environment. The first three parameters, which are required, are the environment's name, the name of its env-specific Lastpass note, and the name of the environment pool it belongs to. The optional fourth parameter is the pipeline configuration to use, which defaults to the base infrastruction pipeline configuration.
configure_infrastructure_pipeline() {
  local name=$1
  local creds=$2
  local potato=$3
  local pipeline=$4

  if [[ -z $pipeline ]]; then
    pipeline=$pipelines_path/infrastructure/pipeline.yml
  fi

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

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

configure_bosh_lite_pool_pipeline() {
  local name=$1

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

  fly -t ci set-pipeline \
    -p $name \
    -c $pipelines_path/cli-dev-bosh-lite/pipeline.yml \
    -v bosh-lite-dns-zone-name=bosh-lite-pool \
    -v bosh-lite-gcp-json-key="$(lpass show Shared-CLI/bosh-service-account-gcp-creds --notes | jq -c . )" \
    -v bosh-lite-pools-github-private-key="$(lpass show Shared-CLI/bosh-lite-pool-github-key --notes)" \
    -v bosh-lite-project-id=cf-cli \
    -v bosh-lite-region=us-west1 \
    -v bosh-lite-storage-bucket=bosh-lites \
    -v bosh-lite-system-domain-suffix=lite.cli.fun \
    -v git-email=cf-cli-eng@pivotal.io \
    -v git-username=cf-cli-eng \
    -v env-pool-repo=git@github.com:cloudfoundry/cli-pools.git \
    -v slack-webhook-url="$(lpass show Shared-CLI/Concourse/Concourse\ Credentials --notes | grep -F slack-webhook-url: | cut -d '"' -f2)"
}

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 cli $pipelines_path/cli/pipeline.yml

configure_infrastructure_pipeline inf-beque beque-pipeline baked-potato
configure_infrastructure_pipeline inf-hardknox hardknox-pipeline baked-potato
configure_infrastructure_pipeline inf-lovingcup lovingcup-pipeline baked-potato
configure_infrastructure_pipeline inf-tartine tartine-pipeline baked-potato

# Arsicault has additional configuration to alert the PM before it rolls.
arsicault_pipeline="bosh int $pipelines_path/infrastructure/pipeline.yml -o $pipelines_path/infrastructure/add-pm-alert-and-wait.yml"
configure_infrastructure_pipeline inf-arsicault arsicault-pipeline deep-fried-potato <(eval $arsicault_pipeline)

# Lola (legacy pipeline) uses a different base pipeline configuration than the other infrastructures.
lola_pipeline=$pipelines_path/infrastructure/lola-pipeline.yml
configure_infrastructure_pipeline inf-lola lola-pipeline kloesse $lola_pipeline

configure_bosh_lite_pool_pipeline bosh-lites
