#!/bin/bash

SHORT="p:,c:,z:,v:,m:,t:,s:,b:,r:,f:,h"
LONG="project:,cluster:,zone:,cluster-version:,machine:,tags:,size:,branch:,repository:,force-cluster,help"
OPTS=$(getopt -a -n create --options $SHORT --longoptions $LONG -- "$@")

eval set -- "$OPTS"

HERE=$( cd -- "$( dirname -- "${BASH_SOURCE[0]}" )" &> /dev/null && pwd )
ROOT=$(dirname $(dirname ${HERE}))

# Source shared helper scripts
. $ROOT/shared/scripts/helpers.sh

# Defaults
CLUSTER_NAME="flux-cluster"
ZONE="us-central1-a"
CLUSTER_VERSION="1.23"
MACHINE_TYPE="n1-standard-1"
FORCE_CLUSTER="false"
SIZE=4
TAGS=flux-cluster
REPOSITORY="flux-framework/flux-operator"
BRANCH="main"

function usage() {
   echo "This is the Google Cloud kubernetes cluster creator."
   echo "usage: cluster-create --project <project> --machine <machine> --cluster <cluster-name> --size <size> --zone <zone>"
}

while :
do
  case "$1" in
    -p | --project)
        GOOGLE_PROJECT=$2
        shift 2
        ;;
    -c | --cluster)
        CLUSTER_NAME=$2
        shift 2
        ;;
    -z | --zone)
        ZONE=$2
        shift 2
        ;;
    -v | --cluster-version)
        CLUSTER_VERSION=$2
        shift 2
        ;;
    -m | --machine)
        MACHINE_TYPE=$2
        shift 2
        ;;
    -t | --tags)
        TAGS=$2
        shift 2
        ;;
    -f | --force-cluster)
        FORCE_CLUSTER="true"
        shift 1
        ;;
    -s | --size)
        SIZE=$2
        shift 2
        ;;
    -b | --branch)
        BRANCH=$2
        shift 2
        ;;
    -r | --repository)
        REPOSITORY=$2
        shift 2
        ;;
    -h | --help)
      usage
      exit 2
      ;;
    --)
      shift;
      break
      ;;
    *)
      echo "Unexpected option: $1"
      ;;
  esac
done

# Required arguments
if [ -z ${GOOGLE_PROJECT+x} ]; then
    echo "Please provide your Google Project with --project";
    exit 1
fi

if [ -z ${ZONE+x} ]; then
    echo "Please provide your Google Cloud zone with --zone";
    exit 1
fi

if [ -z ${MACHINE_TYPE+x} ]; then
    echo "Please provide your Google Cloud machine type with --machine";
    exit 1
fi

print_magenta "   cluster  : ${CLUSTER_NAME}"
print_magenta "    version : ${CLUSTER_VERSION}"
print_magenta "  project   : ${GOOGLE_PROJECT}"
print_magenta "  machine   : ${MACHINE_TYPE}"
print_magenta "     zone   : ${ZONE}"
print_magenta "     tags   : ${TAGS}"
print_magenta "     size   : ${SIZE}"
print_magenta "repository  : ${REPOSITORY}"
print_magenta "     branch : ${BRANCH}"

is_installed kubectl
is_installed gcloud
is_installed wget

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

if [[ "${FORCE_CLUSTER}" != "true" ]]; then
    prompt "Do you want to create this cluster?"
fi

# Create the cluster
run_echo gcloud container clusters create ${CLUSTER_NAME} --project $GOOGLE_PROJECT \
    --zone ${ZONE} --cluster-version ${CLUSTER_VERSION} --machine-type ${MACHINE_TYPE} \
    --num-nodes=${SIZE} --enable-network-policy --tags=${TAGS} --enable-intra-node-visibility

# Get credentials so kubectl will work
run_echo gcloud container clusters get-credentials ${CLUSTER_NAME} --zone ${ZONE} --project $GOOGLE_PROJECT
run_echo kubectl create clusterrolebinding cluster-admin-binding --clusterrole cluster-admin --user $(gcloud config get-value core/account)

# Show nodes
run_echo kubectl get nodes

# Deploy the operator TODO should be variables here
tmpfile=$(mktemp /tmp/flux-operator.XXXXXX.yaml)
rm -rf $tmpfile
run_echo wget -O $tmpfile https://raw.githubusercontent.com/${REPOSITORY}/${BRANCH}/examples/dist/flux-operator.yaml
kubectl apply -f $tmpfile
rm -rf $tmpfile

run_echo kubectl get namespace
run_echo kubectl describe namespace operator-system
