
function gitops_rosa_help() {
  [[ -n "$1" ]] && echo_warning "$1"
  reset_colors
  cat << EOM
Usage:
mas gitops-rosa [options]
Where ${COLOR_YELLOW}specified${TEXT_RESET} each option may also be defined by setting the appropriate environment variable.
When no options are specified on the command line, interactive-mode will be enabled by default.

Options:
  -c, --cluster-id ${COLOR_YELLOW}CLUSTER_ID${TEXT_RESET}                                 Cluster ID
  -v, --ocp-version ${COLOR_YELLOW}OCP_VERSION${TEXT_RESET}                               Version of Rosa OpenShift to use
      --rosa-compute-machine-type ${COLOR_YELLOW}ROSA_COMPUTE_MACHINE_TYPE${TEXT_RESET}   Machine Type to use, optional
      --rosa-compute-nodes ${COLOR_YELLOW}ROSA_COMPUTE_NODES${TEXT_RESET}                 Number of compute nodes to use, optional

Other Commands:
  -h, --help     Show this help message
EOM
  [[ -n "$1" ]] && exit 1 || exit 0
}

function gitops_rosa_noninteractive() {
  while [[ $# -gt 0 ]]
  do
    key="$1"
    shift
    case $key in
      -c|--cluster-id)
        export CLUSTER_ID=$1 && shift
        ;;
      -v|--ocp-version)
        export OCP_VERSION=$1 && shift
        ;;
      --rosa-compute-machine-type)
        export ROSA_COMPUTE_MACHINE_TYPE=$1 && shift
        ;;
      --rosa-compute-nodes)
        export ROSA_COMPUTE_NODES=$1 && shift
        ;;
      # Show help
      -h|--help)
        gitops_rosa_help
        ;;
      # Unknown option
      *)
        echo -e "\n${COLOR_RED}Usage Error: Unsupported flag \"${key}\" ${COLOR_OFF}\n\n"
        gitops_rosa_help "Usage Error: Unsupported flag \"${key}\" "
        exit 1
        ;;
    esac
  done
  [[ -z "$CLUSTER_ID" ]] && gitops_rosa_help "CLUSTER_ID is not set"
}

function gitops_rosa(){
  shift
  if [[ $# -gt 0 ]]; then
    gitops_rosa_noninteractive "$@"
  else
    echo "Not supported yet"
    exit 1
    gitops_rosa_interactive
  fi

  # catch errors
  set -o pipefail
  trap 'echo "[ERROR] Error occurred at $BASH_SOURCE, line $LINENO, exited with $?"; exit 1' ERR

  echo
  reset_colors
  echo_h2 "Review Settings"
  echo $CLUSTER_ID ">>>"
  echo ""
  echo "${TEXT_DIM}"
  echo_h2 "Cluster Details" "    "
  echo_reset_dim "Cluster Id ..................... ${COLOR_MAGENTA}${CLUSTER_ID}"
  echo_reset_dim "OpenShift Version .............. ${COLOR_MAGENTA}${OCP_VERSION}"
  echo_reset_dim "ROSA Token ..................... ${COLOR_MAGENTA}${ROSA_TOKEN:0:8}<snip>"
  echo_reset_dim "ROSA Compute Machine Type ...... ${COLOR_MAGENTA}${ROSA_COMPUTE_MACHINE_TYPE}"
  echo_reset_dim "ROSA Compute nodes ............. ${COLOR_MAGENTA}${ROSA_COMPUTE_NODES}"
  reset_colors

  echo
  echo_h2 "Provision Rosa Cluster"

  if [[ -z $ROSA_TOKEN  ]]; then
    echo_reset_dim "${COLOR_RED} ROSA_TOKEN is required. Export this environment variable before use this function"
    exit 1
  fi

  if [[ -z $ROSA_CONFIG_DIR  ]]; then
    CURRENT_DIR=$PWD
    TEMP_DIR=$CURRENT_DIR/tmp-rosa
    rm -rf $TEMP_DIR
    mkdir -p $TEMP_DIR
    export ROSA_CONFIG_DIR=$TEMP_DIR
  fi
  export CLUSTER_NAME=${CLUSTER_ID}
  export ROLE_NAME=ocp_provision
  export CLUSTER_TYPE=rosa
  ansible-playbook ibm.mas_devops.run_role

}