#!/bin/bash

# Source shared helper scripts
{% include "helpers.sh" %}

# Defaults - these are in the config but left here for information
CLUSTER_NAME="{% if experiment.cluster_name %}{{ experiment.cluster_name }}{% else %}flux{% endif %}"
FORCE_CLUSTER="{% if setup.force_cluster %}true{% else %}false{% endif %}"
ZONE="{% if zone %}{{ zone }}{% else %}us-central1-a{% endif %}"

if [ -z ${ZONE+x} ]; then
    echo "Google Cloud zone template missing as ZONE";
    exit 1
fi

echo "   cluster  : ${CLUSTER_NAME}"
echo "     zone   : ${ZONE}"

is_installed gcloud
is_installed yes || FORCE_CLUSTER="false"

# Check if it already exists
gcloud container clusters list --zone ${ZONE} | grep ${CLUSTER_NAME}
retval=$?
if [[ ${retval} -ne 0 ]]; then
    print_blue "${CLUSTER_NAME} in ${ZONE} does not exist."
    echo
    exit 0
fi

# This command has a confirmation already
if [[ "${FORCE_CLUSTER}" == "true" ]]; then
    yes | gcloud container clusters delete --zone ${ZONE} ${CLUSTER_NAME}
else
    run_echo gcloud container clusters delete --zone ${ZONE} ${CLUSTER_NAME}
fi
