#! /bin/bash
# Version 1.36
# A (GPL'd) script to configure the idesk app. August 2004 John Murray john@jmurray.id.au
# See Changelog file for changes\
# Edited for Absolute Linux by Paul Sherman 07/21/2022

#First make sure Xdialog is available
which Xdialog &> ~/.idesktemp
if cat ~/.idesktemp | grep "no Xdialog in" > /dev/null ; then
	echo "ERROR: Xdialog not installed or not in path. Try http://xdialog.dyns.net/"
	rm -f ~/.idesktemp
	exit
else
	echo "Xdialog found. So far so good.."
	rm -f ~/.idesktemp
fi

#make sure idesk is available
which idesk &> ~/.idesktemp
if cat ~/.idesktemp | grep "no idesk in" > /dev/null ; then
	echo "ERROR: idesk installed or not in path. Try http://sourceforge.net/projects/idesk/"
	rm -f ~/.idesktemp
	exit
else
	echo "idesk found - we're on a roll :)"
	rm -f ~/.idesktemp
fi

#make sure the .idesktop folder exists and if not make it now, so idesk can start
mkdir -p ~/.idesktop

#find out whether an argument was used
if [ "$#" -ne 1 ]; then
	argy=nope
else
	argy=yup
fi
argu=$1

#See what icons are available
if [ -d /usr/share/idesk/icons/ ]; then
    icondir=/usr/share/idesk/icons/
else
	icondir=/usr/share/pixmaps/
fi

DIALOG=Xdialog

###############################################
############Declare some Functions#############
writeconf()
{
echo -e "table Config
  FontName: arial
  FontSize: 12
  FontColor: #ffffff
  Locked: false
  Transparency: 0
  Shadow: true
  ShadowColor: #000000
  ShadowX: 1
  ShadowY: 2
  Bold: false
  ClickDelay: 200
  IconSnap: true
  SnapWidth: 55
  SnapHeight: 100
  SnapOrigin: BottomRight
  SnapShadow: true
  SnapShadowTrans: 200
  CaptionOnHover: false
end
table Actions
  Lock: control right doubleClk
  Reload: middle doubleClk
  Drag: left hold
  EndDrag: left singleClk
  Execute[0]: right singleClk
  Execute[1]: right doubleClk
end" > ~/.ideskrc
}
################################################
################################################
newcaption()
{
$DIALOG --wizard --title "idesktool .... Absolute" \
        --inputbox "Icon Caption:\n
		(Leave blank for no caption)"\
		 12 56 "$cap" 2> ~/.idesktop/inputbox.tmp.$$
retval=$?
case $retval in
     0) cap=$(cat ~/.idesktop/inputbox.tmp.$$)
	caption=$(cat ~/.idesktop/inputbox.tmp.$$ | sed 's/\$/\\$/g')
	rm -f ~/.idesktop/inputbox.tmp.$$
	;;
     1) rm -f ~/.idesktop/inputbox.tmp.$$; exit
	;;
   255) rm -f ~/.idesktop/inputbox.tmp.$$; exit
	;;
     3) if [ "$firstwindow" = "argynope" ]; then
		argynope
	else
		argyyep
	fi
	;;
esac
newcommands
}
##########################################################
##########################################################
newcommands()
{
#We replace troublesome characters with underscores so as to avoid problems writing the file
modcaption=`echo $caption | sed "s/[\ \$\/\*\&\|\?\^\"\']/_/g"`

#if the icon has no caption (ie. empty string) we need to make a unique filename for it,
#otherwise we just use the caption for the filename

if [ "$modcaption" = "" ]; then
	modcaption=NoCaption$$
fi

$DIALOG --wizard --title "idesktool .... Absolute" \
   --separate-output --2inputsbox  "Commands"\
	22 60 "Right-Click Command:" "$command" "Command Location:\n(Default Recommended)" "idesktool $modcaption.lnk" 2> ~/.idesktop/inputbox.tmp.$$
retval=$?

case $retval in
     1) rm -f ~/.idesktop/inputbox.tmp.$$
	    exit
	    ;;
   255) rm -f ~/.idesktop/inputbox.tmp.$$
	    exit
	    ;;
     0) command=`cat ~/.idesktop/inputbox.tmp.$$ | sed -n "1p"`
	    if [ "$ICONTYPE" = "WEB" ]; then
			command="runBrowser $command"
		elif [ "$ICONTYPE" = "QTFM" ]; then
		   command="qtfm $command &"
		elif [ "$ICONTYPE" = "AROX" ]; then
		   command="arox $command &"
		elif [ "$ICONTYPE" = "APP" ]; then
		   command="$command &"
		else
		   echo "No Command Here"
		fi
	    rightclick=`cat ~/.idesktop/inputbox.tmp.$$ | sed -n "2p"`
	    rm -f ~/.idesktop/inputbox.tmp.$$
	    geticon
	    ;;
     3) newcaption
	    ;;
