#!/bin/bash

xprop -root -remove _NET_WORKAREA

LOG_FILE="/tmp/workarea-${DISPLAY}.log"
NAME=`basename $0`
LOCK_FILE="/tmp/$NAME-${DISPLAY}-workarea.lock"

function terminate() {
	rm -f $LOCK_FILE
	pkill inotifywait
    pkill xprop
	exit 1
}

trap terminate INT TERM HUP KILL 

#nothing to do
if [ ! -f /usr/bin/inotifywait ] ; then exit; fi

#lock
copy=`ps h -C $NAME | grep -v $$ | wc -l`

if [ $copy -gt 1 ] ; then
	[ -f $LOCK_FILE ] && exit
fi
echo $$ > $LOCK_FILE

#echo "start monitoring" > $LOG_FILE
xprop -root -notype -spy > $LOG_FILE &

#wait changes in log file
while true ; do
    inotifywait -qq -r -e modify $LOG_FILE &
    wait $!
    MSG=$(tail -1 $LOG_FILE)
    echo "$MSG" | grep  -q "NET_WORKAREA = " && xprop -root -remove _NET_WORKAREA
done

terminate
