#!/bin/bash
CONFIG_FILE=~/.media-detect.conf
TEMP_DIR=/tmp
KDIALOG="kdialog --title Media-Detect"

# Interface Templates
# These functions provide the user interfaces for the menu system.
menuTemplate ()
{
	MENU_COUNT=0
	if [ "$USE_DIALOGS" = "1" ]
	then
		MENU_STRING='SELECTION=`'"$KDIALOG"' --menu '"\"$MENU_TEXT\" "
		until [ "$MENU_COUNT" = "$1" ]
		do
			let "MENU_COUNT+=1"
			MENU_STRING="$MENU_STRING$MENU_COUNT \"${ENTRY_TEXT[$MENU_COUNT]}\" "
		done
		let "RETURN_OPTION=$MENU_COUNT+1"
		MENU_STRING="$MENU_STRING$RETURN_OPTION \"$RETURN_TEXT\" "
		let "EXIT_OPTION=$RETURN_OPTION+1"
		MENU_STRING="$MENU_STRING$EXIT_OPTION \"Exit Media-Detect\""'`'
		echo -e "$MENU_STRING\nif [ \"\$?\" = \"1\" ]\nthen\necho \"CANCEL\" > /tmp/mdmenu_script2.tmp\nelse\necho \$SELECTION > /tmp/mdmenu_script2.tmp\nfi" > $TEMP_DIR/mdmenu_script.tmp
		bash $TEMP_DIR/mdmenu_script.tmp
		OUTPUT=`cat $TEMP_DIR/mdmenu_script2.tmp`
		if [ "$OUTPUT" = "$EXIT_OPTION" ]
		then
			exit 0
		fi
	else
		MENU_STRING="Media-Detect\n------------\n$MENU_TEXT\n"
		until [ "$MENU_COUNT" = "$1" ]
		do
			let "MENU_COUNT+=1"
			MENU_STRING="$MENU_STRING\n$MENU_COUNT) ${ENTRY_TEXT[$MENU_COUNT]}"
		done
		let "RETURN_OPTION=$MENU_COUNT+1"
		MENU_STRING="$MENU_STRING\n$RETURN_OPTION) $RETURN_TEXT"
		let "EXIT_OPTION=$RETURN_OPTION+1"
		MENU_STRING="$MENU_STRING\n$EXIT_OPTION) Exit Media-Detect\nPlease make a selection (1-$EXIT_OPTION)"
		OUTPUT=""
		until [[ $OUTPUT -ge 1 && $OUTPUT -le $EXIT_OPTION ]]
		do
			clear
			echo -e "$MENU_STRING"
			read OUTPUT
		done
		if [ "$OUTPUT" = "$RETURN_OPTION" ]
		then
			if [ "$FUNCTION" != "mainMenu" ]
			then
				OUTPUT="CANCEL"
			fi
		fi
		if [ "$OUTPUT" = "$EXIT_OPTION" ]
		then
			clear
			exit 0
		fi
	fi
}
checklistTemplate ()
{
	MENU_COUNT=0
	if [ "$USE_DIALOGS" = "1" ]
	then
		MENU_STRING='SELECTION=`'"kdialog --title \"Media-Detect\""' --separate-output --checklist '"\"$CHECKLIST_TEXT\" "
		until [ "$MENU_COUNT" = "$1" ]
		do
			let "MENU_COUNT+=1"
			if [ "$MENU_COUNT" = "1" ]
			then
				MENU_STRING="$MENU_STRING$MENU_COUNT \"${ENTRY_TEXT[$MENU_COUNT]}\" on "
			else
				MENU_STRING="$MENU_STRING$MENU_COUNT \"${ENTRY_TEXT[$MENU_COUNT]}\" off "
			fi
		done
		MENU_STRING="$MENU_STRING\`"
		echo -e "$MENU_STRING\nif [ \"\$?\" = \"1\" ]\nthen\necho \"CANCEL\" > /tmp/mdmenu_script2.tmp\nelse\necho \$SELECTION > /tmp/mdmenu_script2.tmp\nfi" > $TEMP_DIR/mdmenu_script.tmp
		bash $TEMP_DIR/mdmenu_script.tmp
		OUTPUT=`cat $TEMP_DIR/mdmenu_script2.tmp`
		OUTPUT_ONE=`echo "$OUTPUT" | cut -d" " -f1-1`
		OUTPUT_TWO=`echo "$OUTPUT" | cut -d" " -f2-2`
	else
		MENU_STRING="Media-Detect\n------------\n$CHECKLIST_TEXT\n"
		until [ "$MENU_COUNT" = "$1" ]
		do
			let "MENU_COUNT+=1"
			MENU_STRING="$MENU_STRING\n$MENU_COUNT) ${ENTRY_TEXT[$MENU_COUNT]}"
		done
		let "CANCEL_OPTION=$1+1"
		MENU_STRING="$MENU_STRING\n$CANCEL_OPTION) $RETURN_TEXT\nPlease make a selection (1-$CANCEL_OPTION)"
		until [[ $OUTPUT_ONE -ge 1 && $OUTPUT_ONE -le $CANCEL_OPTION ]]
		do
			clear
			echo -e "$MENU_STRING"
			read OUTPUT_ONE
		done
		if [ "$OUTPUT_ONE" = "$CANCEL_OPTION" ]
		then
			OUTPUT="CANCEL"
		else
			until [[ $OUTPUT_TWO -ge 1 && $OUTPUT_ONE -le $1 ]]
			do
				echo -e "\nPlease make a second selection (1-$1, excluding $OUTPUT_ONE)"
				read OUTPUT_TWO
			done
			if [ "$OUTPUT_TWO" != "$CANCEL_OPTION" ]
			then
				OUTPUT=`echo -e "$OUTPUT_ONE $OUTPUT_TWO"`
			else
				OUTPUT="CANCEL"
			fi
		fi
	fi
}
confirmTemplate ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		$KDIALOG --yesno "$CONFIRM_TEXT"
		return $?
	else
		echo -e "\n$CONFIRM_TEXT"
		ttyOldSettings=$(stty -g)
		stty -echo
		stty -icanon
		YESNO=$(head -c1)
		stty "$ttyOldSettings"
		if [ "$YESNO" = "y" -o "$YESNO" = "Y" ]
		then
			return 0
		else
			return 1
		fi
	fi
}
inputTemplate ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		OUTPUT=`$KDIALOG --inputbox "$INPUT_TEXT" "$DEFAULT_INPUT"`
		if [ $? = 1 ]
		then
			OUTPUT="CANCEL"
		fi
	else
		clear
		echo -e "Media-Detect\n------------\n$INPUT_TEXT (Default: $DEFAULT_INPUT)"
		read OUTPUT
		if [ x"$OUTPUT" = x ]
		then
			if [ x"$DEFAULT_INPUT" = x ]
			then
				OUTPUT="CANCEL"
			else
				OUTPUT="$DEFAULT_INPUT"
			fi
		fi
	fi
}
openTemplate ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		CHOOSE_FILE=`$KDIALOG --title "$FILE_SELECT" --getopenfilename ~/`
		return $?
	else
		echo -e "\n$FILE_SELECT...\nPlease type the path to a file:"
		read CHOOSE_FILE
		if [ x"$CHOOSE_FILE" = x ]
		then
			return 1
		else
			return 0
		fi
	fi
}
errorTemplate ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		$KDIALOG --sorry "$ERROR_TEXT"
	else
		echo -e "\n$ERROR_TEXT\n"
		sleep 2
	fi
}
messageTemplate ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		$KDIALOG --msgbox "$MESSAGE_TEXT"
	else
		echo -e "\n$MESSAGE_TEXT"
		sleep 2
	fi
}

