#!/bin/sh
# meshly one-click installer (macOS / Linux)
#
#   curl -fsSL https://meshly.mysoul.fun/install.sh | sh
#
# Environment overrides:
#   MESHLY_VERSION      pin a release tag (default: latest)
#   MESHLY_INSTALL_DIR  install directory (default: $HOME/.local/bin)
#   MESHLY_BASE_URL     override the release base URL (local testing)
set -eu

REPO="wms-why/meshly-core"
BASE_URL_LATEST="https://github.com/wms-why/meshly-core/releases/latest/download"
BASE_URL_VERSION="https://github.com/wms-why/meshly-core/releases/download"
CHECKSUMS_FILE="checksums.txt"
BINARY_NAME="meshly-core-client"
COMMAND_NAME="meshly"

say() { printf '%s\n' "$*"; }
err() { printf 'meshly-install: %s\n' "$*" >&2; }
die() { err "$*"; exit 1; }

# --- resolve base URL -------------------------------------------------------
if [ -n "${MESHLY_BASE_URL:-}" ]; then
  BASE_URL="${MESHLY_BASE_URL}"
elif [ -n "${MESHLY_VERSION:-}" ]; then
  BASE_URL="${BASE_URL_VERSION}/${MESHLY_VERSION}"
else
  BASE_URL="${BASE_URL_LATEST}"
fi
BASE_URL="${BASE_URL%/}"

# --- detect platform --------------------------------------------------------
OS_RAW="$(uname -s)"
ARCH_RAW="$(uname -m)"

case "${OS_RAW}" in
  Linux) OS="linux" ;;
  Darwin) OS="macos" ;;
  *) die "unsupported OS: ${OS_RAW} (meshly ships Linux and macOS builds)" ;;
esac

case "${ARCH_RAW}" in
  x86_64 | amd64) ARCH="x86_64" ;;
  aarch64 | arm64) ARCH="aarch64" ;;
  *) die "unsupported architecture: ${ARCH_RAW} (expected x86_64 or aarch64)" ;;
esac

ARCHIVE="meshly-core-client-${OS}-${ARCH}.tar.gz"
INSTALL_DIR="${MESHLY_INSTALL_DIR:-${HOME}/.local/bin}"

# --- downloader -------------------------------------------------------------
if command -v curl >/dev/null 2>&1; then
  download() { curl -fsSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  download() { wget -qO "$2" "$1"; }
else
  die "neither curl nor wget found — install one and re-run"
fi

# --- checksum tool ----------------------------------------------------------
if command -v sha256sum >/dev/null 2>&1; then
  sha256_of() { sha256sum "$1" | awk '{print $1}'; }
elif command -v shasum >/dev/null 2>&1; then
  sha256_of() { shasum -a 256 "$1" | awk '{print $1}'; }
else
  die "neither sha256sum nor shasum found — cannot verify the download"
fi

TMP_DIR="$(mktemp -d 2>/dev/null || mktemp -d -t meshly)"
cleanup() { rm -rf "${TMP_DIR}"; }
trap cleanup EXIT INT TERM

say "meshly installer"
say "  platform : ${OS}-${ARCH}"
say "  release  : ${BASE_URL}"
say "  install  : ${INSTALL_DIR}"

download "${BASE_URL}/${ARCHIVE}" "${TMP_DIR}/${ARCHIVE}" ||
  die "download failed: ${BASE_URL}/${ARCHIVE}"
download "${BASE_URL}/${CHECKSUMS_FILE}" "${TMP_DIR}/${CHECKSUMS_FILE}" ||
  die "download failed: ${BASE_URL}/${CHECKSUMS_FILE}"

# --- verify -----------------------------------------------------------------
EXPECTED="$(awk -v f="${ARCHIVE}" '$2 == f || $2 == "*" f { print $1; exit }' "${TMP_DIR}/${CHECKSUMS_FILE}")"
[ -n "${EXPECTED}" ] || die "${CHECKSUMS_FILE} has no entry for ${ARCHIVE}"
ACTUAL="$(sha256_of "${TMP_DIR}/${ARCHIVE}")"
if [ "${EXPECTED}" != "${ACTUAL}" ]; then
  die "checksum mismatch for ${ARCHIVE}
  expected ${EXPECTED}
  actual   ${ACTUAL}"
fi
say "  checksum : ok (${ACTUAL})"

# --- install ----------------------------------------------------------------
tar -xzf "${TMP_DIR}/${ARCHIVE}" -C "${TMP_DIR}" || die "failed to extract ${ARCHIVE}"
[ -f "${TMP_DIR}/${BINARY_NAME}" ] || die "${BINARY_NAME} not found inside ${ARCHIVE}"
chmod +x "${TMP_DIR}/${BINARY_NAME}"

mkdir -p "${INSTALL_DIR}" || die "cannot create ${INSTALL_DIR}"
cp "${TMP_DIR}/${BINARY_NAME}" "${INSTALL_DIR}/${BINARY_NAME}"
chmod +x "${INSTALL_DIR}/${BINARY_NAME}"
ln -sf "${BINARY_NAME}" "${INSTALL_DIR}/${COMMAND_NAME}"

say ""
say "installed ${INSTALL_DIR}/${BINARY_NAME}"
say "linked    ${INSTALL_DIR}/${COMMAND_NAME}"

case ":${PATH}:" in
  *":${INSTALL_DIR}:"*) ;;
  *)
    say ""
    say "${INSTALL_DIR} is not on your PATH. Add it with:"
    say ""
    say "  echo 'export PATH=\"${INSTALL_DIR}:\$PATH\"' >> \"\$HOME/.profile\""
    say "  # then restart your shell, or: export PATH=\"${INSTALL_DIR}:\$PATH\""
    ;;
esac

say ""
say "Next steps:"
say "  meshly id                      # print this host's NodeID"
say "  meshly run -c client.toml      # expose / consume services"
say "  meshly pair show               # QR-code pairing, no account needed"
say ""
say "Docs: https://meshly.mysoul.fun/docs"
