#!/bin/sh
set -eu
SERVER="https://dash.agentoutpost.app"

os=$(uname -s | tr '[:upper:]' '[:lower:]')
arch=$(uname -m)
case "$arch" in
  x86_64|amd64) arch=amd64 ;;
  arm64|aarch64) arch=arm64 ;;
  *) echo "Outpost does not run on $arch yet." >&2; exit 1 ;;
esac
case "$os" in
  linux|darwin) ;;
  *) echo "Outpost runs on macOS and Linux. This looks like $os." >&2; exit 1 ;;
esac

dir="$HOME/.local/bin"
mkdir -p "$dir"
echo "Downloading the Outpost agent..."
curl -fsSL "$SERVER/download/outpost-host-$os-$arch" -o "$dir/.outpost-host.new"
chmod +x "$dir/.outpost-host.new"
mv "$dir/.outpost-host.new" "$dir/outpost-host"

OUTPOST_INSTALLER=1 "$dir/outpost-host" enrol --control-plane "$SERVER"
OUTPOST_CONTROL_PLANE="$SERVER" "$dir/outpost-host" install >/dev/null

echo
echo "Done. This machine is now in your list at $SERVER"
case ":$PATH:" in
  *":$dir:"*) ;;
  *) echo "To use the outpost-host command yourself, add $dir to your PATH." ;;
esac
