
read_config()
{
	local cf=`trim "$1"`

	[ -f "$cf" ] || { error "$cf: file not found"; return 2; }
	[ -s "$cf" ] || { error "$cf: file is empty";  return 2; }

	. "$cf" 2>/dev/null
	if test $? -ne 0; then
		error "$cf: error while reading configuration file"
		error "$cf: check for syntax errors and ensure configuration is complete"
		return 1
	fi

	return 0
}

write_config()
{
	local cf=`trim "$1"`
	local cfdir=""
	local xid=""
	local xids=""
	local xvar=""
	local xvars=""
	local val=""
	
	cfdir=`dirname "$cf" 2>/dev/null`	# returns "." if no dir element
	mkdirhier "$cfdir"
	if test ! -w "$cfdir"; then		# catch readonly filesystem
		error "$cfdir: no write permissions"
		return 1
	fi

	if test -r "$cf"; then
		debug "$cf: overwriting existing configuration"
	fi

	>"$cf"
	if test $? -ne 0; then
		error "$cf: unable to truncate file"
		return 1
	fi

	echo "# Gomtuu Linux configuration file."    >>$cf
	echo "# Generated by $BASENAME (v$VERSION)." >>$cf

	#
	#
	xids=${!XID_*}
	for xid in $xids; do
		echo >>$cf
		eval xvars=\${\!${!xid}\*}
		for xvar in $xvars; do
			val=${!xvar}
			echo "$xvar=\"$val\"" >>$cf
		done
	done

	return 0
}

clear_config()
{
	local xids="${!XID_*}"
	local xid=""
	local xvar=""
	local xvars=""

	for xid in $xids; do
		eval xvars=\${\!${!xid}\*}
		for xvar in $xvars; do
			unset "$xvar"
		done
	done
}
