#!/bin/bash
#
# Copyright IBM Corp All Rights Reserved
#
# SPDX-License-Identifier: Apache-2.0
#
set -o errexit

# todo: better handling for input parameters.  Argbash?
# todo: skip storage volume init if deploying to a remote / cloud cluster (ICP IKS ROKS etc...)
# todo: for logging, set up a stack and allow multi-line status output codes
# todo: user:pass auth for tls and ecert bootstrap admins.  here and in the server-config.yaml
# todo: refactor chaincode install to support other chaincode routines
# todo: allow the user to specify the chaincode name (hardcoded as 'basic') both in install and invoke/query
# todo: track down a nasty bug whereby the CA service endpoints (kube services) will occasionally reject TCP connections after network down/up.  This is patched by introducing a 10s sleep after the deployments are up...

# todo: allow relative paths for input arguments.
cd "$(dirname "$0")"

export CLUSTER_RUNTIME=${TEST_NETWORK_CLUSTER_RUNTIME:-kind}        # or k3s
export CONTAINER_CLI=${CONTAINER_CLI:-docker}                       # or nerdctl for containerd
export CONTAINER_NAMESPACE=${TEST_NETWORK_CONTAINER_NAMESPACE:-""}  # or "--namespace k8s.io" for containerd / nerdctl

export FABRIC_VERSION=${TEST_NETWORK_FABRIC_VERSION:-2.4.3}
export FABRIC_CA_VERSION=${TEST_NETWORK_FABRIC_CA_VERSION:-1.5.2}
export FABRIC_CONTAINER_REGISTRY=${TEST_NETWORK_FABRIC_CONTAINER_REGISTRY:-hyperledger}
export FABRIC_PEER_IMAGE=${TEST_NETWORK_FABRIC_PEER_IMAGE:-${FABRIC_CONTAINER_REGISTRY}/fabric-peer:${FABRIC_VERSION}}
export NETWORK_NAME=${TEST_NETWORK_NAME:-test-network}
export CLUSTER_NAME=${TEST_NETWORK_KIND_CLUSTER_NAME:-kind}
export NS=${TEST_NETWORK_KUBE_NAMESPACE:-${NETWORK_NAME}}
export DOMAIN=${TEST_NETWORK_DOMAIN:-vcap.me}
export CHANNEL_NAME=${TEST_NETWORK_CHANNEL_NAME:-mychannel}
export ORDERER_TIMEOUT=${TEST_NETWORK_ORDERER_TIMEOUT:-10s}         # see https://github.com/hyperledger/fabric/issues/3372
export TEMP_DIR=${PWD}/build
export CHAINCODE_BUILDER=${TEST_NETWORK_CHAINCODE_BUILDER:-ccaas}   # see https://github.com/hyperledgendary/fabric-builder-k8s/blob/main/docs/TEST_NETWORK_K8S.md
export K8S_CHAINCODE_BUILDER_VERSION=${TEST_NETWORK_K8S_CHAINCODE_BUILDER_VERSION:-v0.4.0}

LOG_FILE=${TEST_NETWORK_LOG_FILE:-network.log}
DEBUG_FILE=${TEST_NETWORK_DEBUG_FILE:-network-debug.log}
LOG_ERROR_LINES=${TEST_NETWORK_LOG_ERROR_LINES:-1}
LOCAL_REGISTRY_NAME=${TEST_NETWORK_LOCAL_REGISTRY_NAME:-kind-registry}
LOCAL_REGISTRY_PORT=${TEST_NETWORK_LOCAL_REGISTRY_PORT:-5000}
STAGE_DOCKER_IMAGES=${TEST_NETWORK_STAGE_DOCKER_IMAGES:-false}
NGINX_HTTP_PORT=${TEST_NETWORK_INGRESS_HTTP_PORT:-80}
NGINX_HTTPS_PORT=${TEST_NETWORK_INGRESS_HTTPS_PORT:-443}


