#!/usr/bin/env bash
# TrubeTech Remote -- installs the Action1 agent (default org, renamed from Single-Use Clients 2026-09-25).
# Uses the native package for whichever package manager is present -- no RPM/DEB
# conversion needed, Action1 ships both directly for this org.
set -e

if [ "$(id -u)" -ne 0 ]; then
    echo "Run this with sudo: sudo bash trubetech-remote-setup.sh"
    exit 1
fi

BASE="https://app.na-2.action1.com/agent/28765d14-b6a9-11f1-a866-2f9b1adbc753/Linux/agent(default)"
TMP="$(mktemp -d)"

if command -v apt-get >/dev/null 2>&1; then
    echo "Downloading Action1 agent (deb)..."
    curl -fsSL -o "$TMP/action1_agent.deb" "${BASE}.deb"
    DEBIAN_FRONTEND=noninteractive apt-get install -y "$TMP/action1_agent.deb"
elif command -v dnf >/dev/null 2>&1; then
    echo "Downloading Action1 agent (rpm)..."
    curl -fsSL -o "$TMP/action1_agent.rpm" "${BASE}.rpm"
    dnf -y install "$TMP/action1_agent.rpm"
elif command -v yum >/dev/null 2>&1; then
    echo "Downloading Action1 agent (rpm)..."
    curl -fsSL -o "$TMP/action1_agent.rpm" "${BASE}.rpm"
    yum -y install "$TMP/action1_agent.rpm"
else
    echo "No supported package manager found (apt/dnf/yum). Download manually:"
    echo "  ${BASE}.deb  (Debian/Ubuntu)"
    echo "  ${BASE}.rpm  (RHEL/Fedora/CentOS)"
    exit 1
fi

rm -rf "$TMP"
echo "Action1 agent installed."
