#! /bin/bash

readonly Fillepath____This_Script=$(realpath "${BASH_SOURCE[0]}")
	readonly Follpath____This_Script=$(dirname "$Fillepath____This_Script")




# Sept. 12, 2018, 8:00 AM.  This next section makes it so this script's behavior
# will change depending on whether it was launched via a symlink or not.

readonly Full_Uncanon_Unreal_Folpath____This_Script=$(dirname "${BASH_SOURCE[0]}")

echo "Full_Uncanon_Unreal_Folpath____This_Script: $Full_Uncanon_Unreal_Folpath____This_Script"

readonly Original_Working_Dir=$(pwd)

first_char_of_uncanon_folpath=$(echo "${Full_Uncanon_Unreal_Folpath____This_Script:0:1}")


if [ "$first_char_of_uncanon_folpath" != "/" ]
then
	readonly Full_Canon_Unreal_Folpath____This_Script=$(readlink -m "$Original_Working_Dir/$Full_Uncanon_Unreal_Folpath____This_Script/../..")
else
	readonly Full_Canon_Unreal_Folpath____This_Script=$(readlink -m "$Full_Uncanon_Unreal_Folpath____This_Script")
fi


if [ "$Full_Canon_Unreal_Folpath____This_Script" != "$Follpath____This_Script" ]
then
	readonly Follpath____Ramphys=$(readlink -m "$Full_Canon_Unreal_Folpath____This_Script")
else
	readonly Follpath____Ramphys=$(readlink -m "$Follpath____This_Script")
fi


readonly Follpath____Ramphyte=$(readlink -m "$Follpath____Ramphys/..")

readonly Foldername____Ramphys=$(basename "$Follpath____Ramphys") 

# Sept. 12, 2018, 8:03 AM.  End of the section which makes it so this script's behavior
# changes depending on whether it was launched via a symlink.


Action____Physave="Physave"
Action____Ramize="Ramize"
Action____Open_Folder="Open $Foldername____Ramphys Folder"
Action____Open_Config="Open config folder of $Foldername____Ramphys"
Action____Go_to_Physave_Path="Go to Physave Path"
Action____Go_to_Ramize_Path="Go to Ramize Path"


List____Vars_to_Export=(
		#"Follpath____This_Script"
		#"Follpath____This_Ramphyte"
		"Follpath____Ramphys"
		"Follpath____Ramphyte"
		"Foldername____Ramphys"
		
		"Action____Physave"
		"Action____Ramize"
		"Action____Open_Folder"
		"Action____Open_Config"
		"Action____Go_to_Physave_Path"
		"Action____Go_to_Ramize_Path"
	)


########
# Function
Search_for_Value_in_Array_Varvars()
{
	# 2016-02-07 08:30:20.  Uses variable variables.
		
	local value_to_search_for="$1"
	local this_array=("${!2}")
	local should_use_value_as_a_varvar="$3"
	local be_noisy="$4"
	
	local found_it
	
	
	#echo "This array: $2"
	
	for element in "${this_array[@]}"
	do 
	#	echo "Element: '$element' Value to search for '$value_to_search_for' '${!value_to_search_for}' "
		element_varvar="${!element}"
		echo "Element: $element  Element varvar: $element_varvar"
		if [ ! -z "$should_use_value_as_a_varvar" ]
		then
			if [ "$element_varvar" = "${!value_to_search_for}" ]
			then
				found_it=true
			fi
		else
			if [ "$element_varvar" = "$value_to_search_for" ]
			then
				found_it=true
			fi
		fi
		
		if [ "$found_it" = true ]
		then
			if [ ! -z "$be_noisy" ]
			# If be_noisy is not empty...
			then
				echo
				echo -n "Found value '$value_to_search_for'" 
				
				if [ ! -z "$should_use_value_as_a_varvar" ]
				then
					echo -n "'${!value_to_search_for}'"
				fi
				
				echo " in array $2!"
			fi
			return 0
		fi
	done
	
	if [ ! -z "$be_noisy" ]
	then
		echo -n "Didn't find value '$value_to_search_for'"
		
		if [ ! -z "$should_use_value_as_a_varvar" ]
		then
			echo -n "'${!value_to_search_for}'"
		fi
			
		echo " in array $2!"
	fi
	
	return 1
}
# End of
# Function - Search_for_Value_in_Array()
########




