# Auxiliary functions for menu system for custom build system
# Copyright (c) 2002 Serge van den Boom
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
#  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

MENU_ITEM_TYPES="MENU CHOICE INPUT"

MENU_ITEM_TYPE_MENU_INIT=menu_init_menu
MENU_ITEM_TYPE_MENU_PRINT_VALUE=menu_print_value_menu
MENU_ITEM_TYPE_MENU_HANDLER=menu_handle_menu
MENU_ITEM_TYPE_MENU_SAVE=menu_save_menu
MENU_ITEM_TYPE_MENU_PROCESS=menu_process_menu

MENU_ITEM_TYPE_CHOICE_INIT=menu_init_choice
MENU_ITEM_TYPE_CHOICE_PRINT_VALUE=menu_print_value_choice
MENU_ITEM_TYPE_CHOICE_HANDLER=menu_handle_choice
MENU_ITEM_TYPE_CHOICE_SAVE=menu_save_choice
MENU_ITEM_TYPE_CHOICE_PROCESS=menu_process_choice

MENU_ITEM_TYPE_INPUT_INIT=menu_init_input
MENU_ITEM_TYPE_INPUT_PRINT_VALUE=menu_print_value_input
MENU_ITEM_TYPE_INPUT_HANDLER=menu_handle_input
MENU_ITEM_TYPE_INPUT_SAVE=menu_save_input
MENU_ITEM_TYPE_INPUT_PROCESS=menu_process_input


# Description: Check if a string can be used as a path.
#              No checks are done if the path really exists.
# Arguments:   $1 - the string to check
# Returns:     0 if the string makes a valid path
#              1 if the string doesn't make a valid path
# Outputs:     the path, possibly modified
validate_path() {
	if [ "$1" = "/" ]; then
		echo "/"
		return
	fi
	cat << EOF
${1%/}
EOF
	return 0
}

# Description: Initialise a menu, setting unset values to default
# Arguments:   $1 - the type of menu item
#              $2 - the name of the menu item
menu_init() {
	local INIT_FUN

	eval INIT_FUN="\$MENU_ITEM_TYPE_$1_INIT"
	"$INIT_FUN" "$2"
}

# Description: Initialise this menu
# Arguments:   $1 - the name of the menu
menu_init_menu() {
	local MENU TEMP_ITEMS ITEM TEMP_TYPE

	MENU="$1"
	eval TEMP_ITEMS="\$MENU_${MENU}_ITEMS"
	for ITEM in $TEMP_ITEMS; do
		eval TEMP_TYPE="\$MENU_${MENU}_ITEM_${ITEM}_TYPE"
		menu_init "$TEMP_TYPE" "$ITEM"
	done
}

# Description: Check if this choice is available
# Arguments:   $1 - the name of the choice menu
#              $2 - the name of the choice
# Returns:     0 - if the choice is available
#              1 - if the choice is not available
menu_have_choice() {
	local MENU CHOICE TEMP_VALID TEMP_PRECOND
	MENU="$1"
	CHOICE="$2"
	eval TEMP_VALID="\$CHOICE_${MENU}_OPTION_${CHOICE}_VALID"
	if [ -n "$TEMP_VALID" ]; then
		return "$TEMP_VALID"
	fi
	eval TEMP_PRECOND="\$CHOICE_${MENU}_OPTION_${CHOICE}_PRECOND"
	if [ -z "$TEMP_PRECOND" ] || $TEMP_PRECOND; then
		eval "CHOICE_${MENU}_OPTION_${CHOICE}_VALID"=0
		return 0
	fi
	eval "CHOICE_${MENU}_OPTION_${CHOICE}_VALID"=1
	return 1
}

# Description: Initialise this choice menu
# Arguments:   $1 - the name of the choice menu
menu_init_choice() {
	local MENU TEMP_VALUE TEMP_DEFAULT TEMP_OPTIONS OPTION TEMP_TITLE
	MENU="$1"
	eval TEMP_VALUE="\$CHOICE_${MENU}_VALUE"
	eval TEMP_DEFAULT="\$CHOICE_${MENU}_DEFAULT"
	eval TEMP_OPTIONS="\$CHOICE_${MENU}_OPTIONS"
	for OPTION in $TEMP_VALUE $TEMP_DEFAULT $TEMP_OPTIONS; do
		if menu_have_choice "$MENU" "$OPTION"; then
			eval CHOICE_${MENU}_VALUE="$OPTION"
			eval CHOICE_${MENU}_OLD_VALUE="$OPTION"
			return
		fi
	done
	eval TEMP_VALUE="\$CHOICE_${MENU}_TITLE"
	echo "Error: No option for '$TEMP_VALUE' is available for your system."
	exit 1
}

