
readonly CPUINFO="/proc/cpuinfo"
readonly MEMINFO="/proc/meminfo"
readonly CMDLINE="/proc/cmdline"
readonly CONFIG_FILE="system"
readonly CONFIG_DIR=$RUNTIMEDIR
readonly READLINK="/usr/bin/readlink"

readonly PATHNAME_RANGE="[-_./~@:A-Za-z0-9]"

PAGER=${PAGER:-"less -c~"}
COLUMNS=${COLUMNS:-80}			# screen width


msg()
{
	echo -e "$1"
}

debug()
{
	if test -n "$DEBUG"; then
		if test $DEBUG -eq 0; then
			:	# output nothing
		elif test $DEBUG -eq 1; then
			msg "debug: $1" >/dev/stderr
		fi
	else
		warn "encountered debug message, but DEBUG setting undefined"
		warn "set DEBUG to 0 or 1 to disable resp. enable debug messages"
	fi
}

warn()
{
	msg "warning: $1" >/dev/stderr
}

error()
{
	msg "error: $1" >/dev/stderr

	return 1
}

fatal()
{
	msg "fatal: $1" >/dev/stderr

	exit 1
}

abort()
{
	local m="$1"

	if test -n "$m"; then
		msg "$m"
	fi

	msg ""
	msg "!!  INSTALLATION ABORTED  !!"
	msg ""

	exit 1
}

mhdr()
{
	local title=`trim "$1"`
	local -i len=0
	local -i npad=0
	local -i padL=0
	local -i padR=0

	#
	#
	len=${#title}
	npad=$(($COLUMNS - $len - 2))
	padL=$(($npad / 2))
	padR=$(($padL + $(($npad % 2))))	# 1 or 0 extra padding char.

	printf "\n\n"
	eval printf "=%.0s" {1..$padL}		# need `eval' to unquote {1..x}
	printf " %s " "$title"
	eval printf "=%.0s" {1..$padR}
	printf "\n\n"
}

ihdr()
{
	local item=`trim "$1"`
	local -i len=0

	len=${#item}
	printf "\n%s\n" "$item"
	eval printf -- "-%.0s" {1..$len}	# use -- to protect "-" in fmt.
	printf "\n\n"
}

wait_for_keypress()
{
	echo -n "                                [Press Any Key]"
	read -n1
}

confirm()
{
	local op_text="$1"
	local -i done=0
	local -i ret=1

	if test -z "$op_text"; then
		op_text="Confirm"
	fi

	while test $done -eq 0; do
		echo -n "$op_text (Y/N) "
		read
		if test $? -eq 0; then
			case "$REPLY" in
			Y|y) done=1; ret=0 ;;
			N|n) done=1; ret=1 ;;
			*)   echo          ;; # force newline
			esac
		else
			done=1
		fi
	done

	return $ret
}

input_field()
{
	local prompt="${1:-Enter input}"
	local func=`trim "$2"`
	local hide=`trim "$3"`
	local input=""
	local noecho_opt=""
	local -i done=0
	local -i ret=1

	if test -n "$hide"; then
		is_number "$hide" || { debug "$hide: parameter NaN"; return 2; }
		if test $hide -eq 1; then
			noecho_opt="-s"
		elif test $hide -eq 1; then
			: # no option needed
		else
			debug "$hide: hide flag not boolean"
			return 2
		fi
	fi

	if test -n "$func"; then
		type $func >/dev/null 2>&1
		if test $? -ne 0; then
			error "$func: input verification function not found"
			return 2
		fi
	fi

	while test $done -eq 0; do
		read $noecho_opt -p "[$prompt] " input
		if test $? -eq 0; then
			if test -n "$func"; then
				eval $func "$input"
			else
				true
			fi
			if test $? -eq 0; then
				echo "$input"
				done=1; ret=0
			else
				error "input is not valid"
			fi
		else
			done=1
		fi
	done

	return $ret
}

trim()
{
	local s="$1"
	local old_extglob=`shopt -p extglob`

	shopt -s extglob		# enable extended pattern matching

	s=${s##+([[:space:]])}
	s=${s%%+([[:space:]])}
	
	eval $old_extglob		# restore previous mode

	echo "$s"
}

sort_list()
{
	local items="$1"
	local item=""
	local sorted_list=""
	
	sorted_list=`for item in $items; do echo $item; done | \
			sort -df 2>/dev/null | uniq 2>/dev/null`

	echo $sorted_list
}

is_number()
{
	local arg=`trim "$1"`

	expr match "$arg" "^[0-9]\+$" >/dev/null 2>&1
	return $?
}

validate_pathname()
{
	local pn=`trim "$1"`

	echo $pn | grep -q "^$PATHNAME_RANGE\+$" 2>/dev/null
	if test $? -eq 0; then
		return 0
	else
		return 1
	fi
}

normalize_pathname()
{
	local pn=`trim "$1"`

	[ -x "$READLINK" ] || { error "$READLINK: command not found"; return; }

	$READLINK -m "$pn" 2>/dev/null
}

mkdirhier()
{
	local dir=`trim "$1"`

	[ -d "$dir" ] && return 0
	
	mkdir -p "$dir" 2>/dev/null
	if test $? -ne 0; then
		error "$dir: failed creating directory"
		return 1
	else
		return 0
	fi
}

show_help()
{
	local hfile=`trim "$1"`
	local hpath="$HELPDIR/$hfile"
	local pager_cmd=""

	pager_cmd=`trim "$PAGER" | cut -d' ' -f1 2>/dev/null`
	type -p "$pager_cmd" >/dev/null 2>&1
	if test $? -eq 0; then
		if test -r "$hpath"; then
			$PAGER "$hpath" 2>/dev/null
		else
			error "help file not available"
		fi
	else
		error "unable to display help; pager not found"
	fi
}
