#!/bin/sh
# Installs the rapadura CLI. Usage: curl -fsSL https://agents.rapadura.net/install.sh | sh
set -eu
BASE="${RAPADURA_GATEWAY:-https://agents.rapadura.net}"
os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$arch" in
  x86_64|amd64) arch=amd64 ;;
  arm64|aarch64) arch=arm64 ;;
  *) echo "unsupported architecture: $arch" >&2; exit 1 ;;
esac
case "$os" in
  linux|darwin) ;;
  mingw*|msys*|cygwin*) os=windows ;;
  *) echo "unsupported OS: $os" >&2; exit 1 ;;
esac
ext=""; [ "$os" = windows ] && ext=".exe"
url="$BASE/dl/rapadura_${os}_${arch}${ext}"
dest="${RAPADURA_INSTALL_DIR:-$HOME/.local/bin}"
mkdir -p "$dest"
tmp=$(mktemp)
echo "Downloading $url"
curl -fsSL -o "$tmp" "$url"
chmod 0755 "$tmp"
mv "$tmp" "$dest/rapadura$ext"
echo "Installed $dest/rapadura$ext"
case ":$PATH:" in
  *":$dest:"*) ;;
  *) echo "Add $dest to your PATH, e.g.:  export PATH=\"$dest:\$PATH\"" ;;
esac
echo
echo "Next:  rapadura login"
echo "Then:  rapadura publish ./site --site <slug> --create"
echo "Docs:  $BASE/docs/"
