# PlasmaZones pacman install script
# SPDX-FileCopyrightText: 2026 fuddlesworth
# SPDX-License-Identifier: GPL-3.0-or-later

# Helper: refresh the icon / desktop / mime / sycoca caches if the
# relevant tool is on PATH. Errors are swallowed so a minimal chroot
# without the helper still finishes the install — a kbuildsycoca.hook
# alacarte handles the canonical sycoca refresh on KDE installs.
_pz_refresh_caches() {
    if [[ -x /usr/bin/gtk-update-icon-cache ]]; then
        /usr/bin/gtk-update-icon-cache -qtf /usr/share/icons/hicolor 2>/dev/null || true
    fi
    if [[ -x /usr/bin/update-desktop-database ]]; then
        /usr/bin/update-desktop-database -q /usr/share/applications 2>/dev/null || true
    fi
    if [[ -x /usr/bin/update-mime-database ]]; then
        /usr/bin/update-mime-database /usr/share/mime 2>/dev/null || true
    fi
    if [[ -x /usr/bin/kbuildsycoca6 ]]; then
        /usr/bin/kbuildsycoca6 --noincremental 2>/dev/null || true
    fi
}

post_install() {
    _pz_refresh_caches
    echo ""
    echo "══════════════════════════════════════════════════════════════════"
    echo "  PlasmaZones installed successfully!"
    echo "══════════════════════════════════════════════════════════════════"
    echo ""
    echo "  To enable the daemon:"
    echo "      systemctl --user enable --now plasmazones.service"
    echo ""
    echo "  The KWin effect is enabled by default, but KWin must be"
    echo "  restarted to load it. Log out and back in, or run:"
    echo "      kwin_wayland --replace &"
    echo ""
    echo "  Settings: System Settings → Window Management → PlasmaZones"
    echo ""
}

post_upgrade() {
    _pz_refresh_caches
    echo ""
    echo "══════════════════════════════════════════════════════════════════"
    echo "  PlasmaZones upgraded!"
    echo "══════════════════════════════════════════════════════════════════"
    echo ""
    echo "  If the daemon was running, restart it:"
    echo "      systemctl --user restart plasmazones.service"
    echo ""
    echo "  Note: KWin must be restarted to pick up effect plugin changes."
    echo "  Log out and back in, or run: kwin_wayland --replace &"
    echo ""
}

post_remove() {
    # The service unit went away with the package, but if the user had
    # enabled it the broken symlink in ~/.config/systemd/user/ lingers
    # until manually cleaned. Disable the unit globally so systemd
    # garbage-collects the symlinks. Safe to run when the unit is
    # already absent.
    if command -v systemctl >/dev/null 2>&1; then
        systemctl --global disable plasmazones.service 2>/dev/null || true
    fi
    _pz_refresh_caches
    echo ""
    echo "  PlasmaZones removed. The systemd unit symlink has been"
    echo "  disabled; per-user state under ~/.config/plasmazones/ and"
    echo "  ~/.local/share/plasmazones/ is intentionally left in place."
    echo ""
}
