#!/bin/bash

SHORT="p:,c:,r:,v:,m:,t:,s:,b:,e:,f:,o:,h"
LONG="cluster:,region:,cluster-version:,machine:,tags:,size:,branch:,repository:,force-cluster,config:,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 - these are in the config but left here for information
CLUSTER_NAME="flux-cluster"
REGION="us-east-1"
CLUSTER_VERSION="1.23"
MACHINE_TYPE="m5.large"
FORCE_CLUSTER="false"
SIZE=4
TAGS="creator=flux-cloud"
REPOSITORY="flux-framework/flux-operator"
BRANCH="main"

function usage() {
   echo "This is the Amazon EKS (elastic kubernetes service) cluster creator."
   echo "usage: cluster-create --config /path/to/cluster-config.yaml"
}

while :
do
  case "$1" in
    --config)
        CONFIG_FILE=$2
        shift 2
        ;;
    -c | --cluster)
        CLUSTER_NAME=$2
        shift 2
        ;;
    -r | --region)
        REGION=$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
        ;;
    -b | --branch)
        BRANCH=$2
        shift 2
        ;;
    -e | --repository)
        REPOSITORY=$2
        shift 2
        ;;
    -s | --size)
        SIZE=$2
        shift 2
        ;;
    -h | --help)
      usage
      exit 2
      ;;
    --)
      shift;
      break
      ;;
    *)
      echo $@
      echo "Unexpected option: $1"
      ;;
  esac
done

# Required arguments
if [ -z ${REGION+x} ]; then
    echo "Please provide your AWS region with --region";
    exit 1
fi

if [ -z ${CONFIG_FILE+x} ]; then
    echo "Please provide your AWS cluster config file with --config";
    exit 1
fi

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

print_magenta "   cluster  : ${CLUSTER_NAME}"
print_magenta "    version : ${CLUSTER_VERSION}"
print_magenta "  machine   : ${MACHINE_TYPE}"
print_magenta "   region   : ${REGION}"
print_magenta "     tags   : ${TAGS}"
print_magenta "     size   : ${SIZE}"
print_magenta "repository  : ${REPOSITORY}"
print_magenta "     branch : ${BRANCH}"
print_magenta "   ssh-key  : ${SSH_KEY}"

is_installed kubectl
is_installed eksctl
is_installed wget

# Check if it already exists
eksctl get clusters --name ${CLUSTER_NAME} --region ${REGION} --color fabulous
retval=$?
if [[ "${retval}" == "0" ]]; then
    print_blue "${CLUSTER_NAME} in ${REGION} already exists."
    echo
    exit 0
fi

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

run_echo eksctl create cluster -f ${CONFIG_FILE}

# 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
