#!/bin/bash
# Ready GLPI Agent installer for Linux.
# Uses the self-contained AppImage installer and the sysadminit.pro settings.
set -euo pipefail

SERVER="${GLPI_SERVER:-https://glpi.sysadminit.pro/front/inventory.php}"
PROXY="${GLPI_PROXY:-http://proxy.gorodperm.ru:3128}"
TAG="${GLPI_TAG:-sysadminit}"
APP="${GLPI_APPIMAGE:-/tmp/glpi-agent-1.21-x86_64.AppImage}"
URL="https://glpi.sysadminit.pro/agent/files/glpi-agent-1.21-x86_64.AppImage"

if [ "$(id -u)" -ne 0 ]; then
  echo "Run as root" >&2
  exit 1
fi

# RED OS uses a different CA bundle path; AppImage/Perl expects the Debian path.
mkdir -p /etc/ssl/certs
if [ ! -e /etc/ssl/certs/ca-certificates.crt ] && [ -e /etc/pki/tls/certs/ca-bundle.crt ]; then
  ln -s /etc/pki/tls/certs/ca-bundle.crt /etc/ssl/certs/ca-certificates.crt
fi

if [ ! -f "$APP" ]; then
  echo "Downloading GLPI Agent AppImage..."
  curl -x "$PROXY" -fsSL -o "$APP" "$URL"
fi
chmod +x "$APP"

"$APP" --install --silent --service --runnow \
  --server="$SERVER" --proxy="$PROXY" --tag="$TAG"

echo "GLPI Agent installed. Inventory has been started."
