#!/usr/bin/env bash
set -euo pipefail

AGENT_BASE_URL="${AGENT_BASE_URL:-https://robot.package}"
ARCH="${ARCH:-$(dpkg --print-architecture)}"
AGENT_URL="${AGENT_URL:-${AGENT_BASE_URL%/}/packages/agent/latest.deb?arch=${ARCH}}"
TMP_DEB="/tmp/local-installer-agent.deb"

echo "Downloading Local Installer Agent..."
curl -fL "$AGENT_URL" -o "$TMP_DEB"

echo "Installing Local Installer Agent..."
apt install -y "$TMP_DEB"

echo "Starting Local Installer Agent..."
systemctl enable local-installer-agent
systemctl restart local-installer-agent

echo "Checking Agent..."
curl -fsSL http://127.0.0.1:5010/health

echo ""
echo "Local Installer Agent installed successfully."
