#!/usr/bin/env bash
set -euo pipefail

# This pre-start script updates datum_gateway_config.json with the selected Bitcoin node's RPC configuration.

APP_DIR="${APP_DATA_DIR:-$(readlink -f "$(dirname "${BASH_SOURCE[0]}")/..")}"
APP_SETTINGS_FILE="${APP_DIR}/settings.yml"
DATUM_CONFIG_FILE="${APP_DIR}/data/settings/datum_gateway_config.json"
KNOTS_APP_DIR="${UMBREL_ROOT}/app-data/bitcoin-knots"
DESIRED_OWNER="1000:1000"
migrate_to_knots=false

if [[ -f "${DATUM_CONFIG_FILE}" ]]; then
  echo "Checking DATUM configuration file."

  selected_bitcoin="$(yq e '.dependencies.bitcoin // ""' "${APP_SETTINGS_FILE}" 2>/dev/null || true)"
  legacy_selection="$(yq e '.dependencies["bitcoin-knots"] // ""' "${APP_SETTINGS_FILE}" 2>/dev/null || true)"
  configured_rpc_url="$(jq -r '.bitcoind.rpcurl // ""' "${DATUM_CONFIG_FILE}" 2>/dev/null || true)"

  if [[ -z "${selected_bitcoin}" ]]; then
    if [[ "${legacy_selection}" == "bitcoin-knots" ]]; then
      migrate_to_knots=true
    elif [[ -d "${KNOTS_APP_DIR}" ]] && [[ -n "${configured_rpc_url}" ]] && [[ "${configured_rpc_url}" != "auto-configure-on-install" ]]; then
      # Older installs predate saved dependency selections. A configured DATUM
      # instance with Bitcoin Knots installed is an existing Knots-backed install.
      migrate_to_knots=true
    fi
  fi

  if [[ "${migrate_to_knots}" == true ]]; then
    if [[ -f "${APP_SETTINGS_FILE}" ]]; then
      yq -i '.dependencies.bitcoin = "bitcoin-knots" | del(.dependencies["bitcoin-knots"])' "${APP_SETTINGS_FILE}"
    else
      printf 'dependencies:\n  bitcoin: bitcoin-knots\n' > "${APP_SETTINGS_FILE}"
    fi
    echo "Migrated existing DATUM dependency selection to Bitcoin Knots."

    # Dependencies were sourced before this hook ran, so this invocation may
    # have RPC variables from the new default rather than the saved legacy
    # selection. Keep the existing Knots RPC config for this transitional start.
    echo "Keeping the existing RPC configuration for this start."
  else
    bitcoin_rpc_user="${APP_BITCOIN_RPC_USER:-${APP_BITCOIN_KNOTS_RPC_USER:-}}"
    bitcoin_rpc_pass="${APP_BITCOIN_RPC_PASS:-${APP_BITCOIN_KNOTS_RPC_PASS:-}}"
    bitcoin_node_ip="${APP_BITCOIN_NODE_IP:-${APP_BITCOIN_KNOTS_NODE_IP:-}}"
    bitcoin_rpc_port="${APP_BITCOIN_RPC_PORT:-${APP_BITCOIN_KNOTS_INTERNAL_RPC_PORT:-${APP_BITCOIN_KNOTS_RPC_PORT:-}}}"

    if [[ -z "${bitcoin_rpc_user}" ]] || [[ -z "${bitcoin_rpc_pass}" ]] || [[ -z "${bitcoin_node_ip}" ]] || [[ -z "${bitcoin_rpc_port}" ]]; then
      echo "Missing selected Bitcoin node RPC environment variables. Exiting."
      exit 1
    fi

    echo "Initializing DATUM config with the selected Bitcoin node's RPC settings."

    jq --arg user "${bitcoin_rpc_user}" \
       --arg pass "${bitcoin_rpc_pass}" \
       --arg url "${bitcoin_node_ip}:${bitcoin_rpc_port}" \
       '.bitcoind.rpcuser = $user |
        .bitcoind.rpcpassword = $pass |
        .bitcoind.rpcurl = $url' \
       "$DATUM_CONFIG_FILE" > "${DATUM_CONFIG_FILE}.tmp" && mv "${DATUM_CONFIG_FILE}.tmp" "$DATUM_CONFIG_FILE"

    echo "DATUM configuration updated successfully."
  fi

  if jq -e '.mining.pool_address | select(. == "enter your bitcoin address")' "$DATUM_CONFIG_FILE" > /dev/null; then

    echo "Bitcoin address invalid, resetting."
    jq -e '.mining.pool_address = ""' "$DATUM_CONFIG_FILE" > "$DATUM_CONFIG_FILE".tmp && mv "${DATUM_CONFIG_FILE}.tmp" "$DATUM_CONFIG_FILE"

  fi

  if jq -e '.api.modify_conf' "$DATUM_CONFIG_FILE" > /dev/null; then
    echo "New API already set"
  else
    jq -e '.api.modify_conf = true' "$DATUM_CONFIG_FILE" > "$DATUM_CONFIG_FILE".tmp && mv "${DATUM_CONFIG_FILE}.tmp" "$DATUM_CONFIG_FILE"
  fi

  if jq -e '.api.admin_password | select(. == "umbrel")' "$DATUM_CONFIG_FILE" > /dev/null; then
    jq --arg password "$APP_PASSWORD" -e '.api.admin_password = $password' "$DATUM_CONFIG_FILE" > "$DATUM_CONFIG_FILE".tmp && mv "${DATUM_CONFIG_FILE}.tmp" "$DATUM_CONFIG_FILE"
  elif ! jq -e '.api.admin_password' "$DATUM_CONFIG_FILE" > /dev/null; then
    jq --arg password "$APP_PASSWORD" -e '.api.admin_password = $password' "$DATUM_CONFIG_FILE" > "$DATUM_CONFIG_FILE".tmp && mv "${DATUM_CONFIG_FILE}.tmp" "$DATUM_CONFIG_FILE"
  else
    echo "Admin password already set"
  fi

  chown "${DESIRED_OWNER}" "${DATUM_CONFIG_FILE}"

else
  echo "DATUM configuration file not found. Installation incomplete."
  exit 1
fi
