#!/usr/bin/env bash
set -e

ROOT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")/.." && pwd)"

echo "Applying NebulaOS Desktop files from:"
echo "$ROOT_DIR"

echo "Installing Plasma desktop theme..."
mkdir -p "$HOME/.local/share/plasma/desktoptheme"
rm -rf "$HOME/.local/share/plasma/desktoptheme/Nebula"
cp -r "$ROOT_DIR/plasma/desktoptheme/Nebula" "$HOME/.local/share/plasma/desktoptheme/"

echo "Installing Look and Feel package..."
mkdir -p "$HOME/.local/share/plasma/look-and-feel"
rm -rf "$HOME/.local/share/plasma/look-and-feel/org.nebula.desktop"
cp -r "$ROOT_DIR/plasma/look-and-feel/org.nebula.desktop" "$HOME/.local/share/plasma/look-and-feel/"

echo "Installing Nebula Launcher plasmoid..."
mkdir -p "$HOME/.local/share/plasma/plasmoids"
rm -rf "$HOME/.local/share/plasma/plasmoids/org.nebula.launcher"
cp -r "$ROOT_DIR/plasma/plasmoids/org.nebula.launcher" "$HOME/.local/share/plasma/plasmoids/"

echo "Installing Nebula Dark color scheme..."
mkdir -p "$HOME/.local/share/color-schemes"
cp "$ROOT_DIR/plasma/color-schemes/NebulaDark.colors" "$HOME/.local/share/color-schemes/NebulaDark.colors"

echo "Refreshing KDE package cache..."
if command -v kbuildsycoca6 >/dev/null 2>&1; then
    kbuildsycoca6
else
    echo "kbuildsycoca6 not found; skipping cache refresh."
fi

echo "Restarting Plasma Shell..."
systemctl --user restart plasma-plasmashell.service || {
    echo "systemd restart failed; falling back to kquitapp6/plasmashell."
    kquitapp6 plasmashell || true
    nohup plasmashell >/tmp/nebula-plasmashell.log 2>&1 &
}

echo "NebulaOS Desktop applied."
