#!/bin/bash

if [ "$1" != "true" ]; then
    LOOK_AND_FEEL="$(kreadconfig5 --key LookAndFeelPackage)"
    if [ "$LOOK_AND_FEEL" == "Sweetified Plasma" ]; then
        if [ "$(kreadconfig5 --key ColorScheme)" == "Sweetified" ] && [ "$(kreadconfig5 --key widgetStyle)" == "kvantum-dark" ] && [ -f ~/.config/autostart/org.kde.latte-dock.desktop ]; then
            exit 0
        fi
    elif [ "$LOOK_AND_FEEL" == "Dr460nized" ]; then
        exit 0
    fi
    exit 1
else
    backup=(kwinrc kcminputrc kdeglobals plasmarc ksplashrc kscreenlockerrc)
    for i in "${backup[@]}"; do
        cp "$HOME/.config/$i" "$HOME/.config/$i.bak"
    done

    # Remove Kwin keys, this is not done by lookandfeeltool
    sed -i 's&Meta=org.kde.lattedock,/Latte,org.kde.LatteDock,activateLauncher&&g' ~/.config/kwinrc || true
    sed -i 's&ColorScheme=Sweetified&ColorScheme=Sweet&g' ~/.local/share/konsole/Garuda.profile || true

    if [ -f ~/.config/fish/config.fish ]; then
        # Apply the new dr460nized fastfetch preset
        sed -i 's/--load-config neofetch/--load-config dr460nized/g' ~/.config/fish/config.fish
    fi

    # Apply the global theme & restart plasmashell gracefully
    if [ -f ~/.config/autostart/org.kde.latte-dock.desktop ]; then
        rm ~/.config/autostart/org.kde.latte-dock.desktop
        killall -SIGINT latte-dock
    fi
    lookandfeeltool --resetLayout -a Dr460nized
    sleep 5
    systemctl --user restart plasma-plasmashell
fi