# todo: more complicated config, as these bleed into the yaml descriptors (sed? kustomize? helm (no)? tkn? ansible?...) or other script locations
TLSADMIN_AUTH=tlsadmin:tlsadminpw
RCAADMIN_AUTH=rcaadmin:rcaadminpw

function print_help() {
  set +x

  log
  log "--- Fabric Information"
  log "Fabric Version     \t\t: ${FABRIC_VERSION}"
  log "Fabric CA Version    \t: ${FABRIC_CA_VERSION}"
  log "Container Registry   \t: ${FABRIC_CONTAINER_REGISTRY}"
  log "Network name       \t\t: ${NETWORK_NAME}"
  log "Ingress domain     \t\t: ${DOMAIN}"
  log "Channel name       \t\t: ${CHANNEL_NAME}"
  log
  log "--- Cluster Information"
  log "Cluster runtime      \t: ${CLUSTER_RUNTIME}"
  log "Cluster name       \t\t: ${CLUSTER_NAME}"
  log "Cluster namespace    \t: ${NS}"
  log "Fabric Registry      \t: ${FABRIC_CONTAINER_REGISTRY}"
  log "Local Registry     \t\t: ${LOCAL_REGISTRY_NAME}"
  log "Local Registry port  \t: ${LOCAL_REGISTRY_PORT}"
  log "nginx http port      \t: ${NGINX_HTTP_PORT}"
  log "nginx https port     \t: ${NGINX_HTTPS_PORT}"
  log
  log "--- Script Information"
  log "Log file           \t\t: ${LOG_FILE}"
  log "Debug log file     \t\t: ${DEBUG_FILE}"
  log

  echo todo: help output, parse mode, flags, env, etc.
}

. scripts/utils.sh
. scripts/prereqs.sh
. scripts/kind.sh
. scripts/cluster.sh
. scripts/fabric_config.sh
. scripts/fabric_CAs.sh
. scripts/test_network.sh
. scripts/channel.sh
. scripts/chaincode.sh
. scripts/rest_sample.sh
. scripts/application_connection.sh

# check for kind, kubectl, etc.
check_prereqs

# Initialize the logging system - control output to 'network.log' and everything else to 'network-debug.log'
logging_init

## Parse mode
if [[ $# -lt 1 ]] ; then
  print_help
  exit 0
else
  MODE=$1
  shift
fi

if [ "${MODE}" == "kind" ]; then
  log "Creating KIND cluster \"${CLUSTER_NAME}\":"
  kind_init
  log "🏁 - KIND cluster is ready"

elif [ "${MODE}" == "unkind" ]; then
  log "Deleting KIND cluster \"${CLUSTER_NAME}\":"
  kind_unkind
  log "🏁 - KIND Cluster is gone."

elif [[ "${MODE}" == "cluster" || "${MODE}" == "k8s" || "${MODE}" == "kube" ]]; then
  cluster_command_group $@

elif [ "${MODE}" == "up" ]; then
  log "Launching network \"${NETWORK_NAME}\":"
  network_up
  log "🏁 - Network is ready."

elif [ "${MODE}" == "down" ]; then
  log "Shutting down test network  \"${NETWORK_NAME}\":"
  network_down
  log "🏁 - Fabric network is down."

elif [ "${MODE}" == "channel" ]; then
  channel_command_group $@

elif [[ "${MODE}" == "chaincode" || "${MODE}" == "cc" ]]; then
  chaincode_command_group $@

elif [ "${MODE}" == "anchor" ]; then
  update_anchor_peers $@

elif [ "${MODE}" == "rest-easy" ]; then
  log "Launching fabric-rest-sample application:"
  launch_rest_sample
  log "🏁 - Fabric REST sample is ready."

elif [ "${MODE}" == "application" ]; then
  log "Getting application connection information:"
  application_connection
  log "🏁 - Application connection information ready."

else
  print_help
  exit 1
fi