for string in "${List____Vars_to_Export[@]}"
do
	eval "export $string"
done



Do_Action()
{
	local action="$1"
	shift
	
	for item in "$@"
	do
		echo "Item $item "
	done
	
	echo "Action: $action" 
	
	case "$action" in
		
		"$Action____Open_Folder")
			cd "$Follpath____Ramphys"
			rox
			;;
			
		"$Action____Open_Config")
			cd "$Follpath____Ramphys/0000-Config"
			rox
			;;
			
		"$Action____Physave")
			cd "$Follpath____Ramphys/Physave"
			rox "Physave"
			;;
			
		"$Action____Ramize")
			cd "$Follpath____Ramphys/Ramize"
			rox "Ramize"
			;;
			
		"$Action____Go_to_Physave_Path")
			cd "$Follpath____Ramphys/Shortcuts"
			bash "Go to Physave Path"
			;;
			
		"$Action____Go_to_Ramize_Path")
			cd "$Follpath____Ramphys/Shortcuts"
			bash "Go to Ramize Path"
			;;
	esac
}

if [ ! -z "$1" ]
then
	Search_for_Value_in_Array_Varvars "$1" List____Vars_to_Export[@]
	if [ "$?" = 0 ]
	then
		Do_Action "$@"
	else
		for arg in "$@"
		do
			# 12:15 11/29/2016.  If it's a directory, open it with Rox.
			if [ -d "$arg" ]
			then
				rox "$arg"
			fi
		done
	fi
	exit "$?"
fi

export -f Do_Action


title="$Foldername____Ramphys"




MAIN_DIALOG='
<window default_height="250" default_width="400"'
MAIN_DIALOG+="title=\"$title\""
MAIN_DIALOG+='resizable="true" decorated="true">
  <vbox>'
  
MAIN_DIALOG+='<text'
MAIN_DIALOG+="><label>$Foldername____Ramphys</label></text>"


MAIN_DIALOG+='<text'
MAIN_DIALOG+="><label>$Follpath____Ramphyte</label></text>"


MAIN_DIALOG+='<list>
      <variable>LIST</variable>'


MAIN_DIALOG+="<item>$Action____Go_to_Ramize_Path</item>"
MAIN_DIALOG+="<item>$Action____Go_to_Physave_Path</item>"
MAIN_DIALOG+="<item>$Action____Physave</item>"
MAIN_DIALOG+="<item>$Action____Open_Config</item>"
MAIN_DIALOG+="<item>$Action____Open_Folder</item>"
MAIN_DIALOG+="<item>$Action____Ramize</item>"


MAIN_DIALOG+='<action>Do_Action "$LIST" &</action>'
	# Sept. 12, 2018, 7:14 AM.  Uncomment the preceding line
	# if you want to be able to run actions with just one click.
	
MAIN_DIALOG+='</list>
    <hbox>
     <button can-default="true" has-default="true">'
    # Sept. 12, 2018, 7:34 AM.  I still haven't figured out how to make
	# double-clicking on a list item run the action.
	#
	# But, making a default button at least made it so
	# you can run actions by highlighting them and pressing Enter.
	

MAIN_DIALOG+='<action>Do_Action "$LIST" &</action>'


MAIN_DIALOG+='</button>
     <button cancel></button>
    </hbox>
  </vbox>'
 

# 07:12:23 03/02/2018.  Ctrl-q or Esc to just quit.
	MAIN_DIALOG+='
<action signal="key-press-event" condition="command_is_true([[ $(( $KEY_MOD & 4 )) = 4 && $KEY_SYM = [qQ] ]] && echo true )">EXIT:quit</action>'

  		MAIN_DIALOG+='
  <action signal="key-press-event" condition="command_is_true([ $KEY_VAL = 0xff1b ] && echo true )">EXIT:quit</action>
	'


MAIN_DIALOG+='  
</window>
'


echo "$MAIN_DIALOG";

export MAIN_DIALOG

gtkdialog --center --program=MAIN_DIALOG