#!/bin/bash
# SamiOS CLI - System management tool

set -e

# ── Canonical version source ────────────────────────────────────────────
# VERSION lives in a single file at the repo root (./VERSION) and is
# mirrored into the live system at /etc/samios-version by profiledef.sh.
# Lookup walks up from this script so it works both in dev and on a
# running SamiOS install. Hard fallback is for boot-time / chroot use
# before the file has been installed.
_lookup_version() {
    local d
    d="$(cd "$(dirname "$0")" && pwd)"
    while [ "$d" != "/" ]; do
        if [ -f "$d/VERSION" ]; then
            head -n1 "$d/VERSION"
            return 0
        fi
        d="$(dirname "$d")"
    done
    if [ -r /etc/samios-version ]; then
        head -n1 /etc/samios-version
        return 0
    fi
    echo "0.0.0-unknown"
}
VERSION="$(_lookup_version)"
unset -f _lookup_version

CONFIG_DIR="/etc/samios"
LOG_FILE="/var/log/samios.log"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $*" | tee -a "$LOG_FILE"
}

cmd_help() {
    cat << EOF
SamiOS CLI v${VERSION}

Usage: samios <command> [options]

Commands:
  version          Show version
  status           Show system status
  update           Update system packages
  install <pkg>    Install a package
  remove <pkg>     Remove a package
  services         List services
  fonts            Manage fonts
  help             Show this help

Examples:
  samios version
  samios status
  samios update
  samios install firefox
EOF
}

cmd_version() {
    echo "SamiOS v${VERSION}"
    echo "Kernel: $(uname -r)"
    echo "Architecture: $(uname -m)"
}

cmd_status() {
    echo "=== SamiOS System Status ==="
    echo "Version: ${VERSION}"
    echo "Hostname: $(hostname)"
    echo "Kernel: $(uname -r)"
    echo "Uptime: $(uptime -p)"
    echo
    echo "=== Disk Usage ==="
    df -h / | tail -n 1
    echo
    echo "=== Memory ==="
    free -h | grep "Mem:"
    echo
    echo "=== Network ==="
    ip -br addr show | grep -v "lo"
}

cmd_update() {
    log "Starting system update"
    pacman -Syu --noconfirm
    log "Update complete"
}

cmd_install() {
    if [ -z "$1" ]; then
        echo "Usage: samios install <package>"
        exit 1
    fi
    log "Installing $1"
    pacman -S --noconfirm "$1"
}

cmd_remove() {
    if [ -z "$1" ]; then
        echo "Usage: samios remove <package>"
        exit 1
    fi
    log "Removing $1"
    pacman -R --noconfirm "$1"
}

cmd_services() {
    systemctl list-units --type=service --state=running
}

cmd_fonts() {
    echo "=== Installed Fonts ==="
    fc-list | wc -l
    echo "fonts installed"
    echo
    echo "=== Font Policy ==="
    echo "Excluded: fonts with '7777' in name (personal branding)"
    echo
    
    # Check for forbidden fonts
    if fc-list | grep -i "7777" > /dev/null; then
        echo "WARNING: Found fonts with '7777' in name:"
        fc-list | grep -i "7777"
    else
        echo "✓ No forbidden fonts detected"
    fi
}

# Main command dispatcher
case "${1:-}" in
    version)
        cmd_version
        ;;
    status)
        cmd_status
        ;;
    update)
        cmd_update
        ;;
    install)
        shift
        cmd_install "$@"
        ;;
    remove)
        shift
        cmd_remove "$@"
        ;;
    services)
        cmd_services
        ;;
    fonts)
        cmd_fonts
        ;;
    help|--help|-h|"")
        cmd_help
        ;;
    *)
        echo "Unknown command: $1"
        echo "Run 'samios help' for usage"
        exit 1
        ;;
esac