# Description: Initialise this input menu
# Arguments:   $1 - the name of the input menu
menu_init_input() {
	local MENU TEMP_VALUE TEMP_DEFAULT
	MENU="$1"
	eval TEMP_VALUE="\$INPUT_${MENU}_VALUE"
	if [ -z "$TEMP_VALUE" ]; then
		eval TEMP_VALUE="\$INPUT_${MENU}_DEFAULT"
		eval INPUT_${MENU}_VALUE="\$TEMP_VALUE"
	fi
	eval INPUT_${MENU}_OLD_VALUE="\$TEMP_VALUE"
}

# Description: Print the string describing the value of a menu item.
# Arguments:   $1 - the type of menu item
#              $2 - the name of the menu item
# Outputs:     The string describing the value of the menu item
menu_print_value() {
	local PRINT_VALUE

	eval PRINT_VALUE="\$MENU_ITEM_TYPE_$1_PRINT_VALUE"
	"$PRINT_VALUE" "$2"
}

# Description: Print the string describing the value this menu
# Arguments:   $1 - the name of the menu item
# Outputs:     The string describing the value of this menu
menu_print_value_menu() {
	echo "[...]"
}

# Description: Print the string describing the value this choice menu
# Arguments:   $1 - the name of the choice menu item
# Outputs:     The string describing the value of this choice menu
menu_print_value_choice() {
	local TEMP_VALUE TEMP_TITLE
	eval TEMP_VALUE="\$CHOICE_$1_VALUE"
	eval TEMP_TITLE=\"\$CHOICE_$1_OPTION_${TEMP_VALUE}_TITLE\"
	cat << EOF
$TEMP_TITLE
EOF
}

# Description: Print the value of this input menu
# Arguments:   $1 - the name of the input menu item
# Outputs:     The value of the given input menu
menu_print_value_input() {
	local TEMP_VALUE TEMP_TITLE
	eval TEMP_VALUE="\$INPUT_$1_VALUE"
	cat << EOF
$TEMP_VALUE
EOF
}

# Description: Print the string describing the value of a menu item.
# Arguments:   $1 - the type of menu item
#              $2 - the name of the menu item
# Outputs:     The string describing the value of the menu item
menu_handle() {
	local HANDLER

	eval HANDLER="\$MENU_ITEM_TYPE_$1_HANDLER"
	"$HANDLER" "$2"
}

# Description: Process a menu-type menu item
# Arguments:   $1 - The name of the menu
menu_handle_menu() {
	local TEMP_ITEMS I CHOICE NUM_ITEMS TEMP_TYPE MENU ITEM TEMP_TITLE
	eval TEMP_ITEMS="\$MENU_$1_ITEMS"
	
	MENU="$1"
	while :; do
		echo
		eval TEMP_TITLE="\$MENU_${MENU}_TITLE"
		cat << EOF
  $ANSI_BOLD-= $TEMP_TITLE =-$ANSI_NORMAL
EOF
		I=0
		for ITEM in $TEMP_ITEMS; do
			I=$(($I + 1))
			eval TEMP_TYPE="\$MENU_${MENU}_ITEM_${ITEM}_TYPE"
			eval TEMP_TITLE="\$${TEMP_TYPE}_${ITEM}_TITLE"
			printf "  %i. %-36s %s\n" $I "$TEMP_TITLE" \
					"$(menu_print_value $TEMP_TYPE $ITEM)"
		done
		NUM_ITEMS="$I"

		echo
		echo "Press a number plus <ENTER> if you want to change something, "
		echo -n "or just <ENTER> if everything is ok: "
		read CHOICE

		# Check if the choice was empty
		if [ -z "$CHOICE" ]; then
			# We're done
			return
		fi

		# Check if what the user entered was a number
		egrep -q "^[0-9]+$" << EOF
$CHOICE
EOF
		if [ $? -ne 0 ]; then
			echo "Invalid choice."
			continue
		fi

		# Check if the number the user entered if valid
		if [ "$CHOICE" -lt 1 -o "$CHOICE" -gt "$NUM_ITEMS" ]; then
			echo "Invalid choice."
			continue
		fi

		# Now look up the choice
		I=0
		for ITEM in $TEMP_ITEMS; do
			I=$(($I + 1))
			if [ "$I" -eq "$CHOICE" ]; then
				eval TEMP_TYPE="\$MENU_${MENU}_ITEM_${ITEM}_TYPE"
				menu_handle "$TEMP_TYPE" "$ITEM"
				break
			fi
		done
	done
}

