#!/bin/sh
# netscrew installer — Linux and macOS.     curl -fsSL https://netscrew.dev/install.sh | sh
#
# What it does (and nothing else): works out your OS and CPU, downloads the matching single
# static binary from netscrew.dev to ~/.local/bin/n (set NETSCREW_DIR to change that), makes it
# executable, and tells you if that folder isn't on your PATH yet. No sudo, no package manager,
# no background service. Remove it with: rm ~/.local/bin/n
set -e

os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$os" in
  linux)  os=linux ;;
  darwin) os=darwin ;;
  *) echo "netscrew: no build for OS '$os' yet (Linux and macOS are supported)"; exit 1 ;;
esac
case "$arch" in
  x86_64|amd64)  arch=amd64 ;;
  aarch64|arm64) arch=arm64 ;;
  *) echo "netscrew: no build for CPU '$arch' yet (amd64 and arm64 are supported)"; exit 1 ;;
esac

dir="${NETSCREW_DIR:-$HOME/.local/bin}"
url="https://netscrew.dev/download/${os}-${arch}"
mkdir -p "$dir"
echo "→ downloading $url"
if command -v curl >/dev/null 2>&1; then
  curl -fsSL "$url" -o "$dir/n"
else
  wget -qO "$dir/n" "$url"
fi
chmod +x "$dir/n"
echo "✓ installed $dir/n"

case ":$PATH:" in
  *":$dir:"*) ;;
  *) echo "  $dir isn't on your PATH yet. Add this to ~/.bashrc or ~/.zshrc:"
     echo "    export PATH=\"$dir:\$PATH\"" ;;
esac
echo
echo "Try:   n            (the front page)"
echo "       n check .    (this machine)      n bench //host/share    (how fast, and why)"