esac
}
###########################################################
###########################################################
getcurrentvalues()
{
#get the caption
	currentcaption=`cat $currentlnkfile | grep "Caption:" | sed "s/Caption://" | sed "s/^[ \t]*//"`
#get the icon
	currenticon=`cat $currentlnkfile | grep "Icon:" | sed "s/Icon:\ //" | sed "s/^[ \t]*//"`
#get the left click command
	command=`cat $currentlnkfile | grep "Command\[0\]:" | sed "s/Command\[0\]:\ //" | sed "s/^[ \t]*//"`
#get the right click command
	currentrightclick=`cat $currentlnkfile | grep "Command\[1\]:" | sed "s/Command\[1\]:\ //" | sed "s/^[ \t]*//"`
#get the X value
	currentxvalue=`cat $currentlnkfile | grep "X:" | sed "s/X://" | sed "s/^[ \t]*//"`
#get the Y value
	currentyvalue=`cat $currentlnkfile | grep "Y:" | sed "s/Y://" | sed "s/^[ \t]*//"`

modifycaption
}
#################################################################
#################################################################
modifycaption()
{
$DIALOG --wizard --title "idesktool .... Absolute" \
        --inputbox "Icon Caption:\n
		   (Leave blank for no caption)"\
		 12 56 "$currentcaption" 2> ~/.idesktop/inputbox.tmp.$$
retval=$?
case $retval in
     0) cap=$(cat ~/.idesktop/inputbox.tmp.$$)
	caption=$(cat ~/.idesktop/inputbox.tmp.$$ | sed 's/\$/\\$/g')
	rm -f ~/.idesktop/inputbox.tmp.$$
	;;
     1) rm -f ~/.idesktop/inputbox.tmp.$$
	exit
	;;
   255) rm -f ~/.idesktop/inputbox.tmp.$$
	exit
	;;
     3) if [ "$firstwindow" = "argyyep" ]; then
		argyyep
	else
		getlnkfile
	fi
	;;
esac
modifycommands
}
##############################################################
##############################################################
modifycommands()
{
comefrom=modif
#We replace troublesome characters with underscores using sed here, so as to avoid problems writing the file
modcaption=`echo $caption | sed "s/[\ \$\/\*\&\|\?\^\"\']/_/g"`

#if the icon has no caption (ie. empty string) we need to make a unique filename for it,
#otherwise we just use the caption for the filename
if [ "$modcaption" = "" ]; then
	modcaption=NoCaption$$
fi

$DIALOG --wizard --title "idesktool .... Absolute" \
   --separate-output --2inputsbox  "Commands"\
	22 60 "Right-Click Command:" "$command" "Command Location:\n(Default Recommended)" "idesktool $modcaption.lnk" 2> ~/.idesktop/inputbox.tmp.$$
retval=$?
case $retval in 
1) rm -f ~/.idesktop/inputbox.tmp.$$
   exit
   ;;
255) rm -f ~/.idesktop/inputbox.tmp.$$
     exit
     ;;
 0) command=`cat ~/.idesktop/inputbox.tmp.$$ | sed -n "1p"`
	if [ "$ICONTYPE" = "WEB" ]; then
		command="runBrowser $command"
	elif [ "$ICONTYPE" = "QTFM" ]; then
	   command="qtfm $command &"
	elif [ "$ICONTYPE" = "AROX" ]; then
	   command="arox $command &"
	elif [ "$ICONTYPE" = "APP" ]; then
		   command="$command &"
	else
	   echo "No Command Here"
	fi
	rightclick=`cat ~/.idesktop/inputbox.tmp.$$ | sed -n "2p"`
	rm -f ~/.idesktop/inputbox.tmp.$$
	geticon
	;;
3) currentcaption=$cap
   modifycaption
   ;;
