#!/bin/bash

CLUSTER_PROJECT="gcp-na-emea-presales"
CLUSTER_NAME="djames-tiup-demo"
CLUSTER_ZONE1="us-west1"

echo -n "GCP Project Name (${CLUSTER_PROJECT}): "
read USER_CLUSTER_PROJECT
if [ "x${USER_CLUSTER_PROJECT}" != "x" ]; then
	CLUSTER_PROJECT=${USER_CLUSTER_PROJECT}
fi

echo -n "Getting environments..."
environments=($(gcloud compute networks list --format="(NAME)" | sed -n "/network/s/-network//p"))
echo -e "done\n\n"
echo "Environment(s):"
env_num=0
while [ ${env_num} -lt ${#environments[@]} ]; do
	printf "  %-27s%-27s%-27s\n" \
		"${environments[$((env_num + 1))]}" \
		"${environments[$((env_num + 2))]}" \
		"${environments[$((env_num + 3))]}"
	env_num=$((env_num+3))
done


echo -n "GKE Cluster Name (${CLUSTER_NAME}): "
read USER_CLUSTER_NAME
if [ "x${USER_CLUSTER_NAME}" != "x" ]; then
	CLUSTER_NAME=${USER_CLUSTER_NAME}
fi

echo -e "\n--------------------------------------------------------------------------------"

echo -n "Getting Instance Data..."
VMS=$(gcloud compute instances list --zones=${CLUSTER_ZONE1}-a --filter="name:( ${CLUSTER_NAME} )" --format="csv[terminator=' ',no-heading](name)")

echo "Done"
echo -n "Getting Disk Data..."
DISKS=$(gcloud compute disks list --zones=${CLUSTER_ZONE1}-a --filter="name:( ${CLUSTER_NAME} )" --format="csv[terminator=' ',no-heading](name)")

echo "Done"
echo -n "Getting Network Data..."
NETWORK_FIREWALLS=$(gcloud compute firewall-rules list --project=${CLUSTER_PROJECT} --filter="name:( ${CLUSTER_NAME} )" --format="csv[terminator=' ',no-heading](name)")
NETWORK_SUBNETS=$(gcloud compute networks subnets list --project=${CLUSTER_PROJECT} --regions=${CLUSTER_ZONE1} --filter="name:( ${CLUSTER_NAME} )" --format="csv[terminator=' ',no-heading](name)")
NETWORKS=$(gcloud compute networks list --filter="name:( ${CLUSTER_NAME} )" --format="csv[terminator=' ',no-heading](name)")


echo -e "Done\n--------------------------------------------------------------------------------"
echo -e "\n- Instances to Delete ----------------------------------------------------------"
echo ${VMS}
echo -e "\n- Disks to Delete --------------------------------------------------------------"
echo ${DISKS}
echo -e "\n- Networks to Delete -----------------------------------------------------------"
echo ${NETWORKS}
echo -e "\n- Networks to Delete -----------------------------------------------------------"
echo ${NETWORK_SUBNETS}
echo -e "\n- Network Firewall Policies to Delete ------------------------------------------"
echo ${NETWORK_FIREWALLS}
echo -e "\n--------------------------------------------------------------------------------"
echo -n "Okay to Delete??? (y/N) "

read OK_TO_DEL
DELOK=$(echo $OK_TO_DEL | sed "s/\(.\)..*/\1/" | tr '[a-z]' '[A-Z]')
if [ "${DELOK}" = "N" ] || [ -z ${DELOK} ]; then
	exit 1
fi

echo -n "Deleting compute instances..."
gcloud compute instances delete ${VMS} --zone=${CLUSTER_ZONE1}-a --delete-disks=all --quiet

echo -e "\nDone. Deleting network...\c"
gcloud compute firewall-rules delete ${NETWORK_FIREWALLS} --quiet
gcloud compute networks subnets delete ${NETWORK_SUBNETS} --region=${CLUSTER_ZONE1} --quiet
gcloud compute networks delete ${NETWORKS} --quiet
echo -e "Done\n\n"
