#!/bin/bash


if [ -z "$TERM" ] || [ "$TERM" = "dumb" ]; then
    for term in gnome-terminal konsole xfce4-terminal xterm; do
        if command -v "$term" >/dev/null 2>&1; then
            exec "$term" -e "bash $0"
            exit 0
        fi
    done
    echo "Error: No terminal emulator found"
    exit 1
fi

# Remove autostart file
DESKTOP_FILE="$HOME/.config/autostart/chrome-kiosk.desktop"
if [ -f "$DESKTOP_FILE" ]; then
    rm "$DESKTOP_FILE" 2>/dev/null || {
        echo "Error: Cannot remove $DESKTOP_FILE"
        exit 1
    }
fi

# Restore backup
if ls "$DESKTOP_FILE.bak-"* >/dev/null 2>&1; then
    mv "$DESKTOP_FILE.bak-"* "$DESKTOP_FILE" 2>/dev/null || {
        echo "Error: Cannot restore backup"
        exit 1
    }
fi

# Re-enable power management
xset s on 2>/dev/null
xset s blank 2>/dev/null
[ -n "$GNOME_DESKTOP_SESSION_ID" ] && gsettings set org.gnome.desktop.screensaver idle-activation-enabled true 2>/dev/null
[ -n "$GNOME_DESKTOP_SESSION_ID" ] && gsettings set org.gnome.desktop.session idle-delay 300 2>/dev/null
[ -n "$KDE_FULL_SESSION" ] && qdbus org.freedesktop.ScreenSaver /ScreenSaver SetActive true 2>/dev/null
[ -n "$KDE_FULL_SESSION" ] && qdbus org.freedesktop.ScreenSaver /ScreenSaver SetTimeout 300 2>/dev/null
[ -n "$XFCE_DESKTOP_SESSION" ] && xfconf-query -c xfce4-power-manager -p /xfce4-power-manager/blank-on-ac -s 10 2>/dev/null

echo "Reverted FRAMR Settings. Reboot to restore desktop."