#!/bin/bash
#
# VL Modifications for IceWM. Set defaults for browser, file manager and terminal

# Check for X
if [ -z $DISPLAY ];then echo "X-windows required";exit;fi

choose=$(
export MAIN_DIALOG='
<window title="Update Menu">
 <vbox>
  <frame  Set default applications >
  <hbox>
      <button use-underline="true">
        <label>" Change _browser            "</label>
        <action>echo browser</action>
        <action type="exit">BROWSER</action>
      </button>
  </hbox>
  <hbox>
      <button use-underline="true">
        <label>" Change _file manager     "</label>
        <action>echo browser</action>
        <action type="exit">FILEMAN</action>
      </button>
  </hbox>
  <hbox>
      <button use-underline="true">
        <label>" Change _terminal            "</label>
        <action>echo editor</action>
        <action type="exit">TERMINAL</action>
      </button>
  </hbox>
  </frame>
   <button cancel></button>
 </vbox>
</window>
'
gtkdialog --program=MAIN_DIALOG
)

# cancel and exit?
if [ ! -z "$(echo $choose | grep "EXIT=\"Cancel\"" -o)" ];then exit;fi

# sort out what was chosen
if [ ! -z "$(echo $choose | grep "EXIT=\"BROWSER\"" -o)" ];then SETBROWSER="yes";fi
if [ ! -z "$(echo $choose | grep "EXIT=\"FILEMAN\"" -o)" ];then SETFILEMAN="yes";fi
if [ ! -z "$(echo $choose | grep "EXIT=\"TERMINAL\"" -o)" ];then SETTERMINAL="yes";fi

# Addition for VL7
if [ "$SETBROWSER" = yes ];then 
	 echo SET BROWSER
	# find browsers on system
		browserlist=
		for i in firefox palemoon google-chrome chromium-browser vivaldi qupzilla opera seamonkey konqueror galeon epiphany iceape iceweasel midori xombrero xxxterm netsurf kazehakase hv3 arora dillo amaya start-tor-browser;do
			found=$(which $i 2>/dev/null)
			if [ ! -z "$found" ];then browserlist=""$browserlist" FALSE $i";fi
		done
		# pick a browser
		browser=$(zenity --list --height 250 --title "Set Default Browser" --text "Pick your favorite from the list" --radiolist --column "Select" --column "Browser" $browserlist)
		if [ -z "$browser" ];then zenity --info --timeout 2 --title "Default Browser" --text " No browser selected. \n No changes made.";exit;fi
	# modify menu
	 oldline=$(grep "prog Browser" $HOME/.icewm/menu)
	 oldpre=$(cat $HOME/.icewm/menu | grep "prog Browser" | awk '{print $1} {print $2} {print $3}')
	 newline="$(echo $oldpre $browser)"
	 sed -i "s^$oldline^$newline^" $HOME/.icewm/menu
	# modify .bashrc and .xprofile
		oldbrowser2=$(grep "^export BROWSER=" $HOME/.bashrc)
		sed -i "s|$oldbrowser2|export BROWSER=$browser|" $HOME/.bashrc
		oldbrowser3=$(grep "^export BROWSER=" $HOME/.xprofile)
		sed -i "s|$oldbrowser3|export BROWSER=$browser|" $HOME/.xprofile
	# modify toolbar
	 oldline=$(grep "prog Browser" $HOME/.icewm/toolbar)
	 oldpre=$(echo $oldline | cut -d ' ' -f 1-3)
	 newline="$(echo $oldpre $browser)"
	 sed -i "s^$oldline^$newline^" $HOME/.icewm/toolbar
	# give some feedback 
	zenity --info --timeout 2 --title "Default Browser" --text " $browser \n set as local default"
	exit
fi 

if [ "$SETFILEMAN" = yes ];then
	 echo SET FILEMAN
	# find file managers on system
		filemanlist=
		for i in spacefm pcmanfm-mod pcmanfm konqueror dolphin nautilus thunar krusader emelfm2 rox worker xnc 4pane rodent xfe gnome-commander;do
			found=$(which $i 2>/dev/null)
			if [ ! -z "$found" ];then filemanlist=""$filemanlist" FALSE $i";fi
		done
		# pick a file manager
		fileman=$(zenity --list --height 250 --title "Set file manager" --text "Pick your favorite from the list" --radiolist --column "Select" --column "File Manager" $filemanlist)
		if [ -z "$fileman" ];then zenity --info --timeout 2 --title "Default file manager" --text " No file manager selected. \n No changes made.";exit;fi
	# modify .bashrc and .xprofile
	 oldfileman2=$(grep "^export EXPLORER=" $HOME/.bashrc)
	 sed -i "s|$oldfileman2|export EXPLORER=$fileman|" $HOME/.bashrc
     oldfileman3=$(grep "^export EXPLORER=" $HOME/.xprofile)
	 sed -i "s|$oldfileman3|export EXPLORER=$fileman|" $HOME/.xprofile
	# modify menu, slight modification for spacefm
	 if [ "$fileman" = spacefm ];then fileman="spacefm -nw ~";fi
	 oldline=$(grep "prog Home" $HOME/.icewm/menu)
	 oldpre=$(cat $HOME/.icewm/menu | grep "prog Home" | awk '{print $1} {print $2} {print $3}')
	 newline="$(echo $oldpre $fileman)"
	 sed -i "s^$oldline^$newline^" $HOME/.icewm/menu
	# modify toolbar
	 oldline=$(grep "prog \"File Manager\"" $HOME/.icewm/toolbar)
	 oldpre=$(echo $oldline | cut -d ' ' -f 1-4)
	 newline="$(echo $oldpre $fileman)"
	 sed -i "s^$oldline^$newline^" $HOME/.icewm/toolbar
	# give some feedback 
	zenity --info --timeout 2 --title "Default file manager" --text " $(echo $fileman|awk '{print $1}') \n set as local default"
	exit
fi 

if [ "$SETTERMINAL" = yes ];then
	 echo SET TERMINAL
	# find terminals on system
		terminallist=
		for i in aterm eterm evilvte gnome-terminal gtkterm guake konsole Kuake kterm lilyterm lxterminal mlterm mrxvt roxterm rxvt rxvt-unicode sakura terminal Terminator termit tilda wterm xterm xvt Yakuake;do
			found=$(which $i 2>/dev/null)
			if [ ! -z "$found" ];then terminallist=""$terminallist" FALSE $i";fi
		done
		# pick a terminal
		terminal=$(zenity --list --height 250 --title "Set Default terminal" --text "Pick your favorite from the list" --radiolist --column "Select" --column "terminal" $terminallist)
		if [ -z "$terminal" ];then zenity --info --timeout 2 --title "Default terminal" --text " No terminal selected. \n No changes made.";exit;fi
	# change menu
	oldline=$(grep "prog Terminal" $HOME/.icewm/menu)
	oldpre=$(cat $HOME/.icewm/menu | grep "prog Terminal" | awk '{print $1} {print $2} {print $3}')
	newline="$(echo $oldpre $terminal)"
	sed -i "s^$oldline^$newline^" $HOME/.icewm/menu
	# change toolbar 
	oldline=$(grep "prog Terminal" $HOME/.icewm/toolbar)
	oldpre=$(cat $HOME/.icewm/toolbar | grep "prog Terminal" | awk '{print $1} {print $2} {print $3}')
	newline="$(echo $oldpre $terminal)"
	sed -i "s^$oldline^$newline^" $HOME/.icewm/toolbar
	
	# give some feedback 
	zenity --info --timeout 2 --title "Default terminal" --text " $terminal \n set as local default"
	exit
fi 