# Description: Process a choice-type menu item
# Arguments:   $1 - The name of the menu
menu_handle_choice() {
	local TEMP_OPTIONS I CHOICE NUM_OPTIONS TEMP_TYPE MENU OPTION \
			TEMP_VALUE TEMP_TITLE SELECTED
	eval TEMP_OPTIONS="\$CHOICE_$1_OPTIONS"
	
	MENU="$1"
	while :; do
		echo
		eval TEMP_TITLE="\$CHOICE_${MENU}_TITLE"
		cat << EOF
  $ANSI_BOLD-= $TEMP_TITLE =-$ANSI_NORMAL
EOF
		eval TEMP_VALUE="\$CHOICE_${MENU}_VALUE"

		# Check in advance which options are present, so that that
		# is echoed before the menu is printed.
		# menu_have_choice caches results.
		# Also, count the number of options
		I=0
		for OPTION in $TEMP_OPTIONS; do
			menu_have_choice "$MENU" "$OPTION"
		done
		NUM_OPTIONS="$I"

		I=0
		for OPTION in $TEMP_OPTIONS; do
			I=$(($I + 1))
			eval TEMP_TITLE="\$CHOICE_${MENU}_OPTION_${OPTION}_TITLE"
			if [ "$TEMP_VALUE" = "$OPTION" ]; then
				SELECTED="-->"
			else
				SELECTED="   "
			fi
			if menu_have_choice "$MENU" "$OPTION"; then
				printf "  %*i. %s %s\n" "${#NUM_OPTIONS}" "$I" \
						"$SELECTED" "$TEMP_TITLE"
			else
				printf "  %-*s  %s (N/A) %s\n" "${#NUM_OPTIONS}" "-" \
						"$SELECTED" "$TEMP_TITLE"
			fi
		done

		echo
		echo "Select the option you want by typing a number plus <ENTER>"
		echo -n "or just <ENTER> if everything is ok: "
		read CHOICE
		echo

		# Check if the choice was empty
		if [ -z "$CHOICE" ]; then
			# We're done
			return
		fi

		# Check if what the user entered was a number
		egrep -q "^[0-9]+$" << EOF
$CHOICE
EOF
		if [ $? -ne 0 ]; then
			echo "Invalid choice."
			continue
		fi

		# Check if the number the user entered if valid
		if [ "$CHOICE" -lt 1 -o "$CHOICE" -gt "$NUM_ITEMS" ]; then
			echo "Invalid choice."
			continue
		fi

		# Now look up the choice
		I=0
		for OPTION in $TEMP_OPTIONS; do
			I=$(($I + 1))
			if [ "$I" -eq "$CHOICE" ]; then
				if menu_have_choice "$MENU" "$OPTION"; then
					eval "CHOICE_${MENU}_VALUE"="$OPTION"
					return
				else
					echo "That option is unavailable on your system."
				fi
			fi
		done
	done
}

# Description: Process an input-type menu item
# Arguments:   $1 - The name of the menu
menu_handle_input() {
	local ITEM TEMP_TITLE TEMP_VALUE TEMP_DEFAULT NEW_VALUE TEMP_VALIDATOR

	ITEM="$1"
	eval TEMP_TITLE="\$INPUT_${ITEM}_TITLE"
	while :; do
		echo
		cat << EOF
  $ANSI_BOLD-= $TEMP_TITLE =-$ANSI_NORMAL
EOF
		eval TEMP_VALUE="\$INPUT_${ITEM}_VALUE"
		eval TEMP_DEFAULT="\$INPUT_${ITEM}_DEFAULT"
		echo "  Default value: $TEMP_DEFAULT"
		echo "  Current value: $TEMP_VALUE"
		echo -n "  New value:     "
		read NEW_VALUE
		
		# If no new value is entered, keep the old one.
		if [ -z "$NEW_VALUE" ]; then
			return
		fi

		# If a validator function is present, validate the new value
		eval TEMP_VALIDATOR="\$INPUT_${ITEM}_VALIDATOR"
		if [ -n "$TEMP_VALIDATOR" ]; then
			NEW_VALUE=`$TEMP_VALIDATOR "$NEW_VALUE"`
			if [ $? -ne 0 ]; then
				echo "Invalid value"
				continue
			fi
		fi
		break
	done
	eval "INPUT_${ITEM}_VALUE"=\"\$NEW_VALUE\"
}

