
readonly XID_SYSTEM="SYS"

readonly HWRPB=hwrpb

readonly MDB_PREFIX="M_"
readonly MDB_FILE="${DATADIR}/models.lst"

#
#
declare -a MDB_SYSTEM_IDS


sysdef_defaults()
{

	local hostname=""
	
	hostname=`get_current_system_id | tr [A-Z] [a-z] 2>/dev/null`
	if test -n "$hostname"; then
		eval ${XID_SYSTEM}_MODEL_ID=\"__ACTUAL__\"
		eval ${XID_SYSTEM}_CPU_ID=\"__ACTUAL__\"
		eval ${XID_SYSTEM}_HOSTNAME=\"$hostname\"
		eval ${XID_SYSTEM}_PASSWD=\"alpha\"

		return 0
	else
		error "failed loading system defaults; unable to derive default hostname"
		return 1
	fi
}

MDB_init_database()
{
	local ids=""
	local -i ret=1
	
	if test -r "$MDB_FILE"; then
		. "$MDB_FILE"
		if test $? -ne 0; then
			fatal "unable to read model database: $MDB_FILE"
		fi

		if test "$MDB_PREFIX" = "MDB_"; then
			fatal "model database prefix reserved: $MDB_PREFIX"
		fi
		eval ids=\${${MDB_PREFIX}SYSTEM_IDS}
		MDB_SYSTEM_IDS=(`sort_list "$ids"`)

		ret=0
	fi

	return $ret
}

MDB_list_system_names()
{
	local m=""
	local n=""

	for m in ${MDB_SYSTEM_IDS[@]}; do
		eval n=\${${MDB_PREFIX}${m}_DESC}
		echo $n
	done
}

MDB_get_system_name_by_id()
{
	local id=`trim "$1"`
	local n=""

	is_system_id "$id"
	if test $? -eq 0; then
		if test "$id" = "__ACTUAL__"; then
			n="$id"
		else
			eval n=\${${MDB_PREFIX}${id}_DESC}
		fi
	fi
	echo $n
}

MDB_get_system_id_by_index()
{
	local idx=`trim "$1"`

	is_number "$idx"
	if test $? -eq 0; then
		if test $idx -le ${#MDB_SYSTEM_IDS[@]}; then
			echo ${MDB_SYSTEM_IDS[$idx]}
		fi
	fi
}

MDB_list_cpu_ids()
{
	local id=`trim "$1"`
	local cpus=""

	is_system_id "$id"
	if test $? -eq 0; then
		eval cpus=\${${MDB_PREFIX}${id}_CPU}
	
		sort_list "$cpus"
	fi
}

MDB_list_cpus()
{
	local id=`trim "$1"`
	local cpu=""
	local n=""
	
	is_system_id "$id"
	if test $? -eq 0; then
		for cpu in `MDB_list_cpu_ids "$id"`; do
			eval n=\${${MDB_PREFIX}${id}_${cpu}}
			if test -z "$n"; then
				n=$cpu
			fi
			echo "$n"
		done
	fi
}

MDB_get_cpu_by_id()
{
	local id=`trim "$1"`
	local cpu=`trim "$2"`
	local d=""
	
	is_system_id "$id"
	if test $? -eq 0; then
		if test "$cpu" = "__ACTUAL__"; then
			d="$cpu"
		else
			eval d=\${${MDB_PREFIX}${id}_${cpu}}
			if test -z "$d"; then
				d="$cpu"
			fi
		fi
	fi

	echo $d
}

MDB_get_cpu_id_by_index()
{
	local id=`trim "$1"`
	local idx=`trim "$2"`
	local -a cpu_ids

	is_system_id "$id"
	if test $? -eq 0; then
		cpu_ids=(`MDB_list_cpu_ids "$id"`)
		is_number "$idx"
		if test $? -eq 0; then
			if test $idx -le ${#cpu_ids[@]}; then
				echo "${cpu_ids[$idx]}"
			fi
		fi
	fi
}

get_current_system_id()
{
	local id=""
	local sys=""
	local match=""
	local -i idx=0
	local -i line=0

	type -p "$HWRPB" >/dev/null
	if test $? -ne 0; then
		error "$HWRPB: command not found"
		return
	fi

	sys=`$HWRPB 2>/dev/null | \
		sed -ne 's/^[ \t]*\(.*\)[ \t]\+.*/\1/p' 2>/dev/null`
	#
	#
	match=`MDB_list_system_names | grep -ni "$sys" 2>/dev/null`
	line=`echo $match | cut -d: -f1 2>/dev/null`
	idx=$(($line - 1))
	
	MDB_get_system_id_by_index "$idx"
}

get_current_cpu_id()
{
	local id=""
	local cpu=""

	type -p "$HWRPB" >/dev/null
	if test $? -ne 0; then
		error "$HWRPB: command not found"
		return
	fi

	cpu=`$HWRPB 2>/dev/null | \
		sed -ne 's/^.*(\([[:alnum:]]\+\))[ \t]*$/\1/p' 2>/dev/null`
	echo "$cpu"
}

resolve_system_id()
{
	local sys=""

	eval sys=\${${XID_SYSTEM}_MODEL_ID}
	if test -z "$sys"; then
		return 1
	fi
	if test "$sys" = "__ACTUAL__"; then
		sys=`get_current_system_id`
		if test -z "$sys"; then
			return 1
		fi
	fi
	echo $sys

	return 0
}

resolve_cpu_id()
{
	local cpu=""

	eval cpu=\${${XID_SYSTEM}_CPU_ID}
	if test -z "$cpu"; then
		return 1
	fi
	if test "$cpu" = "__ACTUAL__"; then
		cpu=`get_current_cpu_id`
		if test -z "$cpu"; then
			return 1
		fi
	fi
	echo $cpu

	return 0
}

is_system_id()
{
	local id=`trim "$1"`

	[ -n "$id" ] || { debug "no system ID specified"; return 1; }

	expr match "$id" "^[[:alnum:]_]\+$" >/dev/null 2>&1
	return $?
}

get_password()
{
	local pass=""

	eval pass=\${${XID_SYSTEM}_PASSWD}
	echo "$pass"	# quote to preserve possible whitespace

	test -n "$pass"
}
