MENU_main_ITEMS="components install_prefix"
MENU_main_TITLE="The Ur-Quan Masters installation"
MENU_main_ITEM_components_TYPE=MENU
MENU_main_ITEM_install_prefix_TYPE=INPUT

MENU_components_TITLE="Optional components"
MENU_components_TEXT=\
"  Here you can select which components you'd like to have installed. If you
  can spare the space, you are adviced to select all, for the maximum game
  experience.
"

package_filename() {
	local FILENAME PACKNAME

	eval PACKNAME="\${UQM_PACKAGE_$1_NAME}"
	# Check complete package first for reinstall or downgrade.
	# If they're not found, we'll settle for an upgrade package,
	# maybe it will work, maybe it won't.
	case "$UQM_INSTALL" in
		NEW|REINSTALL|DOWNGRADE)
			FILENAME="${CONTENT_PATH}uqm-${UQM_VERSION}-$PACKNAME.zip"
			if [ -f "$FILENAME" ]; then
				# Found complete package.
				echo "$FILENAME"
				return 0
			fi
			;;
	esac

	# A full package is needed for a new install
	if [ "$UQM_INSTALL" = NEW ]; then
		return 1
	fi

	# Check for upgrade package.
	FILENAME="${CONTENT_PATH}uqm-${UQM_OLDVERSION}_to_${UQM_VERSION}-$PACKNAME.zip"
	if [ -f "$FILENAME" ]; then
		# Found upgrade package.
		echo "$FILENAME"
		return 0
	fi

	# Check complete package again, as you can upgrade with a complete
	# package.
	if [ "$UQM_INSTALL" = UPGRADE ]; then
		FILENAME="${CONTENT_PATH}uqm-${UQM_VERSION}-$PACKNAME.zip"
		if [ -f "$FILENAME" ]; then
			# Found complete package.
			echo "$FILENAME"
			return 0
		fi
	fi
	return 1
}

package_available() {
	package_filename "$@" > /dev/null
	return $?
}

set_components_menu() {
	local TITLE OPTIONAL DEFAULT

	MENU_install_components_TITLE="Optional components selection"
	MENU_install_components_ITEMS="$UQM_PACKAGES"
	for PACKAGE in $UQM_PACKAGES; do
		eval TITLE="\"\${UQM_PACKAGE_${PACKAGE}_TITLE}\""
		eval OPTIONAL="\${UQM_PACKAGE_${PACKAGE}_OPTIONAL}"
		eval DEFAULT="\${UQM_PACKAGE_${PACKAGE}_DEFAULT}"
		eval MENU_install_components_ITEM_${PACKAGE}_TYPE=CHECK

		eval CHECK_${PACKAGE}_TITLE=\"\$TITLE\"
		if [ "$DEFAULT" = "TRUE" ]; then
			eval CHECK_${PACKAGE}_DEFAULT=CHECKED
		else
			eval CHECK_${PACKAGE}_DEFAULT=UNCHECKED
		fi
		if package_available "$PACKAGE"; then
			if [ "$OPTIONAL" = "FALSE" ]; then
				eval CHECK_${PACKAGE}_VALUE=CHECKED
				eval CHECK_${PACKAGE}_FIXED=TRUE
			fi
		else
			if [ "$OPTIONAL" = "TRUE" ]; then
				eval CHECK_${PACKAGE}_VALUE=UNCHECKED
				eval CHECK_${PACKAGE}_FIXED=TRUE
			else
				echo "Can't find '$PACKAGE' package"
				return 1
			fi
		fi
	done
	return 0
}

show_components_menu() {
	do_menu MENU install_components ""
}

INPUT_install_prefix_TITLE="Installation prefix"
#INPUT_install_prefix_TEXT=\
#"  This is the top directory where the game data will be installed.
#  In the directory you specify directories like 'bin', 'lib' and 'share'
#  will be used.
#"
if [ "$(id -u)" -eq 0 ]; then
	INPUT_install_prefix_DEFAULT="/usr/local/games/"
else
	INPUT_install_prefix_DEFAULT="$HOME/uqm/"
fi
INPUT_install_prefix_VALIDATOR=validate_path

show_install_prefix_menu() {
	do_menu INPUT install_prefix ""
	INPUT_install_prefix_VALUE="${INPUT_install_prefix_VALUE%/}/"
}

INPUT_content_path_TITLE="Location of content packages"
INPUT_content_path_DEFAULT="${PWD%/}/"
INPUT_content_path_VALIDATOR=validate_path
show_content_path_menu() {
	do_menu INPUT content_path ""
	INPUT_content_path_VALUE="${INPUT_content_path_VALUE%/}/"
}

selected_packages() {
	local RESULT PACKAGE VALUE
	RESULT=""
	for PACKAGE in $UQM_PACKAGES; do
		eval VALUE="\$CHECK_${PACKAGE}_VALUE"
		if [ "$VALUE" = CHECKED ]; then
			RESULT="${RESULT:+$RESULT }$PACKAGE"
		fi
	done
	echo "$RESULT"
}