esac
}
###########################################################
###########################################################
geticon()
{
if [ "$comefrom" = "modif" ]; then
	iconplaceholder=$currenticon
else
	iconplaceholder=$icondir
fi
rm -f ~/.ideskicon 2>/dev/null
python3 /usr/local/bin/idesk-icon.py

icon=`cat ~/.ideskicon` 2>/dev/null
rm -f ~/.ideskicon 2>/dev/null
if [ -z "${icon}" ]; then
    exit 0
fi
iconvalidate
}
###########################################################
###########################################################
iconvalidate()
{
if [ -e "$icon" ]; then
	iconexists=yep
else
	$DIALOG --title "D'OH!" \
	--icon ./warning.xpm \
        --msgbox "Error: Icon Image File Not Found." 0 0
case $? in
  	0) geticon
	   ;;
  	255) exit
	     ;;
esac
fi

iconcheck=`echo $icon | grep -i "\.png"`
if [ "$iconcheck" = "$icon" ]; then
writefile
	else
	$DIALOG --title "D'OH!" \
	--icon ./warning.xpm \
        --msgbox "Error: Unknown Filetype. The icon should be be a png image." 0 0
case $? in
  	0) geticon
	   ;;
  	255) exit
	     ;;
esac
fi
}
###########################################################
deleteandrefresh()
{
rm -f $currentlnkfile
killall idesk
idesk &
exit
}
#############################################################
#############################################################
getlnkfile()
{
currentlnkfile=`Xdialog --wizard --stdout --title "Please choose a file" --fselect ~/.idesktop 0 0`
retval=$?
case $retval in
     0) fileselectedyet=yes
	filecheck=`echo $currentlnkfile | grep -i "\.lnk"`
	if [ "$filecheck" = "$currentlnkfile" ] && [ -e "$currentlnkfile" ]; then
		if [ "$choice" = "Properties" ]; then
		getcurrentvalues
			else
			deleteandrefresh
		fi
	else
		$DIALOG --title "D'OH!" \
		--icon ./warning.xpm \
       		 --msgbox "Error: You need to select a valid file" 0 0
		case $? in
  			0) fileselectedyet=no
   			   getlnkfile
			   ;;
  			255) exit
			     ;;
		esac
	fi
	;;
	1) exit
	   ;;
	255) exit
	     ;;
	3) argynope
	   ;;
esac
}
##############################################################
##############################################################
refresh()
{
killall idesk
idesk &
exit
}
###################################################################
###################################################################
writefile()
{
if [ "$choice" = "Properties" ]; then
rm -f $currentlnkfile
	else
		currentxvalue=400
		currentyvalue=500
fi

touch ~/.idesktop/$modcaption.lnk
echo "table Icon" > ~/.idesktop/$modcaption.lnk
echo "  Caption: $caption" | sed 's/.\$/\$/g' >> ~/.idesktop/$modcaption.lnk
echo "  Icon: $icon" >> ~/.idesktop/$modcaption.lnk
echo "  X: $currentxvalue" >> ~/.idesktop/$modcaption.lnk
echo "  Y: $currentyvalue" >> ~/.idesktop/$modcaption.lnk
echo "  Command[0]: $command" >> ~/.idesktop/$modcaption.lnk
echo "  Command[1]: $rightclick" >> ~/.idesktop/$modcaption.lnk
echo "end" >> ~/.idesktop/$modcaption.lnk
chmod +x ~/.idesktop/$modcaption.lnk

$DIALOG --no-buttons --title "Finished!" \
        --infobox "All done. Restarting idesk now..." 0 0
refresh
}
####################################################################
####################################################################

