#!/bin/bash
# Apply the Murexia-Green-Theme-Main icon theme to the running user session.
# Generated by build_icon_debs.sh — run as your normal user (not root).
set -euo pipefail

ICON_THEME="Murexia-Green-Theme-Main"
PKG_NAME="murexia-icons-green"

if [ "$(id -u)" -eq 0 ]; then
    echo "Run this script as your normal user, not root."
    exit 1
fi

if [ ! -d "/usr/share/icons/${ICON_THEME}" ]; then
    echo "Icon theme not found at /usr/share/icons/${ICON_THEME}/"
    echo "Make sure the package is installed:  sudo apt install ${PKG_NAME}"
    exit 1
fi

RED='\033[0;31m'; GREEN='\033[0;32m'; YELLOW='\033[1;33m'
CYAN='\033[0;36m'; BOLD='\033[1m'; NC='\033[0m'
ok()   { echo -e "  ${GREEN}✓${NC}  $*"; }
warn() { echo -e "  ${YELLOW}~${NC}  $*"; }
info() { echo -e "  ${CYAN}→${NC}  $*"; }

echo -e "${BOLD}"
echo "  ╔══════════════════════════════════════════════════════════╗"
echo "  ║         MUREXIA ICON THEME — ACTIVATOR                   ║"
echo "  ╚══════════════════════════════════════════════════════════╝"
echo -e "${NC}"
echo -e "  Icon theme: ${BOLD}${ICON_THEME}${NC}"
echo ""

APPLIED=false
if command -v plasma-apply-icons &>/dev/null; then
    if plasma-apply-icons "$ICON_THEME" &>/dev/null; then
        ok "Icon theme applied → $ICON_THEME"
        APPLIED=true
    fi
fi

if [ "$APPLIED" = false ] && command -v kwriteconfig6 &>/dev/null; then
    kwriteconfig6 --file kdeglobals --group Icons --key Theme "$ICON_THEME"
    ok "Icon theme configured → $ICON_THEME"
    APPLIED=true
fi

# Restart cothon-qml.service if present and running (gracefully)
if systemctl --user is-active cothon-qml.service &>/dev/null; then
    systemctl --user restart cothon-qml.service &>/dev/null \
        && ok "cothon-qml.service restarted" \
        || warn "cothon-qml.service restart failed"
fi

# Refresh KDE caches
if command -v kbuildsycoca6 &>/dev/null; then
    kbuildsycoca6 --noincremental &>/dev/null && ok "KDE service cache rebuilt" || true
fi

# Force GTK / Qt apps to pick up the theme by touching the user icon-theme cache
if command -v gtk-update-icon-cache &>/dev/null; then
    gtk-update-icon-cache -f -t "/usr/share/icons/$ICON_THEME" &>/dev/null || true
fi

echo ""
if [ "$APPLIED" = true ]; then
    echo -e "  ${BOLD}${ICON_THEME}${NC} is now configured."
else
    warn "Could not auto-apply (plasma-apply-icons / kwriteconfig6 not found)."
fi
echo ""

# plasmashell restart — required for panel/taskbar/system-tray icons to refresh.
# Qt caches icon pixmaps in long-running processes, so plasma-apply-icons alone
# is not enough to update what the user actually sees in the panel.
echo -e "  ${YELLOW}Restart plasmashell?${NC} (Required for panel/taskbar icons to update)"
read -p "  Restart now? [y/N] " -n 1 -r; echo ""
if [[ $REPLY =~ ^[Yy]$ ]]; then
    systemctl --user restart plasma-plasmashell.service &>/dev/null \
        && ok "Plasmashell restarted safely via systemd" \
        || warn "Restart failed — log out and back in"
else
    info "Skipped — run 'systemctl --user restart plasma-plasmashell.service' or log out/in"
fi

echo ""
echo -e "  ${CYAN}You can also activate it manually via:${NC}"
echo -e "    System Settings → Appearance → Icons → select '${ICON_THEME}'"
echo ""
