#!/bin/sh
# Streamlogia agent installer.
#
#   curl -fsSL https://get.streamlogia.com/install.sh | sudo sh -s -- --key ls_src_live_…
#
# Downloads the streamlogia-agent binary for this machine from GitHub Releases,
# verifies its checksum, and runs `streamlogia-agent install`, which writes the
# config, registers the agent with whatever service manager the host runs —
# systemd, OpenRC or launchd — and starts it. Every argument after `--` is
# passed to `install` unchanged (--key, --api, --host, --interval, --logs,
# --no-logs, --no-metrics).
#
# Linux and macOS. Windows has its own installer, install.ps1.
#
# Environment:
#   STREAMLOGIA_AGENT_REPO     owner/repo holding the releases
#                              (default streamlogia/streamlogia-agent)
#   STREAMLOGIA_AGENT_VERSION  tag to install, e.g. v1.2.3 (default: latest)
#   STREAMLOGIA_AGENT_BASE_URL full override of the download base, for a mirror
#
# POSIX sh on purpose: it has to run on a box that has only busybox or dash.

set -eu

REPO="${STREAMLOGIA_AGENT_REPO:-streamlogia/streamlogia-agent}"
VERSION="${STREAMLOGIA_AGENT_VERSION:-latest}"
BIN=/usr/local/bin/streamlogia-agent

say()  { printf '%s\n' "$*"; }
fail() { printf 'streamlogia-agent install: %s\n' "$*" >&2; exit 1; }

[ "$(id -u)" -eq 0 ] || fail "run as root: curl -fsSL https://get.streamlogia.com/install.sh | sudo sh -s -- --key …"

case "$(uname -s)" in
  Linux)  OS=linux ;;
  Darwin) OS=darwin ;;
  MINGW*|MSYS*|CYGWIN*)
    fail "on Windows use install.ps1: iwr -useb https://get.streamlogia.com/install.ps1 | iex" ;;
  *) fail "unsupported operating system: $(uname -s)" ;;
esac

case "$(uname -m)" in
  x86_64|amd64)  ARCH=amd64 ;;
  aarch64|arm64) ARCH=arm64 ;;
  *) fail "unsupported architecture: $(uname -m)" ;;
esac

# The agent detects the service manager itself and says so if there is none;
# checking here only buys a clearer message before anything is downloaded.
if [ "$OS" = "linux" ] &&
   ! [ -d /run/systemd/system ] &&
   ! command -v rc-service >/dev/null 2>&1; then
  say "Note: no systemd or OpenRC found. The binary will be installed and can"
  say "      be run with \`streamlogia-agent run\` under your own supervisor."
fi

# -L matters: GitHub answers a release download with a redirect to its CDN.
if command -v curl >/dev/null 2>&1; then
  fetch() { curl -fsSL "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  fetch() { wget -qO "$2" "$1"; }
else
  fail "curl or wget is required"
fi

# Asset names carry no version, which is what makes /releases/latest/download
# resolve to whatever the newest release is.
FILE="streamlogia-agent-${OS}-${ARCH}"

if [ -n "${STREAMLOGIA_AGENT_BASE_URL:-}" ]; then
  URL="${STREAMLOGIA_AGENT_BASE_URL%/}/${FILE}"
elif [ "$VERSION" = "latest" ]; then
  URL="https://github.com/${REPO}/releases/latest/download/${FILE}"
else
  case "$VERSION" in v*) ;; *) VERSION="v${VERSION}" ;; esac
  URL="https://github.com/${REPO}/releases/download/${VERSION}/${FILE}"
fi

TMP="$(mktemp -d)"
trap 'rm -rf "$TMP"' EXIT

say "Downloading streamlogia-agent (${VERSION}, ${OS}/${ARCH})…"
fetch "$URL" "$TMP/agent" || fail "download failed: $URL"
fetch "${URL}.sha256" "$TMP/agent.sha256" || fail "checksum download failed: ${URL}.sha256"

# The checksum file is "<hex>  <name>", as sha256sum writes it.
WANT="$(awk '{print $1}' "$TMP/agent.sha256")"
if command -v sha256sum >/dev/null 2>&1; then
  GOT="$(sha256sum "$TMP/agent" | awk '{print $1}')"
elif command -v shasum >/dev/null 2>&1; then
  GOT="$(shasum -a 256 "$TMP/agent" | awk '{print $1}')"
else
  fail "no sha256sum or shasum on this host; cannot verify the download"
fi
[ -n "$WANT" ] && [ "$WANT" = "$GOT" ] || fail "checksum mismatch (want ${WANT:-none}, got $GOT); refusing to install"

install -m 0755 "$TMP/agent" "$BIN"
say "Installed $BIN"

exec "$BIN" install "$@"
