#!/bin/env bash
set -euo pipefail

[[ -n ${DEBUG:-} ]] && set -x

# If the NETWORK env var is set to a valid cardano network, pre-defined
# configuration will be used.
if [[ -n ${NETWORK:-} ]]; then

  # If either CARDANO_<CONFIG|TOPOLOGY>_JSON_MERGE env vars are set, iohk-nix
  # pre-defined NETWORK configuration will be used as a starting point and
  # merged with custom configuration provided as json in the environment
  # variable(s).
  if [[ -n ${CARDANO_CONFIG_JSON_MERGE:-} || -n ${CARDANO_TOPOLOGY_JSON_MERGE:-} ]]; then

    CFG="/opt/cardano/config"
    if ! [[ -f $CFG/$NETWORK/config.json && -f $CFG/$NETWORK/topology.json ]]; then
      echo "Network \"$NETWORK\" doesn't appear to have expected base configuration available at:"
      echo "  $CFG/$NETWORK/{config,topology}.json"
      echo
      echo "Please check that the NETWORK environment variable is set to a valid cardano network name."
      exit 1
    fi

    # Do a recursive deep merge of iohk-nix NETWORK config and/or topology with
    # the supplied json merge environment variable(s).
    #
    # In a jq deep merge, arrays are replaced, primitive values in the second
    # object override the first, different types for the same key result in
    # full replacement and null values persist.
    #
    # jq -S sorts output keys alphabetically for deterministic diffs.
    if [[ -n ${CARDANO_CONFIG_JSON_MERGE:-} ]]; then
      jq -S \
        --argjson deepMerge "$CARDANO_CONFIG_JSON_MERGE" \
        '. * $deepMerge' \
        < "$CFG/$NETWORK/config.json" \
        > "$CFG/$NETWORK/config-merged.json"
      export CARDANO_CONFIG="$CFG/$NETWORK/config-merged.json"
    else
      export CARDANO_CONFIG="$CFG/$NETWORK/config.json"
    fi

    if [[ -n ${CARDANO_TOPOLOGY_JSON_MERGE:-} ]]; then
      jq -S \
        --argjson deepMerge "$CARDANO_TOPOLOGY_JSON_MERGE" \
        '. * $deepMerge' \
        < "$CFG/$NETWORK/topology.json" \
        > "$CFG/$NETWORK/topology-merged.json"
      export CARDANO_TOPOLOGY="$CFG/$NETWORK/topology-merged.json"
    else
      export CARDANO_TOPOLOGY="$CFG/$NETWORK/topology.json"
    fi

    if [[ -n ${DEBUG:-} ]]; then
      echo "Cardano config in merge mode is:"
      cat "$CARDANO_CONFIG"
      echo
      echo "Cardano topology in merge mode is:"
      cat "$CARDANO_TOPOLOGY"
      echo
    fi

    # Run cardano-node using iohk-nix base config merged with provided custom
    # config for the requested NETWORK.
    unset NETWORK
    if [[ ${1:-} == "run" ]]; then
      exec /usr/local/bin/run-node "$@"
    else
      exec /usr/local/bin/run-node run "$@"
    fi

  else
    # Run cardano-node using "scripts" mode for the requested NETWORK.
    exec /usr/local/bin/run-network "$@"
  fi

elif [[ ${1:-} == "run" ]]; then
  # Run cardano-node using "custom" mode.
  exec /usr/local/bin/run-node "$@"

elif [[ ${1:-} == "cli" ]]; then
  # Run cardano-cli with the provided entrypoint args
  exec /usr/local/bin/run-client "$@"

else
  echo "Nothing to do!  Available modes of operation are:"
  echo
  echo "Scripts mode:"
  echo "  * Set the NETWORK env var to a valid cardano network, such as mainnet to use default network config."
  echo
  echo "Custom mode:"
  echo "  * Leave the NETWORK env var unset and provide entrypoint args starting with \"run\" and:"
  echo "    * Optionally set environment variables interpreted by /usr/local/bin/run-node."
  echo "    * Optionally include additional cardano-node args to the entrypoint after \"run\"."
  echo
  echo "Merge mode:"
  echo "  * Set the NETWORK env var to a valid cardano network, such as mainnet, and"
  echo "    set CARDANO_CONFIG_JSON_MERGE and/or CARDANO_TOPOLOGY_JSON_MERGE env vars"
  echo "    with valid json to run cardano-node with deep merged base NETWORK and custom config."
  echo "  * The extra environment variables and cardano-node args that can be used in custom mode"
  echo "    are also available in merge mode."
  echo
  echo "CLI mode:"
  echo "  * Leave the NETWORK env var unset and provide entrypoint args"
  echo "    starting with \"cli\" followed by cardano-cli command args."
  echo "  * A docker volume mount to the cardano-node ipc socket will need to be included, example:"
  echo "      -v node-ipc:/ipc"
  exit 1

fi
