# Copyright 1999-2006 Gentoo Foundation
# Distributed under the terms of the GNU General Public License v2
# $Header: $

#
# Original Author: john
# Purpose: Provide enhanced input functions for ebuilds
#

ESC_SEQ=$'\e['

BOLD="${ESC_SEQ}01m"
NORMAL="${ESC_SEQ}39;49;00m"

BLUE="${ESC_SEQ}34;01m"
BROWN="${ESC_SEQ}33m"
DARKBLUE="${ESC_SEQ}34m"
DARKGREEN="${ESC_SEQ}32m"
DARKRED="${ESC_SEQ}31m"
FUCHSIA="${ESC_SEQ}35;01m"
GREEN="${ESC_SEQ}32;01m"
PURPLE="${ESC_SEQ}35m"
RED="${ESC_SEQ}31;01m"
TEAL="${ESC_SEQ}36m"
TURQUOISE="${ESC_SEQ}36;01m"
YELLOW="${ESC_SEQ}33;01m"

COLORS=(
	$BLUE
	$BROWN
	$DARKBLUE
	$DARKGREEN
	$DARKRED
	$FUCHSIA
	$GREEN
	$PURPLE
	$RED
	$TEAL
	$TURQUOISE
	$YELLOW
)

DIVIDER="${BOLD}----------------------------------------------${NORMAL}"

choices=( )
last_answer=""
nocolor="$(portageq envvar NOCOLOR 2>/dev/null)"

# Usage: einput_list choice1 prompt1 [ choice2 prompt2 ... ] question
#
# Returns: string converted to lower case and stored in global $last_answer
# Default: none
#
# Example: einput_list "1" "List Entry" "2" "List File" "Choose a listing style"
#
einput_list() {
	local answer_accepted=1
	local num_choices=$(( ($# - 1) / 2 ))
	echo $DIVIDER
	for (( i = 0, j = 0 ; i < num_choices ; i++, j++ )) ; do
		(( j == ${#COLORS[*]} )) && j=0
		echo "  ${COLORS[${j}]}${1})${NORMAL} ${2}"
		choices[$i]="$1"
		shift 2
	done
	echo $DIVIDER
	while [[ $answer_accepted ]] ; do
		echo -n "${BOLD}${1}${NORMAL}"
		read -rp ": " answer
		echo
		for choice in ${choices[*]} ; do
			if [[ "$answer" == "$choice" ]] ; then
				break 2
			elif [[ "$choice" == "${choices[$((num_choices - 1))]}" ]] ; then
				echo "!!! Invalid answer, try again."
				echo
				answer_accepted=0
			fi
		done
	done
	last_answer="$(tr [:upper:] [:lower:] <<< ${answer})"
}

# Usage: einput_confirm question [ default_value ]
#
# Returns: 0 for no, 1 for yes
# Default: default_value if given, else 1
#
# Example: einput_confirm "Are you sure you want to rm -rf /?"
#
einput_confirm() {
	local answer_accepted=1
	local color_yes color_no
	local string_yes="Yes" string_no="No"
	if [[ "$2" == "0" ]] ; then
		if [[ $nocolor == 1 ]] ; then
			string_yes="yes"
			string_no="NO"
		else
			color_yes=$RED
			color_no=$GREEN
		fi
	else
		if [[ $nocolor == 1 ]] ; then
			string_yes="YES"
			string_no="no"
		else
			color_yes=$GREEN
			color_no=$RED
		fi
	fi
	while [[ $answer_accepted ]] ; do
		echo -n "${BOLD}${1}${NORMAL} [${color_yes}${string_yes}${NORMAL}/${color_no}${string_no}${NORMAL}] "
		read -r answer
		echo
		case "$answer" in
			[yY]*)
				return 1 ;;

			[nN]*)
				return 0 ;;

			"")
				[[ "$2" == "0" ]] && return 0
				return 1 ;;

			*)
				echo "!!! Invalid answer, try again."
				echo
				answer_accepted=0 ;;
		esac
	done
}

# Usage: einput_question question default_value
#
# Returns: string stored in global $last_answer
# Default: default_value
#
# Example: einput_question "Is Gentoo a good Linux distro?" "Yes it is Jim"
#
einput_question() {
	echo -n "${BOLD}${1}${NORMAL} [${GREEN}${2}${NORMAL}] "
	read -r answer
	echo
	if [[ -z "$answer" ]] ; then
		last_answer="$2"
	else
		last_answer="$answer"
	fi
}

# Usage: einput_secret question
#
# Returns: string stored in global $last_answer
# Default: none
#
# Example: einput_secret "Please enter your root password"
#
einput_secret() {
	echo -n "${BOLD}${1}${NORMAL}"
	read -rsp ": " answer
	echo
	echo
	last_answer="$answer"
}

if [[ "$nocolor" == "true" || "$nocolor" == "yes" ]] ; then
	nocolor=1
else
	nocolor=0
fi
[[ "$(${ROOT}/sbin/consoletype 2> /dev/null)" == "serial" || $nocolor == 1 ]] &&
	unset BOLD NORMAL COLORS BLUE BROWN DARKBLUE DARKGREEN DARKRED FUCHSIA \
		GREEN PURPLE RED TEAL TURQUOISE YELLOW

# XXX Tests - don't forget to remove!
TEST_LIST=(
	"1" "For the money"
	"2" "For the show"
	"3" "To get ready"
	"4" "Now go, cat, go!"
	"5" "But don't you"
	"6" "Step on my blue suede shoes"
	"7" "You can do anything you want"
	"8" "But lay off of my blue suede shoes"
	"9" "There once was a man from Derry"
	"10" "A lass like an ape he did marry"
	"11" "He gave her bananas"
	"12" "She soiled his pajamas"
	"13" "My limericks, like bollocks big and hairy"
	"14" "Somebody stop me"
	"15" "No, really..."
)
einput_list "${TEST_LIST[@]}" "Choose your madness"
echo "Value returned: ${last_answer}"
echo
einput_confirm "Does anyone know you wear ladies' underwear?" "0"
echo "Value returned: $?"
echo
einput_question "Who's your master, pitiful slave?" "You are, Mistress Gentoo"
echo "Value returned: ${last_answer}"
echo
einput_secret "Please enter your root password"
echo "Value returned: ${last_answer}"
echo
