#!/bin/bash
# GuruRMM Agent Installer for macOS
# Usage: curl -fsSL https://rmm.azcomputerguru.com/downloads/install-mac.sh | sudo bash -s -- --site-id SITE_ID

set -e

SITE_ID=""
ARCH=$(uname -m)

# Parse arguments
while [[ $# -gt 0 ]]; do
    case $1 in
        --site-id)
            SITE_ID="$2"
            shift 2
            ;;
        *)
            echo "Unknown option: $1"
            exit 1
            ;;
    esac
done

if [ -z "$SITE_ID" ]; then
    echo "Error: --site-id required"
    echo "Usage: curl -fsSL https://rmm.azcomputerguru.com/downloads/install-mac.sh | sudo bash -s -- --site-id SITE_ID"
    exit 1
fi

echo "Installing GuruRMM Agent..."
echo "Site ID: $SITE_ID"
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 directory and site.plist
echo "Creating configuration..."
mkdir -p /usr/local/etc/gururmm
cat > /usr/local/etc/gururmm/site.plist <<EOF
<?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>$SITE_ID</string>
</dict>
</plist>
EOF

# Create LaunchDaemon
echo "Installing service..."
cat > /Library/LaunchDaemons/com.azcomputerguru.gururmm-agent.plist <<EOF
<?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>
EOF

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

# Stop existing service if running
if launchctl list | 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

echo ""
echo "✓ GuruRMM Agent installed successfully!"
echo ""
echo "Check status: sudo launchctl list | grep gururmm"
echo "View logs: sudo tail -f /Library/Logs/GuruRMM/agent.log.*"
