#!/bin/bash

ob_rc="$HOME/.config/openbox/rc.xml"
cw=$(grep -n 'application type="normal">' $ob_rc)
m=${cw%:*}; ((n=m+5))

<< 'Manipulate_xy'
screen=$(cat $HOME/.screenlayout/last_resol)
screenWidth=${screen%%x*}
screenHeight=${screen#*x}
(( half_x = screenWidth/2 ))
(( half_y = screenHeight/2 ))
app=$(grep -n ' \* Menu' $ob_rc)
((a=${app%:*}+2))
term=$(grep -n '\*\.\.\. Menu' $ob_rc)
((t=${term%:*}+2))
[ "$(grep "<pos.*</pos" $ob_rc | cut -d'>' -f2 | cut -d'<' -f1)" = "Left" ] && sed -i "$((a+1))s~>[^-]*<~>$((half_y-480))<~; $((t+1))s~>[^-]*<~>$((half_y+230))<~" $ob_rc*
openbox --reconfigure
Manipulate_xy

scroll_state="$(xset q | grep Scroll\ Lock)"
[ -z "$SUSPEND_TIME" ] && SUSPEND_TIME=10
SUSPEND_TIME=$((SUSPEND_TIME*59*1000))
while true; do
    sleep 1
    scroll_state_new="$(xset q | grep Scroll\ Lock)"
    if [ "$scroll_state_new" != "$scroll_state" ]; then
	[[ "$scroll_state_new" =~ "Scroll Lock: off" ]] && sed -i "${m}s~<!--~<~; ${n}s~-->~>~" $ob_rc* || sed -i "${m}s~<a~<!--a~; ${n}s~n>~n-->~" $ob_rc*
	scroll_state="$scroll_state_new"
	openbox --reconfigure
    fi
    if [ $(xprintidle) -ge $SUSPEND_TIME ]; then
	$ENABLE_SUSPEND_TIME && systemctl suspend # || xdotool keydown Shift_L keyup Shift_L
    fi
    IFS=$'\n'; for w in $(wmctrl -l); do unset IFS; win=($w); [ -z "${win[3]}" ] && xdotool windowclose ${win[0]}; done
done