# System configurations
# These are the global configuration functions. They affect how Media-Detect works system-wide.
globalSettings ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="globalSettings"
		MENU_TEXT="Media-Detect Global Configurations"
		RETURN_TEXT="Return To Editor Menu"
		ENTRY_TEXT=([1]="Turn On/Off Extended Mode" [2]="Change Interface Mode" [3]="Adjust Default Volume Control")
		menuTemplate 3
		if [ "$OUTPUT" != "CANCEL" ]
		then
			case $OUTPUT in
				"1")extendedSet;;
				"2")interfaceSet;;
				"3")globalVolume;OUTPUT="";;
				"4")OUTPUT="CANCEL";;
			esac
			rebuildConfig
		fi
	done
}
globalVolume ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="globalVolume"
		MENU_TEXT="Global Volume Settings"
		RETURN_TEXT="Return To Global Configuration Menu"
		ENTRY_TEXT=([1]="Set Default Mixer" [2]="Set Default Channel")
		menuTemplate 2
		if [ "$OUTPUT" != "CANCEL" ]
		then
			case $OUTPUT in
				"1")setGlobalMixer;MESSAGE_TEXT="Default Mixer Updated.";OUTPUT="";;
				"2")setGlobalChannel;MESSAGE_TEXT="Default Channel Updated.";OUTPUT="";;
				"3")OUTPUT="CANCEL";;
			esac
			if [ "$OUTPUT" != "CANCEL" ]
			then
				rebuildConfig
				messageTemplate
			fi
		fi
	done
}
setGlobalMixer ()
{
	CONFIRM_TEXT="Current Default Mixer: ${VOL_GLOBAL[1]}\nDo you want to change this setting?"
	confirmTemplate
	if [ $? = 0 ]
	then
		INPUT_TEXT="New Default Mixer:"
		DEFAULT_INPUT="${VOL_GLOBAL[1]}"
		inputTemplate
		if [ "$OUTPUT" != "CANCEL" ]
		then
			VOL_GLOBAL[1]="$OUTPUT"
		fi
	fi
}
setGlobalChannel ()
{
	FUNCTION="setGlobalChannel"
	CONFIRM_TEXT="Current Default Channel: ${GLOBAL_VOL_DISPLAY[2]}\nDo you want to change this setting?"
	confirmTemplate
	if [ $? = 0 ]
	then
		MENU_TEXT="Set Default Channel (Currently: ${GLOBAL_VOL_DISPLAY[2]})"
		RETURN_TEXT="Return To Global Volume Configuration"
		ENTRY_TEXT=([1]="Master" [2]="PCM" [3]="Line" [4]="Mic" [5]="CD" [6]="PC Speaker" [7]="Other...")
		menuTemplate 7
		if [ "$OUTPUT" = "7" ]
		then
			INPUT_TEXT="Please enter the desired channel (uses aumix channel options):"
			DEFAULT_INPUT=""
			inputTemplate
			if [ "$OUTPUT" != "CANCEL" ]
			then
				GLOBAL_VOL_DISPLAY[2]="User Defined"
				VOL_GLOBAL[2]="$OUTPUT"
			fi
		else
			case $OUTPUT in
				"1")GLOBAL_VOL_DISPLAY[2]="Master";VOL_GLOBAL[2]="v";OUTPUT="CANCEL";;
				"2")GLOBAL_VOL_DISPLAY[2]="PCM";VOL_GLOBAL[2]="w";OUTPUT="CANCEL";;
				"3")GLOBAL_VOL_DISPLAY[2]="Line";VOL_GLOBAL[2]="l";OUTPUT="CANCEL";;
				"4")GLOBAL_VOL_DISPLAY[2]="Mic";VOL_GLOBAL[2]="m";OUTPUT="CANCEL";;
				"5")GLOBAL_VOL_DISPLAY[2]="CD";VOL_GLOBAL[2]="c";OUTPUT="CANCEL";;
				"6")GLOBAL_VOL_DISPLAY[2]="PC Speaker";VOL_GLOBAL[2]="p";OUTPUT="CANCEL";;
				"CANCEL")OUTPUT="CANCEL";;
			esac
		fi
	fi
}
interfaceSet ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="interfaceSet"
		MENU_TEXT="Please select your preferred interface format:"
		RETURN_TEXT="Return To Global Configuration Menu"
		MESSAGE_TEXT="Menu format changed.  Please restart the Media-Detect menu."
		case "${DIALOG_CHECK:8:1}" in
			"0")ENTRY_TEXT=([1]="Command line menus (Current selection)" [2]="Dialog-based menus" [3]="Use system settings");;
			"1")ENTRY_TEXT=([1]="Command line menus" [2]="Dialog-based menus (Current selection)" [3]="Use system settings");;
			"2")ENTRY_TEXT=([1]="Command line menus" [2]="Dialog-based menus" [3]="Use system settings (Current selection)");;
		esac
		menuTemplate 3
		if [ "$OUTPUT" != "CANCEL" ]
		then
			if [[ $OUTPUT -le 3 ]]
			then
				let "OUTPUT-=1"
				DIALOG_CHECK="dialogs=$OUTPUT"
				rebuildConfig
				messageTemplate
				exit 0
			else
				OUTPUT="CANCEL"
			fi
		fi
	done
}
extendedSet ()
{
	case $EXTENDED in
		"0")CONFIRM_TEXT="Extended Mode is currently disabled.\nDo you wish to enable it?";MESSAGE_TEXT="Extended Mode enabled.";;
		"1")CONFIRM_TEXT="Extended Mode is currently enabled.\nIf you disable it, you will lose all Extended commands.\nDo you wish to disable it?";MESSAGE_TEXT="Extended Mode disabled.";;
	esac
	confirmTemplate
	if [ $? = 0 ]
	then
		case $EXTENDED in
			"0")EXTENDED=1;;
			"1")EXTENDED=0;;
		esac
		messageTemplate
	fi
}

