#!/bin/bash
# GuruRMM Agent Installer for Scileppi Law Firm
# Usage: curl -fsSL https://rmm.azcomputerguru.com/install/scileppi | sudo bash

set -e

SITE_ID="9571d9ff-2a43-40b8-9691-63ded40c85b8"
ARCH=$(uname -m)

echo "Installing GuruRMM Agent for Scileppi Law Firm..."
echo "Architecture: $ARCH"

# Determine download URL based on architecture
if [ "$ARCH" = "arm64" ]; then
    DOWNLOAD_URL="https://rmm.azcomputerguru.com/downloads/gururmm-agent-macos-arm64-latest"
elif [ "$ARCH" = "x86_64" ]; then
    DOWNLOAD_URL="https://rmm.azcomputerguru.com/downloads/gururmm-agent-macos-amd64-latest"
else
    echo "Error: Unsupported architecture: $ARCH"
    exit 1
fi

# Download agent
echo "Downloading agent..."
curl -fsSL -o /usr/local/bin/gururmm-agent "$DOWNLOAD_URL"
chmod 755 /usr/local/bin/gururmm-agent

# Create config.toml (required by agent)
echo "Creating configuration..."
mkdir -p /etc/gururmm
cat > /etc/gururmm/config.toml <<'CONFIG'
[server]
url = "wss://rmm-api.azcomputerguru.com/ws"
api_key = "will-auto-enroll"

[metrics]
interval_seconds = 60
collect_cpu = true
collect_memory = true
collect_disk = true
collect_network = true
CONFIG

# Create site.plist for site_id
mkdir -p /usr/local/etc/gururmm
cat > /usr/local/etc/gururmm/site.plist <<'PLIST'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>SiteId</key>
    <string>9571d9ff-2a43-40b8-9691-63ded40c85b8</string>
</dict>
</plist>
PLIST

# Create LaunchDaemon
echo "Installing service..."
cat > /Library/LaunchDaemons/com.azcomputerguru.gururmm-agent.plist <<'LAUNCHD'
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.azcomputerguru.gururmm-agent</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/local/bin/gururmm-agent</string>
        <string>run</string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <true/>
    <key>StandardOutPath</key>
    <string>/Library/Logs/GuruRMM/stdout.log</string>
    <key>StandardErrorPath</key>
    <string>/Library/Logs/GuruRMM/stderr.log</string>
</dict>
</plist>
LAUNCHD

# Create log directory
mkdir -p /Library/Logs/GuruRMM

# Stop existing service if running
if launchctl list 2>/dev/null | grep -q com.azcomputerguru.gururmm-agent; then
    echo "Stopping existing service..."
    launchctl unload /Library/LaunchDaemons/com.azcomputerguru.gururmm-agent.plist 2>/dev/null || true
    sleep 2
fi

# Load and start service
echo "Starting service..."
launchctl load /Library/LaunchDaemons/com.azcomputerguru.gururmm-agent.plist
launchctl start com.azcomputerguru.gururmm-agent

sleep 3

# Verify installation
if launchctl list | grep -q com.azcomputerguru.gururmm-agent; then
    echo ""
    echo "✓ GuruRMM Agent installed successfully for Scileppi Law Firm!"
    echo ""
    echo "The agent is now running and will appear in the dashboard shortly."
    echo ""
    echo "Check status: sudo launchctl list | grep gururmm"
    echo "View logs: sudo tail -f /Library/Logs/GuruRMM/agent.log.*"
else
    echo ""
    echo "Warning: Service may not have started properly."
    echo "Check logs: sudo tail -f /Library/Logs/GuruRMM/agent.log.*"
    exit 1
fi
