# Set the identifier of the current package
# USAGE: set_current_package $package
set_current_package() {
	local package
	package="$1"

	# Check that the identifier uses the expected PKG_xxx format.
	local regexp
	regexp='^PKG\(_[0-9A-Z]\+\)\+$'
	if ! printf '%s' "$package" | grep --quiet --regexp="$regexp"; then
		error_current_package_format_invalid "$package"
		## exit is used instead of return to ensure a failure when called from a subshell.
		exit 1
	fi

	# Check that the identifier is included in the list of packages to build.
	if ! package_is_included_in_packages_list "$package"; then
		error_current_package_not_in_list "$package"
		## exit is used instead of return to ensure a failure when called from a subshell.
		exit 1
	fi

	export PLAYIT_CONTEXT_PACKAGE="$package"
}

# Set the identifier of the default package
# USAGE: set_default_package $package
set_default_package() {
	local package
	package="$1"

	# Check that the identifier uses the expected PKG_xxx format.
	local regexp
	regexp='^PKG\(_[0-9A-Z]\+\)\+$'
	if ! printf '%s' "$package" | grep --quiet --regexp="$regexp"; then
		error_current_package_format_invalid "$package"
		return 1
	fi

	# Check that the identifier is included in the list of packages to build.
	if ! package_is_included_in_packages_list "$package"; then
		error_current_package_not_in_list "$package"
		return 1
	fi

	export PLAYIT_CONTEXT_PACKAGE_DEFAULT="$package"
}

# Print the identifier of the current package.
# If called too early, it prints an empty string.
# USAGE: current_package
current_package() {
	local package
	package="${PLAYIT_CONTEXT_PACKAGE:-$(default_package)}"

	printf '%s' "$package"
}

# Print the identifier of the default package.
# If called too early, it prints an empty string.
# USAGE: default_package
default_package() {
	printf '%s' "${PLAYIT_CONTEXT_PACKAGE_DEFAULT:-}"
}