argyyep()
{
firstwindow=argyyep
$DIALOG --title "idesktool .... Absolute" \
        --radiolist "		Desktop Icon Manager" 28 60 0 \
        "Properties" "		View or Edit This Icon" off \
        "Delete" "    		Remove This Icon" off \
        "New" "       		Add a New App Icon" off \
		"Open_QTFM" "      	Shortcut with QTFM" off \
		"Open_Arox" "      	Shortcut with Arox" off \
        "Website" "      		Add an internet Shortcut" off \
		"Set_Background" "	Choose a Desktop Image" off \
        "Refresh" "   		Reload the Desktop" off \
	    "Configure" " 	Edit iDesk's conf. file" off 2> ~/.idesktop/checklist.tmp.$$

retval=$?
case $retval in
	1) rm -f ~/.idesktop/checklist.tmp.$$
	   exit
	   ;;
	255) rm -f ~/.idesktop/checklist.tmp.$$
	     exit
	     ;;
	0) choice=`cat ~/.idesktop/checklist.tmp.$$`
	   rm -f ~/.idesktop/checklist.tmp.$$
	   case $choice in
	Delete)	deleteandrefresh
		    ;;
	Set_Background) setBackground.py &
					exit
					;;
	Properties) getcurrentvalues
		    modifycaption
		    ;;
	Website) cap=""
	         command="ENTER-URL-HERE"
			 ICONTYPE="WEB"
	         newcaption
	         ;;
	Open_QTFM) cap=""
	           command="ENTER_PATH_HERE"
			   ICONTYPE="QTFM"
	           newcaption
	           ;;
	Open_Arox) cap=""
	           command="ENTER_PATH_HERE"
			   ICONTYPE="AROX"
			   newcaption
	           ;;
	Refresh) refresh
		     ;;
	New) cap=""
	     command=""
		 ICONTYPE="APP"
	     newcaption
	     ;;
	Configure) editconfig
		       ;;
	esac
	;;
	esac
}
##################################################################
###################################################################
argynope()
{
firstwindow=argynope
$DIALOG --title "idesktool .... Absolute" \
        --radiolist "		Desktop Icon Manager" 28 60 0 \
        "Properties" "		View or Edit an Icon" off \
        "Delete" "    		Remove an Icon" off \
        "New" "       		Add a new App Icon" off \
		"Website" "       		Add an internet Shortcut" off \
		"Open_QTFM" "      	Shortcut with QTFM" off \
		"Open_Arox" "      	Shortcut with Arox" off \
		"Set_Background" "	Choose a Desktop Image" off \
        "Refresh" "   		Reload the Desktop" off \
	    "Configure" "		Edit iDesk's conf. file" off 2> ~/.idesktop/checklist.tmp.$$	
retval=$?
case $retval in
1) rm -f ~/.idesktop/checklist.tmp.$$
   exit
   ;;
255) rm -f ~/.idesktop/checklist.tmp.$$
     exit
     ;;
0) choice=`cat ~/.idesktop/checklist.tmp.$$`
   rm -f ~/.idesktop/checklist.tmp.$$
   case $choice in
		Delete)	getlnkfile
				deleteandrefresh
				;;
		Properties) getlnkfile
					;;
		Refresh) refresh
				 ;;
		Set_Background) setBackground.py &
						exit
				        ;;
		New) cap=""
			 command=""
			 ICONTYPE="APP"
			 newcaption
			 ;;
		Website) cap=""
				 command="ENTER-URL-HERE"
				 ICONTYPE="WEB"
			     newcaption
			     ;;
		Open_QTFM) cap=""
			       command="ENTER_PATH_HERE"
				   ICONTYPE="QTFM"
			       newcaption
			       ;;
		Open_Arox) cap=""
			       command="ENTER_PATH_HERE"
				   ICONTYPE="AROX"
			       newcaption
			       ;;
		Configure) editconfig
				   ;;
	esac
	;;
	esac
}
##########################################################
##########################################################
editconfig()
{
Xdialog --title "iDesk Configuration" --fixed-font  "" \
	--editbox ~/.ideskrc 0 0 2> ~/.idesktop/editbox.txt.$$

case $? in
  0) cat ~/.idesktop/editbox.txt.$$ > ~/.ideskrc
     rm -f ~/.idesktop/editbox.txt.$$
     refresh
     ;;
  1) rm -f ~/.idesktop/editbox.txt.$$
     exit
     ;;
  255) rm -f ~/.idesktop/editbox.txt.$$
       exit
       ;;
esac
}
##########################################################
##########################################################
#now let's see whether the configuration files are present
if [ -e ~/.ideskrc ]; then
	echo "Config. file  found - excellent!"
else
	$DIALOG --wrap --title "iDesk Configuration" \
        --yesno "WARNING: No ideskrc file was found.\n
Would you like to have this file\n
added now?" 0 0

case $? in
	1) exit
	   ;;
	255) exit
	     ;;
	0) writeconf
	   $DIALOG --title "iDesk Configuration"\
                --yesno "The configuration file has been written. \n
		Drag any icons you create to the \n
		desired locations and they will \n
		be remembered between sessions \n
		\n 
		Would you like to run the icon wizard now?" 0 0
		case $? in
			1) exit
			   ;;
			255) exit
			     ;;
			0) argynope
			   ;;
		esac
esac
fi

# Clear out any old temp files
rm -f ~/.idesktop/inputbox* 2>/dev/null

ICONTYPE=""

if [ "$argy" = "nope" ]; then
	argynope
else
	currentlnkfile=~/.idesktop/$argu
	argyyep
fi