# Description: echo the current state of a menu item in a form that can be
#              executed to restore the state.
# Arguments:   $1 - the type of menu item
#              $2 - the name of the menu item
# Outputs:     The string describing the value of the menu item
menu_save() {
	local SAVE_FUN
	eval SAVE_FUN="\$MENU_ITEM_TYPE_$1_SAVE"
	"$SAVE_FUN" "$2"
}

# Description: echo the current state of a menu in a form that can be
#              executed to restore the state.
# Arguments:   $1 - the name of the menu
# Outputs:     The string describing the value of the menu
menu_save_menu() {
	local MENU TEMP_ITEMS ITEM TEMP_TYPE

	MENU="$1"
	eval TEMP_ITEMS="\$MENU_${MENU}_ITEMS"
	for ITEM in $TEMP_ITEMS; do
		eval TEMP_TYPE="\$MENU_${MENU}_ITEM_${ITEM}_TYPE"
		menu_save "$TEMP_TYPE" "$ITEM"
	done	
}

# Description: echo the current state of a choice menu in a form that can be
#              executed to restore the state.
# Arguments:   $1 - the name of the choice menu
# Outputs:     The string describing the value of the choice menu
menu_save_choice() {
	local MENU TEMP_VALUE
	MENU="$1"
	eval TEMP_VALUE="\$CHOICE_${MENU}_VALUE"
	cat << EOF
CHOICE_${MENU}_VALUE='$TEMP_VALUE'
EOF
}

# Description: echo the current state of an input menu in a form that can be
#              executed to restore the state.
# Arguments:   $1 - the name of the input menu
# Outputs:     The string describing the value of the input menu
menu_save_input() {
	local MENU TEMP_VALUE
	MENU="$1"
	eval TEMP_VALUE="\$INPUT_${MENU}_VALUE"
	cat << EOF
INPUT_${MENU}_VALUE='$TEMP_VALUE'
EOF
}

# Description: Perform the actions associated with the choice made for a
#              menu items.
# Arguments:   $1 - the type of menu item
#              $2 - the name of the menu item
menu_process() {
	local PROCESS_FUN
	eval PROCESS_FUN="\$MENU_ITEM_TYPE_$1_PROCESS"
	"$PROCESS_FUN" "$2"
}

# Description: Perform the actions associated with the chosen menu items
#              for a menu.
# Arguments:   $1 - the name of the menu
menu_process_menu() {
	local MENU TEMP_ITEMS ITEM TEMP_TYPE

	MENU="$1"
	eval TEMP_ITEMS="\$MENU_${MENU}_ITEMS"
	for ITEM in $TEMP_ITEMS; do
		eval TEMP_TYPE="\$MENU_${MENU}_ITEM_${ITEM}_TYPE"
		menu_process "$TEMP_TYPE" "$ITEM"
	done	
}

# Description: Perform the actions associated with the choice made for
#              a choice menu.
# Arguments:   $1 - the name of the choice menu
menu_process_choice() {
	local MENU TEMP_VALUE TEMP_ACTION
	MENU="$1"
	eval TEMP_VALUE="\$CHOICE_${MENU}_VALUE"
	eval TEMP_ACTION="\$CHOICE_${MENU}_OPTION_${TEMP_VALUE}_ACTION"
	if [ -n "$TEMP_ACTION" ]; then
		$TEMP_ACTION
	fi
}

# Description: Perform the actions associated with the input menu.
# Arguments:   $1 - the name of the input menu
menu_process_input() {
	# Nothing to do
	:
}

do_menu() {
	if [ -e config.state ]; then
		. config.state
	fi
	menu_init MENU "$@"
	menu_handle MENU "$@"
	echo
	echo "Saving configuration..."
	menu_save MENU "$@" > config.state
	echo "Propagating configuration..."
	menu_process MENU "$@"
	echo "Configuration complete."
}

