#!/bin/bash

# toogle click actions

singelClick_Actions="table Actions
  Lock:       control right singleClk
  Reload:     control left  singleClk
  Drag:       left    hold
  EndDrag:    left    singleClk
  Execute[0]: left    singleClk
  Execute[1]: middle  singleClk
  Execute[2]: right   singleClk
end"

doubleClick_Actions="table Actions
  Lock:       control right doubleClk
  Reload:     control left singleClk
  Drag:       right   hold
  EndDrag:    right   singleClk
  Execute[0]: left    singleClk
  Execute[1]: middle  singleClk
  Execute[2]: right   doubleClk
end"

# systemClick_Actions="$(sed -n -e '/^table Actions[[:space:]]*$/,/^end/p' $IDESKRC_SYSTEM_DEFAULT)"
# Check actions
# idesktoolClick_Actions="$(sed -n -e '/end".*/s//end/' -e '/^table Actions[[:space:]]*$/,/^end/p' $(which mx-idesktool))"
# currentActions="$(sed -n -e '/^table Actions[[:space:]]*$/,/^end/p' ~/.ideskrc)"

# check insert Command[12] of missing  
# check insert Command[12] of missing
for LNK in ~/.idesktop/*.lnk;
do
	if ! grep -sq -E 'Command(\[[12]\])' $LNK; then
	   sed -i -e "s/Command:/Command[0]:/" \
			  -e "/^end/i\ \ Command[1]:"  \
	          -e "/^end/i\ \ Command[2]:" $LNK
	fi
done

if  grep -sq 'doubleClk' ~/.ideskrc; then
	NewActions="$singelClick_Actions"
	#sed -r -i.$(date '+%Y.%m.%d_%H%M%S').bak 
	sed -i -r \
	    -e '/^.*Command(\[[012]\])?:/s/mx-idesktool//g;' \
	    -e '/^.*Command(\[[012]\])?:/s/idesk(run|menu|toggle)//g;' \
	    -e '/^.*Command:([[:space:]]*)(.*)/s//  Command[0]: ideskrun \2/;' \
	    -e '/^.*Command\[0\]:([[:space:]]*)(.*)/s//  Command[0]: ideskrun \2/;' \
	    -e '/^.*Command\[1\]:([[:space:]]*)(.*)/s//  Command[1]: mx-idesktool \2/;' \
	    -e '/^.*Command\[2\]:([[:space:]]*)(.*)/s//  Command[2]: ideskmenu \2/;' \
	    -e 's/:[[:space:]]+/: /' \
	     ~/.idesktop/*.lnk

	echo "singleClick Actions enabled"
else
	NewActions="$doubleClick_Actions"

#	sed -r -i.$(date '+%Y.%m.%d_%H%M%S').bak 
	sed -i -r \
	    -e '/^.*Command(\[[012]\])?:/s/mx-idesktool//g;' \
	    -e '/^.*Command(\[[012]\])?:([[:space:]]*)(.*)/s/idesk(run|menu|toggle)//g;' \
	    -e '/^.*Command\[1\]:([[:space:]]*)(.*)/s//  Command[1]: ideskmenu \2/;' \
	    -e 's/:[[:space:]]+/: /' \
		~/.idesktop/*.lnk

	echo "doubleClick Actions enabled"
fi

# remove current actions
# sed -i.$(date '+%Y.%m.%d_%H%M%S').bak -e '/^table Actions/,/^end/d'  ~/.ideskrc
sed  -i -e '/^table Actions/,/^end/d'  ~/.ideskrc

echo "$NewActions" >>  ~/.ideskrc
pkill -u $USER -x idesk; sleep 0.3;  ( idesk & disown) >/dev/null 2>&1
 