# Startup functions
# These functions read the config file to determine which applications to display and how to
# display them at startup.
sysDialogs ()
{
	if [ "$DESKTOP" = "kde" ]
	then
		USE_DIALOGS=1
	else
		USE_DIALOGS=0
	fi
}
getAppConfig ()
{
	APP_COUNT=`echo "$CURRENT_CONFIG" | grep -c plps`
	APP_NUM=0
	until [ "$APP_NUM" = "$APP_COUNT" ]
	do
		ENTRY=1
		let "APP_NUM+=1"
		processInfo $APP_NUM "$CURRENT_CONFIG"
		ENTRY=1
		processAlts $APP_NUM "$CURRENT_CONFIG"
		if [ "$EXTENDED" = "1" ]
		then
			ENTRY=1
			processExtd $APP_NUM "$CURRENT_CONFIG"
		fi
		# Upgrades configuration to include volume
		for ALL in "7" "8"
		do
			if [ x"${APP_INFO[$APP_NUM$ALL]}" = x ]
			then
				APP_INFO[$APP_NUM$ALL]="default"
			fi
		done
	done
	GET_VOL=`echo "$CURRENT_CONFIG" | grep -i default_mixer`
	if [ x"$GET_VOL" = x ]
	then
		VOL_GLOBAL[1]="/dev/mixer"
	else
		VOL_GLOBAL[1]="${GET_VOL#default_mixer=}"
	fi
	GET_VOL=`echo "$CURRENT_CONFIG" | grep -i default_channel`
	if [ x"$GET_VOL" = x ]
	then
		GLOBAL_VOL_DISPLAY[2]="Master";VOL_GLOBAL[2]="v"
	else
		VOL_GLOBAL[2]="${GET_VOL#default_channel=}"
		case ${GET_VOL#default_channel=} in
			"v")GLOBAL_VOL_DISPLAY[2]="Master";;
			"w")GLOBAL_VOL_DISPLAY[2]="PCM";;
			"l")GLOBAL_VOL_DISPLAY[2]="Line";;
			"m")GLOBAL_VOL_DISPLAY[2]="Mic";;
			"c")GLOBAL_VOL_DISPLAY[2]="CD";;
			"p")GLOBAL_VOL_DISPLAY[2]="PC Speaker";;
			"*")GLOBAL_VOL_DISPLAY[2]="User Configured";;
		esac
	fi
}
getMenuFormat ()
{
	DIALOG_CHECK=`echo "$CURRENT_CONFIG" | grep dialog`
	SOUND_CHECK=`echo "$CURRENT_CONFIG" | grep default_mixer`
	case ${DIALOG_CHECK:8:1} in
		"0")USE_DIALOGS=0;;
		"1")USE_DIALOGS=1;;
		"2")sysDialogs;;
	esac
	if [ x"$EXTENDED" = x ]
	then
		EXTENDED_CHECK=`echo "$CURRENT_CONFIG" | grep extended`
		EXTENDED=${EXTENDED_CHECK:9:1}
	fi
}
processInfo ()
{
	for ALL in "name" "prog" "prev" "next" "stop" "plps" "mixr" "chnl" "type"
	do
		GET_INFO=`echo "$2" | grep $ALL$1\ `
		APP_INFO[$APP_NUM$ENTRY]="${GET_INFO#$ALL$1 }"
		if [ "$ALL" = "type" ]
		then
			if [ x"${APP_INFO[$APP_NUM$ENTRY]}" = x ]
			then
				APP_INFO[$APP_NUM$ENTRY]="normal"
			fi
		fi
		let "ENTRY+=1"
	done
}
processAlts ()
{
	for ALL in "aprv" "anxt" "astp" "apls"
	do
		GET_INFO=`echo "$2" | grep $ALL$1\ `
		APP_ALTS[$APP_NUM$ENTRY]="${GET_INFO#$ALL$1 }"
		let "ENTRY+=1"
	done
}
processExtd ()
{
	for ALL in "1btn" "2btn" "3btn" "4btn" "5btn"
	do
		GET_INFO=`echo "$2" | grep $ALL$1\ `
		APP_EXTD[$APP_NUM$ENTRY]="${GET_INFO#$ALL$1 }"
		let "ENTRY+=1"
	done
}

