#!/usr/bin/env python3
# sysctl-aware system profiler for Py-Agent

import glob
import os
import re
import socket
import subprocess
import sys

# Target primary py-agent skills directory with local-ai fallback
cfg_root = os.environ.get("XDG_CONFIG_HOME") or os.path.expanduser("~/.config")
p_dir = os.path.join(cfg_root, "py-agent", "skills", "system")
legacy_dir = os.path.join(cfg_root, "local-ai", "skills", "system")
os.makedirs(p_dir, exist_ok=True)
p_path = os.path.join(p_dir, "mysys.md")

sys.stderr.write("Generating universal system profile...\n")
sys.stderr.flush()

def run(cmd: str) -> str:
    try:
        return subprocess.check_output(cmd, shell=True, text=True, stderr=subprocess.DEVNULL, timeout=5).strip()
    except Exception:
        return "Unknown"

# 1. Hostname detection
try:
    hostname_str = socket.gethostname()
except Exception:
    hostname_str = run("hostname")

# 2. OS and CPU detection (with Max Turbo Frequency)
cpu_threads = os.cpu_count() or "Unknown"
os_name, cpu = "Unknown Linux", "Unknown"
if os.path.exists("/etc/os-release"):
    with open("/etc/os-release") as f:
        m = re.search(r'^PRETTY_NAME="?([^"\n]+)"?', f.read(), re.M)
        if m:
            os_name = m.group(1)

if os.path.exists("/proc/cpuinfo"):
    with open("/proc/cpuinfo") as f:
        m = re.search(r'^model name\s*:\s*(.+)', f.read(), re.M)
        if m:
            cpu = m.group(1).strip()

max_freq = ""
max_freq_path = "/sys/devices/system/cpu/cpu0/cpufreq/cpuinfo_max_freq"
if os.path.exists(max_freq_path):
    try:
        with open(max_freq_path) as f:
            max_freq = f" (Max: {float(f.read().strip()) / 1000000:.2f} GHz)"
    except Exception:
        pass

# 3. Installed Memory & ZRAM Detection
total_ram_str = "Unknown"
zram_status = ""
if os.path.exists("/proc/meminfo"):
    try:
        with open("/proc/meminfo") as f:
            content = f.read()
            m_ram = re.search(r'^MemTotal:\s+(\d+)\s+kB', content, re.M)
            if m_ram:
                ram_gb = float(m_ram.group(1)) / (1024 * 1024)
                total_ram_str = f"{ram_gb:.1f} GB"
            m_swap = re.search(r'^SwapTotal:\s+(\d+)\s+kB', content, re.M)
            if m_swap and int(m_swap.group(1)) > 0:
                swap_gb = float(m_swap.group(1)) / (1024 * 1024)
                if os.path.exists("/dev/zram0") or "zram" in run("cat /proc/swaps"):
                    zram_status = f" (ZRAM: {swap_gb:.1f} GB active)"
                else:
                    zram_status = f" (Swap: {swap_gb:.1f} GB)"
    except Exception:
        pass

# 4. Active Graphics Driver via DRM Sysfs
drivers = []
for path in glob.glob("/sys/class/drm/card*/device/driver"):
    try:
        if os.path.islink(path):
            drv = os.path.basename(os.readlink(path))
            if drv not in drivers:
                drivers.append(drv)
    except Exception:
        pass
driver = ", ".join(drivers) if drivers else "Unknown"

# 5. Detect CachyOS/Arch x86-64 microarchitecture optimization level (v3/v4)
arch_v = ""
if os.path.exists("/etc/pacman.conf"):
    try:
        with open("/etc/pacman.conf") as f:
            conf = f.read()
            if "cachyos-v4" in conf:
                arch_v = " (v4)"
            elif "cachyos-v3" in conf:
                arch_v = " (v3)"
    except Exception:
        pass

# 6. Display / Login Manager
dm = "Unknown"
dm_symlink = "/etc/systemd/system/display-manager.service"
if os.path.islink(dm_symlink):
    try:
        dm = os.path.basename(os.readlink(dm_symlink)).replace(".service", "")
    except Exception:
        pass

# 7. Accurate Bootloader Detection
bootloader = "Unknown"
loader_vars = glob.glob("/sys/firmware/efi/efivars/LoaderInfo-*")
if loader_vars:
    try:
        with open(loader_vars[0], "rb") as f:
            # First 4 bytes are EFI attributes; remainder is UTF-16LE
            raw = f.read()[4:].decode("utf-16le", errors="ignore").strip("\x00 \n\r\t")
            if raw:
                bootloader = raw
    except Exception:
        pass

if bootloader == "Unknown":
    if os.path.exists("/boot/loader/loader.conf"):
        bootloader = "systemd-boot"
    elif os.path.exists("/boot/grub/grub.cfg"):
        bootloader = "GRUB"
    elif os.path.exists("/boot/limine.conf") or os.path.exists("/boot/limine.cfg"):
        bootloader = "Limine"

# 8. Boot Partition Filesystem & Mount Options
boot_fstype = "Unknown"
boot_options = "Unknown"
boot_path = "Unknown"
if os.path.exists("/proc/mounts"):
    try:
        with open("/proc/mounts") as f:
            for line in f:
                parts = line.split()
                if len(parts) >= 4 and parts[1] in ["/boot", "/efi", "/boot/efi"]:
                    if parts[2] in ["vfat", "msdos", "exfat"] or boot_fstype == "Unknown":
                        boot_path = parts[1]
                        boot_fstype = parts[2]
                        boot_options = parts[3]
    except Exception:
        pass

