#!/usr/bin/env bash
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements.  See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership.  The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License.  You may obtain a copy of the License at
#
#   http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied.  See the License for the
# specific language governing permissions and limitations
# under the License.

WORKDIR=$(dirname "$0")
GPDEMO_LIB=$WORKDIR/lib/gpdemo

README() {
  cat "$GPDEMO_LIB"/README
}

PROBE_CONFIG() {
  source_gpdemo_defaults
  "$GPDEMO_LIB"/probe_config.sh
}

DEMO_CLUSTER() {
  source_gpdemo_defaults
  "$GPDEMO_LIB"/demo_cluster.sh "$@"
}

source_gpdemo_defaults() {
  . "$GPDEMO_LIB"/gpdemo-defaults.sh

  # overwrite the shorter usage defined in gpdemo-defaults.sh
  USAGE() {
    echo ""
    echo " gpdemo {-c | -d | -p | -h | -H | -v} <-K>"
    echo "    : Default behaviour (no argument) is to create demo cluster."
    echo " -c : Check if demo is possible."
    echo " -d : Delete the demo."
    echo " -K : Create cluster without data checksums."
    echo " -p : Probe configuration of an alive cluster."
    echo " -h : Usage, prints this message."
    echo " -H : Detailed usage."
    echo " -v : Show version."
    echo ""
    echo " There are few environment variables (e.g., PORT_BASE, DATADIRS) to"
    echo " further config the demo cluster. See \"gpdemo -H\" for more info."
    echo ""
  }
}

CHECK_FILENAME=("demo_cluster.sh" "probe_config.sh" "gpdemo-defaults.sh" "lalshell" "README" "../gp_bash_version.sh")
for filename in "${CHECK_FILENAME[@]}"; do
  checking="$GPDEMO_LIB"/"$filename"
  if [[ ! -f "$checking" ]]; then
    echo "Missing $checking"
    echo ""
    echo "This may be caused by incomplete installation or packaging error."
    echo "Try to re-install Cloudberry. If it doesn't help, contact support."
    exit 1
  fi
done

. "$WORKDIR"/lib/gp_bash_version.sh

while getopts ":pHv" opt
do
  case $opt in
    p) PROBE_CONFIG; exit 0;;
    H) README; exit 0;;
    v) print_version; exit 0;;
    *) DEMO_CLUSTER "$@"; exit $?;; # fallback to demo_cluster.sh
  esac
done

# default behaviour is fallback to "demo_cluster.sh" (no argument)
DEMO_CLUSTER