# Menu functions
# These provide the main menu and editor menu.
mainMenu ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="mainMenu"
		MENU_COUNT=0
		until [ "$MENU_COUNT" = "$APP_COUNT" ]
		do
			let "MENU_COUNT+=1"
			ENTRY_TEXT[$MENU_COUNT]="${APP_INFO[$MENU_COUNT\1]}"
		done
		MENU_TEXT="Media-Detect Player and Options Menu"
		RETURN_TEXT="Go To Editor Menu"
		menuTemplate $APP_COUNT
		if [[ $OUTPUT -le $APP_COUNT ]]
		then
			MENU_CMD="${APP_INFO[$OUTPUT\2]#prog$OUTPUT }"
			$MENU_CMD &
			exit 0
		elif [ "$OUTPUT" = "$RETURN_OPTION" ]
		then
			editMenu
			OUTPUT=""
		fi
	done
}
editMenu ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="editMenu"
		MENU_TEXT="Media-Detect Editor Menu             "
		RETURN_TEXT="Go To Main Menu"
		ENTRY_TEXT=([1]="Use Application Editor" [2]="Edit Config File Manually" [3]="Restore Default Applications" [4]="Import Existing Config File" [5]="Global Settings")
		menuTemplate 5
		case $OUTPUT in
			"1")editAppSelect;OUTPUT="";;
			"2")manualEditor;OUTPUT="";;
			"3")restoreConfig;;
			"4")importConfig;;
			"5")globalSettings;OUTPUT="";;
			"6")OUTPUT="CANCEL";;
		esac
	done
}

# Config manipulation functions
# These functions rebuild the config file and import new configurations.
importConfig ()
{
	START_NUM=$APP_COUNT+1
	FILE_SELECT="Importing configuration file"
	openTemplate
	if ( cat $CHOOSE_FILE | grep -q plps )
	then
		NEW_COUNT=`cat "$CHOOSE_FILE" | grep -c plps`
		NEW_CONFIG=`cat "$CHOOSE_FILE"`
		APP_NUM=$APP_COUNT
		let "APP_COUNT=$APP_NUM+$NEW_COUNT"
		NEW_APP_NUM=0
		until [ "$APP_NUM" = "$APP_COUNT" ]
		do
			let "NEW_APP_NUM+=1"
			ENTRY=1
			let "APP_NUM+=1"
			processInfo $NEW_APP_NUM "$NEW_CONFIG"
			ENTRY=1
			processAlts $NEW_APP_NUM "$NEW_CONFIG"
			if [ "$EXTENDED" = "1" ]
			then
				ENTRY=1
				processExtd $NEW_APP_NUM "$NEW_CONFIG"
			fi
		done
		rebuildConfig
		OUTPUT="CANCEL"
	else
		ERROR_TEXT="The file \"$CHOOSE_FILE\" does not appear to be a valid configuration file."
		showError
		OUTPUT=""
	fi
}
rebuildConfig ()
{
	APP_NUM=0
	CONFIG_REBUILT=""
	until [ "$APP_NUM" = "$APP_COUNT" ]
	do
		ENTRY=1
		let "APP_NUM+=1"
		CONFIG_REBUILT="$CONFIG_REBUILT""[Application $APP_NUM]\n"
		for ALL in "name" "prog" "prev" "next" "stop" "plps" "mixr" "chnl"
		do
			CONFIG_REBUILT="$CONFIG_REBUILT$ALL$APP_NUM ${APP_INFO[$APP_NUM$ENTRY]}\n"
			let "ENTRY+=1"
		done
		ENTRY=1
		for ALL in "aprv" "anxt" "astp" "apls"
		do
			CONFIG_REBUILT="$CONFIG_REBUILT$ALL$APP_NUM ${APP_ALTS[$APP_NUM$ENTRY]}\n"
			let "ENTRY+=1"
		done
		if [ "$EXTENDED" = "1" ]
		then
			ENTRY=1
			for ALL in "1btn" "2btn" "3btn" "4btn" "5btn"
			do
				CONFIG_REBUILT="$CONFIG_REBUILT$ALL$APP_NUM ${APP_EXTD[$APP_NUM$ENTRY]}\n"
				let "ENTRY+=1"
			done
		fi
		CONFIG_REBUILT="${CONFIG_REBUILT}type$APP_NUM ${APP_INFO[$APP_NUM\9]}\n\n"
	done
	if [ -z "${VOL_GLOBAL[1]}" ]
	then
		VOL_GLOBAL[1]="/dev/mixer"
		VOL_GLOBAL[2]="v"
	fi
	echo -e "$CONFIG_REBUILT""[Global Configurations]\nextended=$EXTENDED\ndialogs=${DIALOG_CHECK:8:1}\ndefault_mixer=${VOL_GLOBAL[1]}\ndefault_channel=${VOL_GLOBAL[2]}" >> $TEMP_DIR/mdnew_$USER.tmp
	mv $TEMP_DIR/mdnew_$USER.tmp $CONFIG_FILE
	CURRENT_CONFIG=`cat $CONFIG_FILE`
}
manualEditor ()
{
	if [ "$USE_DIALOGS" = "1" ]
	then
		kwrite $CONFIG_FILE
	else
		xedit $CONFIG_FILE
	fi
	CURRENT_CONFIG=`cat $CONFIG_FILE`
	getMenuFormat
	getAppConfig
}
restoreConfig ()
{
	if [ -e "$CONFIG_FILE" ]
	then
		CONFIRM_TEXT="Restore default applications? (You will lose all current application configurations)"
	else
		CONFIRM_TEXT="This is the first time you've run Media-Detect.\nDo you want Media-Detect to generate the default application configurations? (Recommended)"
	fi
	confirmTemplate
	if [ $? = 0 ]
	then
		APP_INFO=([11]="xine Multimedia Player" [12]="xine" [13]="xine -S pl=prev" [14]="xine -S pl=next" [15]="xine -S stop" [16]="xine -S pause" [17]="default" [18]="default" [19]="type1 normal" [21]="CD Player" [22]="kscd" [23]="CDPlayer previous" [24]="CDPlayer next" [25]="CDPlayer stop" [26]="CDPlayer play" [27]="default" [28]="default" [29]="type2 dcop" [31]="Xmms Music Player" [32]="xmms" [33]="xmms --rew" [34]="xmms --fwd" [35]="xmms --stop" [36]="xmms --play-pause" [37]="default" [38]="default" [39]="type3 normal" [41]="Juk Music Player" [42]="juk" [43]="Player back" [44]="Player forward" [45]="Player stop" [46]="Player playPause" [47]="default" [48]="default" [49]="type4 dcop")
		APP_ALTS[14]="xine -S play"
		APP_COUNT=4
		rebuildConfig
		OUTPUT="CANCEL"
	else
		OUTPUT=""
	fi
}

