if [ "$GX_ACCEPT_USE_IN_SESSION" = "true" ] ; then

BACKUP_FILE="/tmp/xfce-shortcuts-backup.xml"

# external timeout that will only start after x activity
#/usr/bin/groovix-accept-use-x-activity-timeout&

# Backup current shortcuts
echo "Backing up shortcuts..."
cp ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml "$BACKUP_FILE"

# Function to restore shortcuts
restore_shortcuts() {
    echo "Restoring shortcuts..."
    cp "$BACKUP_FILE" ~/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-keyboard-shortcuts.xml
    # Force xfconfd to reload the config
    killall -HUP xfconfd 2>/dev/null
    sleep 1
    # Restart xfsettingsd to pick up changes
    killall xfsettingsd 2>/dev/null
    xfsettingsd &
    rm -f "$BACKUP_FILE"
    echo "Shortcuts restored"
}

# Trap to ensure restoration on exit
trap restore_shortcuts EXIT

# Disable all window manager shortcuts
echo "Disabling shortcuts..."
for prop in $(xfconf-query -c xfce4-keyboard-shortcuts -p /xfwm4/custom -l 2>/dev/null); do
    xfconf-query -c xfce4-keyboard-shortcuts -p "$prop" -r 2>/dev/null
done

# Disable custom command shortcuts too
for prop in $(xfconf-query -c xfce4-keyboard-shortcuts -p /commands/custom -l 2>/dev/null); do
    xfconf-query -c xfce4-keyboard-shortcuts -p "$prop" -r 2>/dev/null
done

# Start monitoring script to keep app on top
(
    # would probably be good enough to only force it on top for 2 minutes,  after that keyboard shortcuts should be disabled anyways
    #for $(seq 120) ; do
    #this will run as long as groovix-session-start is running, which is overkill but won't hurt much
    while kill -0 $$ 2>/dev/null; do
        WID=$(xdotool search --class "groovix-accept-use" 2>/dev/null | tail -1)
        if [ -n "$WID" ]; then
            wmctrl -i -r "$WID" -b add,fullscreen,above 2>/dev/null
            xdotool windowactivate "$WID" 2>/dev/null
        fi
        sleep 1
    done
) &
MONITOR_PID=$!

echo "Starting groovix-accept-use..."
/usr/bin/groovix-accept-use
RC=$?
if [ $RC -ge 23 ] ; then
	#groovix-session-forced-exit "The acceptable use policy was not accepted."
	groovix-session-kill
	#wait for things to get killed, don't continue on with session start scripts
	sleep 1d
fi


# Trap will call restore_shortcuts when app exits

#without a delay here if the user hits accept within 1-2 seconds then xfsettingsd isn't loaded and ctrl-alt-g doesn't work
echo "waiting 5 seconds..."
#sleep 2
#2 is not enough
# but this should be replaced by waiting for something specific!
sleep 5

fi
