
readonly XID_SWSET="SWSET"

readonly ROOT_DIR_SWSET="SWSET"
readonly SWLIST_FILE="RELEASES"		# mind conflict w/ XID variables
readonly PING_CMD="ping -q -c3 -n -w5"
readonly WGET_CMD="wget"
readonly EXTRACT_SWSET="gbsswim"


swset_defaults()
{

	local dev=`sysinv_get_bootdev`
	local src=`sysinv_get_bootsrc "$dev"`

	if test -n "$src"; then
		eval ${XID_SWSET}_SEL=\"BASE-OS BASE-OS-DOC\"
		eval ${XID_SWSET}_SRC=\"$src\"

		return 0
	else
		error "failed loading Software Set defaults; unable to determine source parameter"
		return 1
	fi
}

get_swset_src()
{
	eval echo \${${XID_SWSET}_SRC}
}

validate_swset_src()
{
	local src=`trim "$1"`

	is_local_swset_src  "$src" && return 0
	is_remote_swset_src "$src" && return 0

	return 1
}

is_local_swset_src()
{
	local src=`trim "$1"`
	local -i ret=1

	if test "${src:0:1}" = "/"; then
		ret=0
	fi

	return $ret
}

is_remote_swset_src()
{
	local src=`trim "$1"`
	local host=""
	local proto=""

	proto=`echo $src | sed -ne 's@^\([A-Za-z]\+://\).*@\1@p' 2>/dev/null`
	proto=`echo $proto | tr [A-Z] [a-z] 2>/dev/null`
	
	case "$proto" in
	"ftp://" | "http://" | "https://")
	    host=`echo $src | sed -ne 's@^[A-Za-z]\+://@@p' 2>/dev/null` ;;
	"") host=$src ;;
	esac

	validate_ip "$host" || validate_hostname "$host"
	return $?
}

check_local_swset_src()
{
	local src=`trim "$1"`
	local pn=""		# pathname List file

	is_local_swset_src "$src" || { debug "$src: not local source"; return 1; }

	pn="$src/$ROOT_DIR_SWSET/$SWLIST_FILE"
	if test -r "$pn"; then
		return 0
	else
		return 1
	fi
}

get_swlist_file()
{
	local src=`trim "$1"`
	local dst=`trim "$2"`
	local url=""
	local pn=""			# pathname List file
	local -i ret=1

	dst=${dst:-.}
	pn="$dst/$SWLIST_FILE"
	
	#
	#
	if test ! -d "$dst"; then
		$DRY_RUN mkdirhier "$dst" || return 1
	fi
	if test -w "$dst"; then
		if test -r "$pn"; then
			warn "Software Set List file already exists; moving existing file to $pn~"
			$DRY_RUN mv -f "$pn" "$pn~"
		fi

		#
		#
		url="$src/$ROOT_DIR_SWSET/$SWLIST_FILE"
		if is_local_swset_src "$src"; then
			if test -f "$url"; then
				$DRY_RUN cp -av "$url" "$dst" 2>/dev/null
			else
				error "$url: List file not found"
			fi
		elif is_remote_swset_src "$src"; then
			type -p "$WGET_CMD" >/dev/null
			if test $? -eq 0; then
				$DRY_RUN $WGET_CMD -q -O "$pn" "$url"
				if test $? -ne 0; then
					error "$url: failed retrieving List file from remote source"
				fi
			else
				error "$WGET_CMD: command not found"
			fi
		fi
		ret=$?
	else
		error "$dst: no write permissions"
	fi

	[ $ret -eq 0 ] && echo $SWLIST_FILE
	return $ret
}

get_requested_swset_names()
{
	local swsets=""

	eval swsets=\${${XID_SWSET}_SEL}

	echo $swsets
}

