#!/bin/sh
# Praesidias: the one-line installer for Mac and Linux, served at https://get.praesidias.com
#
#   curl -fsSL https://get.praesidias.com | sh
#
# Downloads the praesidias program for this computer, checks it against its published SHA-256,
# puts it in ~/.praesidias/bin, and then runs `praesidias connect` in this folder. Nothing else on
# the computer is changed: no PATH edits, no sudo, no Node.js needed.
#
#   PRAESIDIAS_VERSION=1.2.3   install that version instead of the newest
#   PRAESIDIAS_NO_CONNECT=1    install only; do not run connect
#
# Anything after `sh -s --` is passed to connect, for example:
#   curl -fsSL https://get.praesidias.com | sh -s -- --only mcp
set -eu

base="${PRAESIDIAS_RELEASES_URL:-https://get.praesidias.com}"
home_dir="${PRAESIDIAS_HOME:-$HOME/.praesidias}"

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

case "$(uname -s)" in
  Darwin) os=darwin ;;
  Linux) os=linux ;;
  *) fail "this installer is for Mac and Linux. On Windows, in PowerShell: irm https://get.praesidias.com/win | iex" ;;
esac
case "$(uname -m)" in
  arm64 | aarch64) arch=arm64 ;;
  x86_64 | amd64) arch=x64 ;;
  *) fail "there is no praesidias program for this processor ($(uname -m)). With Node.js: npx praesidias connect" ;;
esac

# https only, except a release server on this computer (the installer's own tests).
case "$base" in
  http://127.0.0.1:* | http://localhost:*) proto='=http,https'; wget_https='' ;;
  *) proto='=https'; wget_https='--https-only' ;;
esac
if command -v curl >/dev/null 2>&1; then
  get() { curl -fsSL --proto "$proto" --tlsv1.2 "$1" -o "$2"; }
elif command -v wget >/dev/null 2>&1; then
  get() { wget -q $wget_https -O "$2" "$1"; }
else
  fail "curl or wget is needed to download praesidias"
fi
if command -v sha256sum >/dev/null 2>&1; then
  sum() { sha256sum "$1" | cut -d' ' -f1; }
elif command -v shasum >/dev/null 2>&1; then
  sum() { shasum -a 256 "$1" | cut -d' ' -f1; }
else
  fail "sha256sum or shasum is needed to check the download"
fi

tmp="$(mktemp -d)"
trap 'rm -rf "$tmp"' EXIT INT TERM

version="${PRAESIDIAS_VERSION:-}"
if [ -z "$version" ]; then
  get "$base/latest" "$tmp/latest" || fail "could not find the newest version"
  version="$(tr -d ' \r\n' <"$tmp/latest")"
fi
version="${version#v}"
case "$version" in
  *[!0-9A-Za-z.-]* | '') fail "that is not a version: $version" ;;
esac

asset="praesidias-$os-$arch"
say "Downloading praesidias $version for $os ($arch)…"
get "$base/v$version/$asset" "$tmp/$asset" || fail "could not download $asset"
get "$base/v$version/$asset.sha256" "$tmp/$asset.sha256" || fail "could not download its checksum"
want="$(cut -d' ' -f1 <"$tmp/$asset.sha256" | tr 'A-F' 'a-f')"
got="$(sum "$tmp/$asset")"
[ "$want" = "$got" ] || fail "the download does not match its published checksum, so it was not installed"

mkdir -p "$home_dir/bin"
chmod 700 "$home_dir"
chmod 755 "$tmp/$asset"
mv -f "$tmp/$asset" "$home_dir/bin/praesidias"
say "✓ Installed $home_dir/bin/praesidias ($version, checksum verified)"

if [ "${PRAESIDIAS_NO_CONNECT:-}" = "1" ]; then
  say "Next, in your project folder: $home_dir/bin/praesidias connect $*"
  exit 0
fi
if [ -r /dev/tty ] && [ -w /dev/tty ]; then
  say ""
  exec "$home_dir/bin/praesidias" connect "$@" </dev/tty
fi
say "Next, in your project folder: $home_dir/bin/praesidias connect $*"
