# List the application identifiers for the current game.
# Identifiers are printed one per line, separated by line breaks.
# This function does not fail if no application is supported (common case for expansions).
# USAGE: applications_list
applications_list() {
	# Fetch the explicit list if it is set
	local applications_list
	# FIXME: Ensure that applications_list is always called with the correct context set.
	applications_list=$(context_value 'APPLICATIONS_LIST')

	# Parse the environment to compute an applications list from it.
	if [ -z "$applications_list" ]; then
		# The following expression matches:
		# - APP_xxx_EXE
		# - APP_xxx_SCUMMID
		# - APP_xxx_TYPE
		# and the suffixed variants of these variables.
		local sed_expression
		if compatibility_level_is_at_least '2.35'; then
			sed_expression='s/^(APP(_[0-9A-Z]+)+)_(EXE|SCUMMID|TYPE)(_[0-9A-Z]+)*=.*/\1/p'
		else
			# With compatibility level ≤ 2.34, extra underscores are not allowed in the identifier:
			# - APP_MAIN is matched
			# - APP_MAIN_RESOLUTION is not matched
			# It prevents matching APP_MAIN_PREFIX by accident, due to APP_MAIN_PREFIX_TYPE matching the expression.
			sed_expression='s/^(APP_[0-9A-Z]+)_(EXE|SCUMMID|TYPE)(_[0-9A-Z]+)*=.*/\1/p'
		fi
		applications_list=$(set | sed --silent --regexp-extended --expression="$sed_expression")
	fi

	# Fall back on the default applications list for the current game engine
	if [ -z "$applications_list" ]; then
		local game_engine
		game_engine=$(game_engine)
		case "$game_engine" in
			('ags')
				applications_list='APP_MAIN'
			;;
			('gamemaker')
				applications_list=$(applications_list_gamemaker)
			;;
			('unity3d')
				# Unity3D games are expected to provide a single application
				applications_list='APP_MAIN'
			;;
			('visionaire')
				applications_list=$(visionaire_applications_list)
			;;
		esac
	fi

	printf '%s\n' $applications_list | list_clean
}

