#!/usr/bin/env bash

set -e

xargs_func () {
  if [[ $(uname) == "Darwin" ]]; then
    xargs -n 1 -P 15 $@
  else
    xargs -n 1 -P 15 -r $@
  fi
}

CF_API=${CF_INT_API:-"api.bosh-lite.com"}
export CF_USERNAME=${CF_INT_USERNAME:-"admin"}
export CF_PASSWORD=${CF_INT_PASSWORD:-"admin"}

export CF_CLI_EXPERIMENTAL=true
export CF_DIAL_TIMEOUT=15

if [[ -z $SKIP_SSL_VALIDATION || $SKIP_SSL_VALIDATION == "true" ]]; then
  cf api $CF_API --skip-ssl-validation
else
  cf api $CF_API
fi

cf auth

# we don't want the pipeline job to fail because there's a high chance of
# failure when running commands in parallel
set +e

cf create-org temp-org
cf target -o temp-org

cf domains | egrep -i ^\(sub.\)?integration- | cut -d " " -f1 | xargs_func cf delete-shared-domain -f

# This is required for older stacks where the delete-security-group API call was not implemented recursively by the CC.
if [[ $1 == "unbind-first" ]]; then
  cf security-groups | grep -i "integration-sec-group" | grep -v \<all\>| grep -i staging | awk '{print $2 " " $3 " " $4}' | uniq - | xargs -n 3 -P 15 -r cf unbind-security-group --lifecycle staging
  cf security-groups | grep -i "integration-sec-group" | grep -v \<all\>| grep -i staging | awk '{print $1 " " $2 " " $3}' | uniq - | xargs -n 3 -P 15 -r cf unbind-security-group --lifecycle staging
  cf security-groups | grep -i "integration-sec-group" | grep -v \<all\>| grep -i running | awk '{print $2 " " $3 " " $4}' | uniq - | xargs -n 3 -P 15 -r cf unbind-security-group --lifecycle running
  cf security-groups | grep -i "integration-sec-group" | grep -v \<all\>| grep -i running | awk '{print $1 " " $2 " " $3}' | uniq - | xargs -n 3 -P 15 -r cf unbind-security-group --lifecycle running
fi
cf security-groups | grep -i "integration-sec-group" | awk '{print $2}' | xargs_func cf delete-security-group -f

cf isolation-segments | grep -i ^integration-isolation-segment | xargs_func cf delete-isolation-segment -f

cf service-brokers | grep -i -e ^integration-service-broker- -e CATS- | awk '{print $1}' | xargs_func cf delete-service-broker -f
cf quotas | grep -i -e ^integration-quota -e CATS- | awk '{print $1}' | xargs_func cf delete-quota -f
cf orgs | grep -i -e ^integration-org -e CATS- | xargs_func cf delete-org -f

cf delete-org -f temp-org

if [[ $(cf version) == *"version 6"* ]]; then
  for stack in $(cf stacks | tail -n +5 | awk '{print $1}'); do
    cf buildpacks | \
      grep -i ^integration-buildpack | \
      grep -i -e \\s${stack} | \
      awk '{print $1}' | \
      xargs_func cf delete-buildpack -f -s $stack

    # Delete stack if stack happens to be integration stack
    if [[ $stack == INTEGRATION-STACK* ]]; then
      echo Deleting $stack
      cf curl -X DELETE /v2/stacks/$(cf stack --guid $stack)
    fi
  done
  cf buildpacks | grep -i -e ^integration-buildpack | awk '{print $1}' | xargs_func cf delete-buildpack -f
else
  for stack in $(cf stacks | tail -n +4 | awk '{print $1}'); do
    cf buildpacks | \
      grep -i integration-buildpack | \
      grep -i -e \\s${stack} | \
      awk '{print $2}' | \
      xargs_func cf delete-buildpack -f -s $stack

    # Delete stack if stack happens to be integration stack
    if [[ $stack == INTEGRATION-STACK* ]]; then
      echo Deleting $stack
      cf curl -X DELETE /v2/stacks/$(cf stack --guid $stack)
    fi
  done
  cf buildpacks | grep -i -e integration-buildpack | awk '{print $2}' | xargs_func cf delete-buildpack -f
fi

CF_USERS=$(cf curl /v2/users | grep total_results | grep -o '[0-9]\+')
USER_PAGES=$(( $CF_USERS / 50 + 1))

for ((i=1; i<=${USER_PAGES}; i++)) ; do
  cf curl "/v2/users?results-per-page=50&page=${i}" | \
  jq -r .resources[].entity.username | \
  grep -i -e ^integration-user -e CATS- | \
  xargs_func cf delete-user -f || echo
done

