#!/bin/sh
# partyline installer.
#   curl -fsSL https://partyline.sh/install.sh | sh
#
# Downloads the latest signed release for your OS/arch from GitHub Releases,
# verifies its SHA-256 against the published checksums, and installs the binary.
set -eu

REPO="partyline-sh/cli"
BIN="partyline"

# --- detect platform ---
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$arch" in
  x86_64 | amd64) arch="amd64" ;;
  arm64 | aarch64) arch="arm64" ;;
  *) echo "partyline: unsupported architecture: $arch" >&2; exit 1 ;;
esac
case "$os" in
  darwin | linux) ;;
  *) echo "partyline: unsupported OS: $os (macOS and Linux only)" >&2; exit 1 ;;
esac

# --- resolve latest version ---
ver=$(curl -fsSL "https://api.github.com/repos/$REPO/releases/latest" \
  | grep '"tag_name"' | head -1 | sed -E 's/.*"tag_name": *"([^"]+)".*/\1/')
[ -n "${ver:-}" ] || { echo "partyline: could not determine latest release" >&2; exit 1; }
num=${ver#v}

tarball="${BIN}_${num}_${os}_${arch}.tar.gz"
base="https://github.com/$REPO/releases/download/$ver"

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

echo "partyline: downloading $ver ($os/$arch)..."
curl -fsSL "$base/$tarball" -o "$tmp/$tarball"
curl -fsSL "$base/checksums.txt" -o "$tmp/checksums.txt"

# --- verify checksum ---
(
  cd "$tmp"
  if command -v sha256sum >/dev/null 2>&1; then
    grep " $tarball\$" checksums.txt | sha256sum -c - >/dev/null
  else
    want=$(grep " $tarball\$" checksums.txt | awk '{print $1}')
    got=$(shasum -a 256 "$tarball" | awk '{print $1}')
    [ "$want" = "$got" ] || { echo "partyline: checksum mismatch" >&2; exit 1; }
  fi
)
echo "partyline: checksum verified"

tar -xzf "$tmp/$tarball" -C "$tmp"

# --- choose install dir: prefer a writable dir that's already on PATH (so the
# command actually resolves), else fall back to ~/.local/bin with a PATH hint ---
dir=""
for cand in /usr/local/bin /opt/homebrew/bin; do
  case ":$PATH:" in *":$cand:"*) [ -w "$cand" ] && { dir="$cand"; break; } ;; esac
done
if [ -z "$dir" ]; then
  if [ "$(id -u)" = 0 ] || [ -w /usr/local/bin ]; then
    dir=/usr/local/bin
  else
    dir="$HOME/.local/bin"
    mkdir -p "$dir"
  fi
fi

if ! install -m 0755 "$tmp/$BIN" "$dir/$BIN" 2>/dev/null; then
  echo "partyline: writing $dir requires sudo"
  sudo install -m 0755 "$tmp/$BIN" "$dir/$BIN"
fi

# The macOS menu bar companion rides in the same archive (darwin only — linux archives have no
# tray binary, so this whole block is skipped there). Without copying it out, the tray shipped in
# the release and still never reached the machine: the archive had it, the installer didn't take it.
if [ -f "$tmp/ptln-tray" ]; then
  if ! install -m 0755 "$tmp/ptln-tray" "$dir/ptln-tray" 2>/dev/null; then
    sudo install -m 0755 "$tmp/ptln-tray" "$dir/ptln-tray" 2>/dev/null || true
  fi
fi

# macOS: drop the quarantine attr if present (the binaries are Apple-notarized).
if [ "$os" = "darwin" ]; then
  xattr -d com.apple.quarantine "$dir/$BIN" 2>/dev/null || true
  [ -f "$dir/ptln-tray" ] && xattr -d com.apple.quarantine "$dir/ptln-tray" 2>/dev/null || true
fi

# `ptln` short alias → the same binary (this is the command shown everywhere).
if ! ln -sf "$BIN" "$dir/ptln" 2>/dev/null; then
  sudo ln -sf "$BIN" "$dir/ptln" 2>/dev/null || true
fi

# Use the known $ver, not `$BIN version` — the latter prints a second "up to date" line
# (a network check) that gets mangled into this message.
echo "partyline: installed $ver to $dir/$BIN — run it with: ptln (opens your session manager; ptln start to share a shell)"
# Install the man page into a writable manpath so `man ptln` works (best-effort; the binary
# knows how — see `ptln man install`). Silent on failure; `ptln man` always works regardless.
"$dir/$BIN" man install >/dev/null 2>&1 || true
case ":$PATH:" in
  *":$dir:"*) ;;
  *) echo "partyline: ⚠ $dir is not on your PATH — add it:  export PATH=\"$dir:\$PATH\"" ;;
esac
# Warn if a DIFFERENT partyline is ahead on PATH — it'll run instead of this one.
existing=$(command -v partyline 2>/dev/null || true)
if [ -n "$existing" ] && [ "$existing" != "$dir/$BIN" ]; then
  echo "partyline: ⚠ another 'partyline' is ahead on your PATH: $existing"
  echo "           it will run instead of the one just installed — remove it or fix PATH order."
fi