# Application editor functions
# These functions are for adding, deleting, and editing application configurations.
editAppSelect ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="editAppSelect"
		MENU_COUNT=0
		until [ "$MENU_COUNT" = "$APP_COUNT" ]
		do
			let "MENU_COUNT+=1"
			ENTRY_TEXT[$MENU_COUNT]="${APP_INFO[$MENU_COUNT\1]}"
		done
		let "ADD_STANDARD=$MENU_COUNT+1"
		ENTRY_TEXT[$ADD_STANDARD]="Add New Standard Application"
		let "ADD_DCOP=$ADD_STANDARD+1"
		ENTRY_TEXT[$ADD_DCOP]="Add New DCOP Application"
		let "ADJUST_PRIORITY=$ADD_DCOP+1"
		ENTRY_TEXT[$ADJUST_PRIORITY]="Adjust Application Priority"
		MENU_TEXT="Application Editor\nPlease choose a configuration to edit or add a new one:"
		RETURN_TEXT="Return To Editor Menu"
		menuTemplate $ADJUST_PRIORITY
		if [ "$OUTPUT" != "CANCEL" ]
		then
			if [[ $OUTPUT -le $APP_COUNT ]]
			then
				APP_SELECTION="$OUTPUT"
				editApp
				OUTPUT=""
			else
				case $OUTPUT in
					"$ADD_STANDARD")newApp normal;OUTPUT="";;
					"$ADD_DCOP")newApp dcop;OUTPUT="";;
					"$ADJUST_PRIORITY")editPriority;OUTPUT="";;
					"$RETURN_OPTION")OUTPUT="CANCEL";;
				esac
			fi
		fi
	done
}
editApp ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="editApp"
		MENU_TEXT="Editing Configuration For ${APP_INFO[$APP_SELECTION\1]}        "
		ENTRY_TEXT=([1]="Change Display Name" [2]="Edit Basic Commands" [3]="Edit Extended Commands" [4]="Edit Volume Controls" [5]="Edit Alternate Commands" [6]="Rebuild Entire Configuration" [7]="Delete ${APP_INFO[$APP_SELECTION\1]} Configuration")
		RETURN_TEXT="Return To Application Selection"
		menuTemplate 7
		case $OUTPUT in
			"1")editDisplay;OUTPUT="";;
			"2")editBasic;OUTPUT="";;
			"3")editExtended;OUTPUT="";;
			"4")editAppVolume;OUTPUT="";;
			"5")editAlternates;OUTPUT="";;
			"6")editAppWhole;OUTPUT="";;
			"7")delApp;;
			"8")OUTPUT="CANCEL";;
			"9")exit 0;;
		esac
		if [ "$OUTPUT" != "CANCEL" ]
		then
			rebuildConfig
			messageTemplate
		fi
	done
}
editAppWhole ()
{
	CONFIRM_TEXT="Change Display Name?"
	confirmTemplate
	if [ $? = 0 ]
	then
		editDisplay
	fi
	CONFIRM_TEXT="Edit Basic Commands?"
	confirmTemplate
	if [ $? = 0 ]
	then
		editBasic
	fi
	if [ "$EXTENDED" = "1" ]
	then
		CONFIRM_TEXT="Edit Extended Commands?"
		confirmTemplate
		if [ $? = 0 ]
		then
			editExtended
		fi
	fi
	CONFIRM_TEXT="Edit Volume Controls?"
	confirmTemplate
	if [ $? = 0 ]
	then
		editAppVolume
	fi
	CONFIRM_TEXT="Edit Alternate Commands?"
	confirmTemplate
	if [ $? = 0 ]
	then
		editAlternates
	fi
	MESSAGE_TEXT="Finished Updating Configuration For ${APP_INFO[$APP_SELECTION\1]}"
}
editDisplay ()
{
	INPUT_TEXT="Application Display Name:"
	DEFAULT_INPUT="${APP_INFO[$APP_SELECTION\1]}"
	inputTemplate
	if [ "$OUTPUT" != "CANCEL" ]
	then
		APP_INFO[$APP_SELECTION\1]="$OUTPUT"
		MESSAGE_TEXT="Finished updating Display Name."
	fi
}
editBasic ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		BASIC_ENTRY=2
		for ALL in "Application Start Command" "Rewind/Previous Command" "Fast-forward Command" "Stop Command" "Play/Pause Command"
		do
			INPUT_TEXT="$ALL"
			DEFAULT_INPUT="${APP_INFO[$APP_SELECTION$BASIC_ENTRY]}"
			inputTemplate
			if [ "$OUTPUT" != "CANCEL" ]
			then
				APP_INFO[$APP_SELECTION$BASIC_ENTRY]="$OUTPUT"
			else
				break
			fi
			let "BASIC_ENTRY+=1"
		done
		MESSAGE_TEXT="Finished updating Basic commands."
		OUTPUT="CANCEL"
	done
}
editAlternates ()
{
	OUTPUT=""
	until [ "$OUTPUT" = "CANCEL" ]
	do
		COUNTER=3
		for ALL in "1" "2" "3" "4"
		do
			if [ x"${APP_ALTS[$APP_SELECTION$ALL]}" = x ]
			then
				ENTRY_TEXT[$ALL]="${APP_INFO[$APP_SELECTION$COUNTER]}"
			else
				ENTRY_TEXT[$ALL]="${APP_INFO[$APP_SELECTION$COUNTER]}     (Current Alternate: ${APP_ALTS[$APP_SELECTION$ALL]})"
			fi
			let "COUNTER+=1"
		done
		MENU_TEXT="Please choose one of the following commands to modify:"
		RETURN_TEXT="Return To Application Editor"
		menuTemplate 4
		if [ "$OUTPUT" != "CANCEL" -a "$OUTPUT" != "5" ]
		then
			COMMAND_NUM="$OUTPUT"
			INPUT_TEXT="Please enter your Alternate command. (Or enter \"None\" to leave blank)"
			DEFAULT_INPUT=${APP_ALTS[$APP_SELECTION$COMMAND_NUM]}
			if [ x"$DEFAULT_INPUT" = x ]
			then
				DEFAULT_INPUT="None"
			fi
			inputTemplate
			if [ $? = 0 ]
			then
				case "$OUTPUT" in
					"None")APP_ALTS[$APP_SELECTION$COMMAND_NUM]="";;
					*)APP_ALTS[$APP_SELECTION$COMMAND_NUM]="$OUTPUT";;
				esac
				rebuildConfig
			fi
		fi
		if [ "$OUTPUT" = "5" ]
		then
			OUTPUT="CANCEL"
		fi
	done
	MESSAGE_TEXT="Finished Updating Alternate Commands."
}
editExtended ()
{
	if [ "$EXTENDED" = "0" ]
	then
		extendedSet
	fi
	if [ "$EXTENDED" = "1" ]
	then
		until [ "$OUTPUT" = "CANCEL" ]
		do
			for ALL in "1" "2" "3" "4" "5"
			do
				INPUT_TEXT="Extended Command $ALL"
				DEFAULT_INPUT="${APP_EXTD[$APP_SELECTION$ALL]}"
				if [ x"$DEFAULT_INPUT" = x ]
				then
					DEFAULT_INPUT="None"
				fi
				inputTemplate
				if [ "$OUTPUT" != "CANCEL" ]
				then
					APP_EXTD[$APP_SELECTION$ALL]="$OUTPUT"
					if [ "${APP_EXTD[$APP_SELECTION$ALL]}" = "None" ]
					then
						APP_EXTD[$APP_SELECTION$ALL]=""
					fi
				else
					break
				fi
			done
			MESSAGE_TEXT="Finished updating Extended commands."
			OUTPUT="CANCEL"
		done
	fi
}
editAppVolume ()
{
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="editAppVolume"
		MENU_TEXT="Application Volume Settings                "
		RETURN_TEXT="Return To Application Editor"
		ENTRY_TEXT=([1]="Set Mixer" [2]="Set Channel")
		menuTemplate 2
		if [ "$OUTPUT" != "CANCEL" ]
		then
			 case $OUTPUT in
				 "1")setAppMixer;OUTPUT="";;
				 "2")setAppChannel;OUTPUT="";;
				 "3")OUTPUT="CANCEL";;
			 esac
		fi
		MESSAGE_TEXT="Finished Updating Application Volume Settings"
	 done
 }
