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
else
	# policy accepted 
	if [ "$GX_LOGIN_EFFECTIVE_POST_ACCEPT_USE" = "true" ] && [ "$GX_ACCEPT_USE_IN_SESSION" != "true" ] ; then
	        touch /var/lib/groovix/session-info/$USER.effective-login-time
	        echo $USER > /var/lib/groovix/session-info/effective-current-user
	fi
fi


# Trap will call restore_shortcuts when app exits


fi
