#!/usr/bin/env bash
set -euo pipefail
DIR="$(cd "$(dirname "$0")" && pwd)"
SRC="$DIR/percepta-agent"
if [ ! -f "$SRC" ]; then
echo "Agent executable not found: $SRC" >&2
exit 1
fi
chmod +x "$SRC" 2>/dev/null || true
if command -v systemctl >/dev/null 2>&1 && [ "$(id -u)" -eq 0 ]; then
INSTALL_DIR="/opt/percepta-agent"
install -d -m 0755 "$INSTALL_DIR"
install -m 0755 "$SRC" "$INSTALL_DIR/percepta-agent"
for name in server-config.txt ca_cert.pem otk.txt; do
if [ -f "$DIR/$name" ]; then
install -m 0644 "$DIR/$name" "$INSTALL_DIR/$name"
fi
done
cat > /etc/systemd/system/percepta-agent.service << 'SVCEOF'
[Unit]
Description=Percepta SIEM Agent
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
WorkingDirectory=/opt/percepta-agent
ExecStart=/opt/percepta-agent/percepta-agent --service
Restart=always
RestartSec=5s
StandardOutput=journal
StandardError=journal
[Install]
WantedBy=multi-user.target
SVCEOF
systemctl daemon-reload
systemctl enable --now percepta-agent
echo "✅ Percepta agent installed as systemd service."
echo "   Binary: /opt/percepta-agent/percepta-agent"
echo "   Status: systemctl status percepta-agent"
echo "   Logs:   journalctl -u percepta-agent -f"
else
INSTALL_DIR="$HOME/.local/share/percepta-agent"
DESKTOP_DIR="$HOME/.local/share/applications"
install -d -m 0755 "$INSTALL_DIR" "$DESKTOP_DIR"
install -m 0755 "$SRC" "$INSTALL_DIR/percepta-agent"
for name in server-config.txt ca_cert.pem otk.txt; do
if [ -f "$DIR/$name" ]; then
install -m 0644 "$DIR/$name" "$INSTALL_DIR/$name"
fi
done
cat > "$DESKTOP_DIR/percepta-agent.desktop" <<DESKTOPEOF
[Desktop Entry]
Type=Application
Name=Percepta SIEM Agent
Exec=$INSTALL_DIR/percepta-agent
Path=$INSTALL_DIR
Terminal=false
Categories=Utility;Security;
StartupWMClass=Percepta SIEM Agent
DESKTOPEOF
chmod 0644 "$DESKTOP_DIR/percepta-agent.desktop"
if command -v update-desktop-database >/dev/null 2>&1; then
update-desktop-database "$DESKTOP_DIR" >/dev/null 2>&1 || true
fi
if [ -n "${DISPLAY:-}" ] || [ -n "${WAYLAND_DISPLAY:-}" ]; then
nohup "$INSTALL_DIR/percepta-agent" >/dev/null 2>&1 &
echo "✅ Percepta agent GUI started."
else
echo "Installed desktop launcher at $DESKTOP_DIR/percepta-agent.desktop."
fi
echo "User install path: $INSTALL_DIR"
echo "Run this script with sudo if you want a systemd service."
fi
