#!/bin/bash

export PATH="/var/vcap/packages/cf_cli/bin:$PATH"
export CF=`which cf`

function cf() {
	echo cf "$@"
	output=$($CF "$@" 2>&1)
	result="$?"
	if [ "$result" -ne "0" ]; then
		echo "$output"
		# No exit here - best effort continuation on failure for the delete script
	fi
}

function is_true() {
	equals_ignore_case "$1" "true"
}

function equals_ignore_case() {
	echo "$1" | grep -i "^$2\$" >/dev/null
}

function import_opsmgr_variables() {
	export SCHEME=https
	export ADMIN_USER={{ 'properties.cf.admin_user' | shell_string }}
	export ADMIN_PASSWORD={{ 'properties.cf.admin_password' | shell_string }}
	export DOMAIN={{ 'properties.domain' | shell_string }}
	export APP_DOMAIN={{ 'properties.app_domains[0]' | shell_string }}
	export CF_ORG={{ 'properties.org' | shell_string }}
	export CF_SPACE={{ 'properties.space' | shell_string }}
	export CF_TARGET=$SCHEME://api.${DOMAIN}
	export CF_SKIP_SSL={{ 'properties.ssl.skip_cert_verify' | shell_string }}
}

function prepare_cf_cli {
	export PATH="/var/vcap/packages/cf_cli/bin:$PATH"
	export CF_HOME=`pwd`/home/cf
	mkdir -p $CF_HOME
}

function authenticate() {
	$CF --version
	if is_true "$CF_SKIP_SSL"; then
		cf api $CF_TARGET --skip-ssl-validation
	else
		cf api $CF_TARGET
	fi
	cf auth $ADMIN_USER $ADMIN_PASSWORD
}

function target_org() {
	if [ -z "$CF_ORG" ]; then
		CF_ORG={{ context.name }}-org
	fi
	cf target -o $CF_ORG >/dev/null
}

function target_space() {
	if [ -z "$CF_SPACE" ]; then
		CF_SPACE={{ context.name }}-space
	fi
	cf target -s $CF_SPACE >/dev/null
}

function delete_empty_space() {
	output=`$CF apps | tail -1`
	if [ "$output" == "No apps found" ]; then
		cf delete-space -f $CF_SPACE
	fi
}

function delete_empty_org() {
	output=`$CF spaces | tail -1`
	if [ "$output" == "No spaces found" ]; then
		cf delete-org -f $CF_ORG
	fi
}

function delete_all_versions() {
	$CF apps | grep "^$1-" | while read app rest; do
		cf delete -f $app
	done
}

function delete_service_broker() {
    {% if context.purge_service_brokers %}
    services=`$CF service-access | awk "/broker: $1/{flag=1;next}/broker: /{flag=0}flag" | egrep -v "access *orgs" | grep "."  | awk '{print $1}' | uniq`
    for service in $services; do
      cf purge-service-offering -f "$service"
    done
    {% endif %}
    cf delete-service-broker -f $1
}

import_opsmgr_variables
prepare_cf_cli
authenticate
if target_org; then
	if target_space; then

		{% for package in context.packages | reverse %}
		# Delete package {{ package.name }}
		#
		{% if package.pre_delete %}
		{{ package.pre_delete | render }}
		{% endif %}

		{% if package.delete %}
		{{ package.delete | render }}
		{% else %}

		{% if package.is_broker %}
		delete_service_broker {{ package.name | hyphens }}
		{% endif %}
		{% if package.is_app %}
		delete_all_versions {{ package.name | hyphens }}
		{% endif %}
		{% if package.is_buildpack %}
		cf delete-buildpack -f {{ package.name }}
		{% endif %}

		{% endif %}

		{% if package.post_delete %}
		{{ package.pre_delete | render }}
		{% endif %}

		{% endfor %}
		delete_empty_space
	fi
	delete_empty_org
fi
