#!/bin/sh
# Clawdie Shell — Desktop Enablement Module
# Purpose: Detect display and enable/disable desktop accordingly
# POSIX-compliant (no bash-isms)

set -eu

# Configuration (can be overridden for testing)
RC_CONF="${RC_CONF:-/etc/rc.conf}"
LOG_FILE="${LOG_FILE:-/var/log/clawdie-firstboot.log}"
PROGRESS_FILE="${PROGRESS_FILE:-/var/log/clawdie-firstboot.progress}"

# ============================================================================
# MAIN ENTRY POINT
# ============================================================================

clawdie_shell_desktop_detect() {
  # Detect display hardware and configure desktop accordingly
  # VPS/cloud: no display → headless (sddm disabled)
  # Baremetal: display detected → XFCE desktop (sddm enabled)

  log_msg "[desktop] Detecting display configuration"

  # Check for display controller in PCI devices
  local display_device
  display_device=$(pciconf -lv 2>/dev/null | grep -i "class=0x030000" | head -1 || true)

  if [ -n "$display_device" ]; then
    log_msg "[desktop] Display controller found - enabling XFCE desktop"
    
    # Enable sddm for graphical login
    if command -v sysrc >/dev/null 2>&1; then
      sysrc sddm_enable=YES >/dev/null 2>&1 || true
    else
      # Fallback: direct rc.conf edit
      if ! grep -q "^sddm_enable=" "$RC_CONF" 2>/dev/null; then
        echo 'sddm_enable="YES"' >> "$RC_CONF"
      else
        sed -i.bak 's/^sddm_enable=.*/sddm_enable="YES"/' "$RC_CONF"
        rm -f "$RC_CONF.bak"
      fi
    fi
    
    echo "[DESKTOP] XFCE" >> "$PROGRESS_FILE"
    log_msg "[desktop] Desktop enabled (sddm_enable=YES)"
  else
    log_msg "[desktop] No display controller - headless mode"
    
    # Disable sddm for headless operation
    if command -v sysrc >/dev/null 2>&1; then
      sysrc sddm_enable=NO >/dev/null 2>&1 || true
    else
      # Fallback: direct rc.conf edit
      if ! grep -q "^sddm_enable=" "$RC_CONF" 2>/dev/null; then
        echo 'sddm_enable="NO"' >> "$RC_CONF"
      else
        sed -i.bak 's/^sddm_enable=.*/sddm_enable="NO"/' "$RC_CONF"
        rm -f "$RC_CONF.bak"
      fi
    fi
    
    echo "[DESKTOP] HEADLESS" >> "$PROGRESS_FILE"
    log_msg "[desktop] Desktop disabled (sddm_enable=NO)"
    log_msg "[desktop] wayvnc available for remote graphical access"
  fi
  
  log_msg "[desktop] Desktop configuration complete"
}

# ============================================================================
# VALIDATION
# ============================================================================

clawdie_shell_desktop_validate() {
  # Verify desktop configuration

  if [ ! -f "$RC_CONF" ]; then
    log_msg "[desktop] ERROR: $RC_CONF not found"
    return 1
  fi

  if ! grep -q "^sddm_enable=" "$RC_CONF" 2>/dev/null; then
    log_msg "[desktop] ERROR: sddm_enable not configured"
    return 1
  fi

  return 0
}

# ============================================================================
# LOGGING HELPER
# ============================================================================

log_msg() {
  echo "$(date '+%H:%M:%S') $1" | tee -a "$LOG_FILE" 2>/dev/null || true
}

# Only run if sourced directly (not during test)
if [ "${SHELL_DESKTOP_TEST:-0}" -eq 0 ]; then
  clawdie_shell_desktop_detect
fi