setAppMixer ()
{
	CONFIRM_TEXT="Current Mixer: ${APP_INFO[$APP_SELECTION\7]}\nDo you want to change this setting?"
	confirmTemplate
	if [ $? = 0 ]
	then
		INPUT_TEXT="New Mixer (Enter \"default\" to use the default mixer"
		DEFAULT_INPUT="${APP_INFO[$APP_SELECTION\7]}"
		inputTemplate
		if [ "$OUTPUT" != "CANCEL" ]
		then
			APP_INFO[$APP_SELECTION\7]="$OUTPUT"
		fi
	fi
}
setAppChannel ()
{
	case ${APP_INFO[$APP_SELECTION\8]} in
		"default")APP_VOL_DISPLAY="Use Default Channel";;
		"v")APP_VOL_DISPLAY="Master";;
		"w")APP_VOL_DISPLAY="PCM";;
		"l")APP_VOL_DISPLAY="Line";;
		"m")APP_VOL_DISPLAY="Mic";;
		"c")APP_VOL_DISPLAY="CD";;
		"p")APP_VOL_DISPLAY="PC Speaker";;
		*)APP_VOL_DISPLAY="User Defined";;
	esac
	FUNCTION="setAppChannel"
	CONFIRM_TEXT="Current Channel: $APP_VOL_DISPLAY\nDo you want to change this setting?"
	confirmTemplate
	if [ $? = 0 ]
	then
		MENU_TEXT="Set Channel (Currently: $APP_VOL_DISPLAY)"
		RETURN_TEXT="Return To Application Editor"
		ENTRY_TEXT=([1]="Use Default" [2]="Master" [3]="PCM" [4]="Line" [5]="Mic" [6]="CD" [7]="PC Speaker" [8]="Other...")
		menuTemplate 8
		if [ "$OUTPUT" = "8" ]
		then
			INPUT_TEXT="Please enter the desired channel (uses aumix channel options):"
			DEFAULT_INPUT=""
			inputTemplate
			if [ "$OUTPUT" != "CANCEL" ]
			then
				APP_VOL_DISPLAY="User Defined"
				APP_INFO[$APP_SELECTION\8]="$OUTPUT"
			fi
		else
			case $OUTPUT in
				"1")APP_VOL_DISPLAY="Use Default Channel";APP_INFO[$APP_SELECTION\8]="default";OUTPUT="CANCEL";;
				"2")APP_VOL_DISPLAY="Master";APP_INFO[$APP_SELECTION\8]="v";OUTPUT="CANCEL";;
				"3")APP_VOL_DISPLAY="PCM";APP_INFO[$APP_SELECTION\8]="w";OUTPUT="CANCEL";;
				"4")APP_VOL_DISPLAY="Line";APP_INFO[$APP_SELECTION\8]="l";OUTPUT="CANCEL";;
				"5")APP_VOL_DISPLAY="Mic";APP_INFO[$APP_SELECTION\8]="m";OUTPUT="CANCEL";;
				"6")APP_VOL_DISPLAY="CD";APP_INFO[$APP_SELECTION\8]="c";OUTPUT="CANCEL";;
				"7")APP_VOL_DISPLAY="PC Speaker";APP_INFO[$APP_SELECTION\8]="p";OUTPUT="CANCEL";;
				"CANCEL")OUTPUT="CANCEL";;
			esac
		fi
	fi
}
editPriority ()
{
	OUTPUT=""
	until [ "$OUTPUT" = "CANCEL" ]
	do
		FUNCTION="editPriority"
		MENU_COUNT=0
		until [ "$MENU_COUNT" = "$APP_COUNT" ]
		do
			let "MENU_COUNT+=1"
			ENTRY_TEXT[$MENU_COUNT]="${APP_INFO[$MENU_COUNT\1]}"
		done
		RETURN_TEXT="Return To Application Editor"
		CHECKLIST_TEXT="Select two applications to switch their positions."
		checklistTemplate $MENU_COUNT
		if [ "$OUTPUT" != "CANCEL" ]
		then
			for ALL in "1" "2" "3" "4" "5" "6" "7" "8" "9"
			do
				TMP_INFO[$OUTPUT_ONE$ALL]=${APP_INFO[$OUTPUT_ONE$ALL]}
				APP_INFO[$OUTPUT_ONE$ALL]=${APP_INFO[$OUTPUT_TWO$ALL]}
				APP_INFO[$OUTPUT_TWO$ALL]=${TMP_INFO[$OUTPUT_ONE$ALL]}
			done
			for ALL in "1" "2" "3" "4"
			do
				TMP_ALTS[$OUTPUT_ONE$ALL]=${APP_ALTS[$OUTPUT_ONE$ALL]}
				APP_ALTS[$OUTPUT_ONE$ALL]=${APP_ALTS[$OUTPUT_TWO$ALL]}
				APP_ALTS[$OUTPUT_TWO$ALL]=${TMP_ALTS[$OUTPUT_ONE$ALL]}
			done
			for ALL in "1" "2" "3" "4" "5"
			do
				TMP_EXTD[$OUTPUT_ONE$ALL]=${APP_EXTD[$OUTPUT_ONE$ALL]}
				APP_EXTD[$OUTPUT_ONE$ALL]=${APP_EXTD[$OUTPUT_TWO$ALL]}
				APP_EXTD[$OUTPUT_TWO$ALL]=${TMP_EXTD[$OUTPUT_ONE$ALL]}
			done
			rebuildConfig
			OUTPUT="CANCEL"
		fi
	done
}
newApp ()
{
	case $1 in
		"dcop")NEW_TEXT=([1]="Application Display Name (Example: Juk Music Player" [2]="Application Start Command/DCOP Search String (Example: juk)" [3]="DCOP Rewind/Previous Command (Example: Player back)" [4]="DCOP Forward/Next Command (Example: Player forward)" [5]="DCOP Stop Command (Example: Player stop)" [6]="DCOP Play/Pause Command (Example: Player playPause)" [7]="Audio Mixer Device (Example: /dev/mixer or \"default\" for global default)" [8]="Audio Mixer Channel");;
		"normal")NEW_TEXT=([1]="Application display name (Example: Xmms Music Player)" [2]="Application start command (Example: xmms)" [3]="Application rewind/previous command (Example: xmms --rew)" [4]="Application forward/next command (Example: xmms --fwd)" [5]="Application stop command (Example: xmms --stop)" [6]="Application play/pause command (Example: xmms --play-pause)" [7]="Audio Mixer Device (Example: /dev/mixer or \"default\" for global default)" [8]="Audio Mixer Channel");;
	esac
	for ALL in "1" "2" "3" "4" "5" "6"
	do
		INPUT_TEXT="${NEW_TEXT[$ALL]}"
		DEFAULT_INPUT=""
		inputTemplate
		if [ "$OUTPUT" = "CANCEL" -o x"$OUTPUT" = x ]
		then
			break
		else
			NEW_INFO[$ALL]="$OUTPUT"
		fi
	done
	if [ "$OUTPUT" != "CANCEL" -a "$EXTENDED" = "1" ]
	then
		CONFIRM_TEXT="Extended Mode is enabled.\nWould you like to add up to 5 additional commands?"
		confirmTemplate
		if [ $? = 0 ]
		then
			for ALL in "1" "2" "3" "4" "5"
			do
				INPUT_TEXT="Extended Command $ALL:"
				DEFAULT_INPUT="None"
				inputTemplate
				if [ "$OUTPUT" = "None" ]
				then
					NEW_EXTD[$ALL]=""
				elif [ "$OUTPUT" = "CANCEL" ]
				then
					break
				else
					NEW_EXTD[$ALL]="$OUTPUT"
				fi
			done
		fi
	fi				
	if [ "$OUTPUT" != "CANCEL" ]
	then
		INPUT_TEXT="${NEW_TEXT[7]}"
		DEFAULT_INPUT="default"
		inputTemplate
		if [ "$OUTPUT" = "CANCEL" ]
		then
			:
		else
			NEW_INFO[7]="$OUTPUT"
		fi
	fi
	if [ "$OUTPUT" != "CANCEL" ]
	then
		MENU_TEXT="${NEW_TEXT[8]}"
		RETURN_TEXT="Cancel"
		ENTRY_TEXT=([1]="Use Default Channel" [2]="Master" [3]="PCM" [4]="Line" [5]="Mic" [6]="CD" [7]="PC Speaker" [8]="User Defined")
		menuTemplate 8
		if [ "$OUTPUT" = "CANCEL" ]
		then
			break
		elif [ "$OUTPUT" = "8" ]
		then
			INPUT_TEXT="Please enter the desired channel (uses aumix channel options):"
			DEFAULT_INPUT=""
			inputTemplate
			if [ "$OUTPUT" = "CANCEL" ]
			then
				break
			else
				APP_VOL_DISPLAY="User Defined"
				NEW_INFO[8]="$OUTPUT"
			fi
		else
			case $OUTPUT in
				"1")APP_VOL_DISPLAY="Use Default Channel";NEW_INFO[8]="default";;
				"2")APP_VOL_DISPLAY="Master";NEW_INFO[8]="v";;
				"3")APP_VOL_DISPLAY="PCM";NEW_INFO[8]="w";;
				"4")APP_VOL_DISPLAY="Line";NEW_INFO[8]="l";;
				"5")APP_VOL_DISPLAY="Mic";NEW_INFO[8]="m";;
				"6")APP_VOL_DISPLAY="CD";NEW_INFO[8]="c";;
				"7")APP_VOL_DISPLAY="PC Speaker";NEW_INFO[8]="p";;
			esac
		fi
	fi
	if [ "$OUTPUT" != "CANCEL" ]
	then
		let "APP_COUNT+=1"
		for ALL in "1" "2" "3" "4" "5" "6" "7" "8"
		do
			APP_INFO[$APP_COUNT$ALL]="${NEW_INFO[$ALL]}"
		done
		if [ "$EXTENDED" = "1" ]
		then
			for ALL in "1" "2" "3" "4" "5"
			do
				APP_EXTD[$APP_COUNT$ALL]="${NEW_EXTD[$ALL]}"
			done
		fi
		APP_INFO[$APP_COUNT\9]="$1"
		rebuildConfig		
		CONFIRM_TEXT="Finished Adding New Application ${APP_INFO[$APP_COUNT\1]}.\nWould you like to add Alternate Commands?"
		confirmTemplate
		if [ $? = 0 ]
		then
			APP_SELECTION="$APP_COUNT"
			editAlternates
			editApp
			OUTPUT="CANCEL"
		fi
	fi
}
delApp ()
{
	CONFIRM_TEXT="Are you sure?"
	confirmTemplate
	if [ $? = 0 ]
	then
		APP_NUM="$APP_SELECTION"
		DEL_NUM="$APP_SELECTION"
		until [ "$APP_NUM" = "$APP_COUNT" ]
		do
			ENTRY=1
			let "APP_NUM+=1"
			for ALL in "1" "2" "3" "4" "5" "6" "7" "8" "9"
			do
				APP_INFO[$DEL_NUM$ALL]=${APP_INFO[$APP_NUM$ALL]}
			done
			for ALL in "1" "2" "3" "4"
			do
				APP_ALTS[$DEL_NUM$ALL]=${APP_ALTS[$APP_NUM$ALL]}
			done
			if [ "$EXTENDED" = "1" ]
			then
				for ALL in "1" "2" "3" "4" "5"
				do
					APP_EXTD[$DEL_NUM$ALL]=${APP_EXTD[$APP_NUM$ALL]}
				done
			fi
		done
		let "APP_COUNT-=1"
		rebuildConfig
		MESSAGE_TEXT="Application Configuration Deleted."
		messageTemplate
		OUTPUT="CANCEL"
	else
		MESSAGE_TEXT="Deletion Aborted."
		OUTPUT=""
	fi
}

# System startup
if [ -e "$CONFIG_FILE" ]
then
	CURRENT_CONFIG=`cat $CONFIG_FILE`
	getMenuFormat
	getAppConfig
else
	EXTENDED=0
	DIALOG_CHECK="        2"
	USE_DIALOGS=1
	VOL_GLOBAL=([1]="/dev/mixer" [2]="v")
	restoreConfig
	if [ x"$OUTPUT" = x ]
	then
		MESSAGE_TEXT="Warning: Starting Media-Detect menu without any configured applications!"
		messageTemplate
	fi
fi

case $1 in
	"menu")mainMenu;;
	"edit")editMenu;;
	"")mainMenu;;
	*)MESSAGE_TEXT="$1 is not a valid option.";messageTemplate;;
esac
exit 0
