#!/bin/bash
#pRun 1.7.2
#Set hotkeys (radky)

export TEXTDOMAIN=prun
export OUTPUT_CHARSET=UTF-8

case "$1" in

-prun_hotkey)
#IceWM hotkey
	if [ ! -s $HOME/.icewm/keys-bak ] && [ -s $HOME/.icewm/keys ]; then
	 cp $HOME/.icewm/keys $HOME/.icewm/keys-bak
	fi
	if [ ! -s $HOME/.icewm/keys ] && [ -s /usr/share/icewm/keys ]; then
	 cat /usr/share/icewm/keys | sed '$a key "Ctrl+Alt+r"		prun' > /tmp/tmpfile ; mv -f /tmp/tmpfile $HOME/.icewm/keys
	fi
	if [ -s $HOME/.icewm/keys ] && [ "`grep -aE 'prun' $HOME/.icewm/keys`" = "" ]; then
	 cat $HOME/.icewm/keys | sed '$a key "Ctrl+Alt+r"		prun' > /tmp/tmpfile ; mv -f /tmp/tmpfile $HOME/.icewm/keys
	fi

#JWM hotkey
	if [ ! -s $HOME/.jwm/jwmrc-personal-bak ] && [ -s $HOME/.jwm/jwmrc-personal ]; then
	 cp $HOME/.jwm/jwmrc-personal $HOME/.jwm/jwmrc-personal-bak
	fi
	if [ -s $HOME/.jwm/jwmrc-personal ] && [ "`grep -aE 'Key mask="CA" key="r">exec:prun' $HOME/.jwm/jwmrc-personal`" = "" ]; then
	 awk '/<Key mask=/{p++} /<Key mask=/ && p==1 {$0= $0"\n<Key mask=\"CA\" key=\"r\">exec:prun</Key>"}1' $HOME/.jwm/jwmrc-personal > /tmp/tmpfile ; mv -f /tmp/tmpfile $HOME/.jwm/jwmrc-personal
	fi

#Lxde/Lxpanel hotkey
	if [ ! -s $HOME/.config/openbox/lxde-rc.xml-bak ] && [ -s $HOME/.config/openbox/lxde-rc.xml ]; then
	 cp $HOME/.config/openbox/lxde-rc.xml $HOME/.config/openbox/lxde-rc.xml-bak
	fi
	if [ -s $HOME/.config/openbox/lxde-rc.xml ]; then
	 cat $HOME/.config/openbox/lxde-rc.xml | sed -e 's/^.*<keyboard>.*$/  <keyboard>/' -e 's/^.*Keybindings for running applications.*$/    <\!-- Keybindings for running applications -->/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/lxde-rc.xml
	 if [ "`grep -aE '<\!-- Keybindings for running applications -->' $HOME/.config/openbox/lxde-rc.xml`" = "" ]; then
	  cat $HOME/.config/openbox/lxde-rc.xml | sed 's/^.*  <keyboard>.*$/  <keyboard>\n    <\!-- Keybindings for running applications -->/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/lxde-rc.xml
	 fi
	 if [ "`grep -aE 'keybind key=\"C-A-r\"' $HOME/.config/openbox/lxde-rc.xml`" = "" ]; then
	  cat $HOME/.config/openbox/lxde-rc.xml | sed -e 's/^.*    <\!-- Keybindings for running applications -->.*$/    <\!-- Keybindings for running applications -->\n    <keybind key=\"C-A-r\">\n      <action name=\"Execute\">\n        <command>prun run<\/command>\n      <\/action>\n    <\/keybind>/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/lxde-rc.xml
	 fi
	fi

#Openbox hotkey
	if [ ! -s $HOME/.config/openbox/rc.xml ] && [ -s /etc/xdg/openbox/rc.xml ]; then
	 if [ ! -d  $HOME/.config/openbox/ ]; then
	  mkdir -p $HOME/.config/openbox/
	 fi
	 cp -f /etc/xdg/openbox/rc.xml $HOME/.config/openbox/rc.xml
	fi
	if [ ! -s $HOME/.config/openbox/menu.xml ] && [ -s /etc/xdg/openbox/menu.xml ]; then
	 cp -f /etc/xdg/openbox/menu.xml $HOME/.config/openbox/menu.xml
	fi
	if [ ! -s $HOME/.config/openbox/rc.xml-bak ] && [ -s $HOME/.config/openbox/rc.xml ]; then
	 cp $HOME/.config/openbox/rc.xml $HOME/.config/openbox/rc.xml-bak
	fi
	if [ -s $HOME/.config/openbox/rc.xml ]; then
	 cat $HOME/.config/openbox/rc.xml | sed -e 's/^.*<keyboard>.*$/  <keyboard>/' -e 's/^.*Keybindings for running applications.*$/    <\!-- Keybindings for running applications -->/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/rc.xml
	 if [ "`grep -aE '<\!-- Keybindings for running applications -->' $HOME/.config/openbox/rc.xml`" = "" ]; then
	  cat $HOME/.config/openbox/rc.xml | sed 's/^.*  <keyboard>.*$/  <keyboard>\n    <\!-- Keybindings for running applications -->/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/rc.xml
	 fi
	fi
	if [ -s $HOME/.config/openbox/rc.xml ] && [ "`grep -aE 'keybind key=\"C-A-r\"' $HOME/.config/openbox/rc.xml`" = "" ]; then
	 cat $HOME/.config/openbox/rc.xml | sed -e 's/^.*    <\!-- Keybindings for running applications -->.*$/    <\!-- Keybindings for running applications -->\n    <keybind key=\"C-A-r\">\n      <action name=\"Execute\">\n        <command>prun run<\/command>\n      <\/action>\n    <\/keybind>/' > /tmp/tmpfile && mv -f /tmp/tmpfile $HOME/.config/openbox/rc.xml
	fi

#Hotkey notification
	Xdialog --title pRun --beep --msgbox "\n  $(gettext 'Setting the keyboard hotkey...')  \n\n  $(gettext 'Ctrl+Alt+R') \n\n  $(gettext 'Restart the window manager')  \n   $(gettext 'to enable the hotkey function.')  \n"  0 0 &
	;;

esac