# 9. GPU Hardware Identification
gpu_list = []
if os.path.exists("/usr/bin/lspci"):
    try:
        pci_output = run("lspci | grep -i -E 'vga|3d|display'")
        if pci_output and pci_output != "Unknown":
            for line in pci_output.splitlines():
                parts = line.split(":")
                if len(parts) >= 3:
                    gpu_name = parts[2].strip()
                    if gpu_name not in gpu_list:
                        gpu_list.append(gpu_name)
    except Exception:
        pass
gpu = " + ".join(gpu_list) if gpu_list else "Integrated Graphics"

# 10. WM / Compositor Detection
wm = os.environ.get("XDG_CURRENT_DESKTOP", "")
if not wm or wm.lower() in ["standard window manager", "unknown"]:
    known_wms = ["hyprland", "sway", "i3", "kwin", "gnome-shell", "xfwm4", "openbox"]
    try:
        for pid in os.listdir("/proc"):
            if pid.isdigit():
                try:
                    with open(os.path.join("/proc", pid, "comm"), "r") as f:
                        comm = f.read().strip().lower()
                        if comm in known_wms:
                            wm = comm.capitalize()
                            break
                except Exception:
                    pass
    except Exception:
        pass

if os.environ.get("HYPRLAND_INSTANCE_SIGNATURE"):
    wm = "Hyprland"
elif "hyprland" in wm.lower():
    wm = "Hyprland"

if not wm:
    wm = "Standard Window Manager"

kernel_ver = run("uname -r")
active_qdisc = run("sysctl -n net.core.default_qdisc")
tcp_cc = run("sysctl -n net.ipv4.tcp_congestion_control")

# 11. Dynamic Rule Directives for AI System Prompts
graphics_rules = ""
if wm.lower() == "hyprland" or any(d in driver for d in ["xe", "amdgpu"]):
    graphics_rules = f"The active graphics driver is **{driver}** running the **{wm}** Wayland compositor. Prioritize Wayland-native utilities (such as `hyprctl`, `grimblast`, `wl-copy`) and modern kernel driver configurations over deprecated X11 tools."
else:
    graphics_rules = f"The active graphics driver is **{driver}** running the **{wm}** environment. Prioritize tools and commands native to this graphics and compositor setup."

optimization_rules = ""
if "cachyos" in kernel_ver.lower() or "v4" in arch_v or "v3" in arch_v:
    optimization_rules = f"The host utilizes a performance-optimized kernel (`{kernel_ver}{arch_v}`). Always prioritize compiled-optimized packages, performance-tuned parameters, and modern scheduler suggestions (like Bore, EEVDF, or CachyOS optimizations)."
else:
    optimization_rules = "Prioritize standard package manager commands and standard execution runtimes appropriate for basic system hardware profiles."

fs_rules = ""
if boot_fstype in ["vfat", "msdos"]:
    fs_rules = f"Your boot partition at `{boot_path}` utilizes a **{boot_fstype}** filesystem. NEVER suggest using `chmod` or `chown` on files or directories under `/boot` or `/efi`. If the current live **Boot Mount Options** contain `fmask=0077,dmask=0077`, the system is already secure! Inform the user that log errors related to mount permissions are purely historical entries from earlier in the boot sequence, and no action is required."
else:
    fs_rules = f"Your boot partition at `{boot_path}` utilizes a **{boot_fstype}** filesystem. Apply standard security permission logic native to this layout."

# Formulate Final Structured Profile
profile = f"""# SYSTEM ARCHITECTURE BLUEPRINT
Host Profile context for local AI shell executions.

## Host Environment Context
* **Static Hostname**: `{hostname_str}`
* **Operating System**: `{os_name}`
* **Linux Kernel**: `{kernel_ver}{arch_v}`
* **CPU Hardware**: `{cpu}{max_freq}`
* **CPU Threads (Logical)**: `{cpu_threads}`
* **Total Memory**: `{total_ram_str}{zram_status}`
* **GPU Hardware**: `{gpu}`
* **Active Driver**: `{driver}`
* **Compositor / WM**: `{wm}`
* **Login Manager**: `{dm}`
* **Bootloader**: `{bootloader}`
* **Active Qdisc**: `{active_qdisc}`
* **TCP Congestion Control**: `{tcp_cc}`
* **Boot Filesystem**: `{boot_fstype}`
* **Boot Mount Options**: `{boot_options}`

---

## AI System Rules & Guidelines

### 1. OS & Windowing Alignment
All shell suggestions, scripts, and commands must strictly target **{os_name}** using the **{wm}** interface, **{dm}** display manager, and **{bootloader}** configurations. Avoid legacy alternative parameters unless explicitly requested.

### 2. Pacman & Kernel Optimization
{optimization_rules}

### 3. Wayland & Graphics Integration
{graphics_rules}

### 4. Filesystem & Storage Safety
{fs_rules}

### 5. Non-Destructive Diagnostics
Suggest non-destructive terminal configurations and queries. Prioritize standard `systemd` logging and diagnosis (`journalctl`, `systemctl`) to investigate boot or active runtime issues.
"""

# Save to primary destination
try:
    with open(p_path, "w", encoding="utf-8") as f:
        f.write(profile)
    sys.stderr.write(f"Saved system profile successfully to {p_path}!\n")

    # Optional legacy fallback sync
    if os.path.isdir(legacy_dir):
        try:
            with open(os.path.join(legacy_dir, "mysys.md"), "w", encoding="utf-8") as lf:
                lf.write(profile)
        except Exception:
            pass

    sys.exit(0)
except Exception as e:
    sys.stderr.write(f"Failed to save system profile: {str(e)}\n")
    sys.exit(1)
