#!/bin/sh
set -eu

case "$(uname -s):$(uname -m)" in
  Linux:x86_64|Linux:amd64)
    os="linux"
    ;;
  Darwin:arm64|Darwin:x86_64)
    os="darwin"
    ;;
  *)
    echo "nteract installer supports Linux x64 and macOS (Apple silicon, Intel)." >&2
    echo "Windows users: download the installer from https://github.com/nteract/nteract/releases" >&2
    exit 1
    ;;
esac

if ! command -v curl >/dev/null 2>&1; then
  echo "curl is required to install nteract." >&2
  exit 1
fi

tmp="$(mktemp -d)"
cleanup() {
  rm -rf "$tmp"
}
trap cleanup EXIT INT TERM

curl --proto '=https' --tlsv1.2 -fsSL \
  "https://github.com/nteract/nteract/releases/download/v2.5.1-stable.202605261941/install-linux-release" \
  -o "$tmp/install-linux-release"

# Self-healing bridge: release-pinned installers older than macOS support
# only know Linux. Until the first release that ships the darwin-aware
# script, fall back to the script on main for Darwin hosts. Delete this
# block once the latest stable release carries Darwin support.
if [ "$os" = "darwin" ] && ! grep -q "Darwin:arm64" "$tmp/install-linux-release"; then
  curl --proto '=https' --tlsv1.2 -fsSL \
    "https://raw.githubusercontent.com/nteract/nteract/main/scripts/install-linux-release" \
    -o "$tmp/install-linux-release"
fi

chmod 0755 "$tmp/install-linux-release"
exec "$tmp/install-linux-release" "$@"