get_available_swset_names()
{
	local lst=`trim "$1"`
	local sys=""
	local cpu=""
	local swsets=""
	local -i ret=1

	[ -n "$lst" ] || { debug "no List file specified"; return 2; }
	[ -r "$lst" ] || { error "$lst: List file not readable"; return 1; }

	sys=`resolve_system_id`
	[ $? -eq 0 ] || { error "no system selected"; return 2; }

	cpu=`resolve_cpu_id`
	[ $? -eq 0 ] || { error "no CPU selected"; return 2; }

	swsets=`grep "^$sys:$cpu[[:blank:]]" <"$lst" 2>/dev/null | \
		cut -f3 | tr ',' ' '`
	swsets=`trim "$swsets"`
	if test -n "$swsets"; then
		echo $swsets
		ret=0
	fi

	return $ret
}

lookup_swset_dir()
{
	local lst=`trim "$1"`
	local swset=`trim "$2"`
	local sys=""
	local cpu=""
	local dir=""
	local -i ret=1

	[ -n "$lst" ] || { debug "no List file specified"; return 2; }
	[ -r "$lst" ] || { error "$lst: List file not readable"; return 1; }

	sys=`resolve_system_id`
	[ $? -eq 0 ] || { error "no system selected"; return 2; }

	cpu=`resolve_cpu_id`
	[ $? -eq 0 ] || { error "no CPU selected"; return 2; }

	dir=`grep "^$sys:$cpu[[:blank:]]" <"$lst" 2>/dev/null | cut -f2`
	dir=`trim "$dir"`		     # rel. to Software Set dir. root
	if test -n "$dir"; then
		echo $ROOT_DIR_SWSET/$dir    # rel. to Software Set source root
		ret=0
	fi
	
	return $ret
}

fetch_swset_file()
{
	local lst=`trim "$1"`
	local swset=`trim "$2"`
	local dst_dir=`trim "$3"`
	local src_dir=""	# full (local) path of Software Set
	local swset_src=""	# machine or medium
	local swset_dir=""	# path on Software Set source
	local url=""
	local -i ret=1

	[ -n "$lst"   ] || { debug "no List file specified"; return 2; }
	[ -f "$lst"   ] || { error "$lst: List file not found"; return 1; }
	[ -n "$swset" ] || { debug "no Software Set specified"; return 2; }

	swset_dir=`lookup_swset_dir "$lst" "$swset"`
	if test -z "$swset_dir"; then
		error "$lst: no directory location available for $swset"
		return 1
	fi

	eval swset_src=\${${XID_SWSET}_SRC}
	validate_swset_src "$swset_src"
	if test $? -ne 0; then
		error "$swset_src: invalid Software Set source"
		return 1
	fi

	#
	#
	if is_local_swset_src "$swset_src"; then
		src_dir="$swset_src/$swset_dir"
		if test -r "$src_dir/$swset"; then
			dst_dir="$src_dir"
			ret=0
		fi
	elif is_remote_swset_src "$swset_src"; then
		$DRY_RUN mkdirhier "$dst_dir" || return 1

		type -p "$WGET_CMD" >/dev/null
		if test $? -eq 0; then
			url="$swset_src/$swset_dir/$swset"
			$DRY_RUN $WGET_CMD -q -O "$dst_dir/$swset" "$url"
			if test $? -ne 0; then
				error "$url: failed retrieving Software Set from remote source"
			fi
		else
			error "$WGET_CMD: command not found"
		fi
		ret=$?
	fi

	if test $ret -eq 0; then
		echo $dst_dir
	fi
	return $ret
}

install_swset()
{
	local swset=`trim "$1"`
	local dst=`trim "$2"`
	local -i ret=1

	[ -r "$swset" ] || { debug "$swset: file not readable"; return 1; }
	[ -n "$dst"   ] || { debug "no install directory specified"; return 2; }

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

	$DRY_RUN mkdirhier "$dst" || return 1

	$DRY_RUN $EXTRACT_SWSET -i -d "$dst" "$swset"
	ret=$?
	
	return $ret
}
