# List the game scripts providing support for the given archive name
# USAGE: games_find_scripts_for_archive $archive_name
# RETURN: a list of game scripts,
#         separated by line breaks
games_find_scripts_for_archive() {
	local archive_name
	archive_name="$1"

	# games_list_scripts_all is called on its own first, so its error code is not hidden by the pipe
	local scripts_list
	scripts_list=$(games_list_scripts_all)
	# grep error status in case of no game script found is ignored
	printf '%s' "$scripts_list" | xargs --no-run-if-empty grep \
		--files-with-matches \
		--regexp="^ARCHIVE_[0-9A-Z_]\\+=['\"]${archive_name}['\"]" || true
}

# Print the path to the first game script with support for the given archive name.
# USAGE: games_find_script_for_archive $archive_name
games_find_script_for_archive() {
	local archive_name=$1

	local scripts_list
	scripts_list=$(games_find_scripts_for_archive "$archive_name")

	# Throw an error if no game script is found for the given archive.
	if [ -z "$scripts_list" ]; then
		error_no_script_found_for_archive "$archive_name"
		## Remove the empty working directory
		rmdir "$PLAYIT_WORKDIR"
		return 1
	fi

	# If multiple scripts have been found with support for a given archive name, check if a hash comparison should be used.
	local scripts_list_length
	scripts_list_length=$(printf '%s' "$scripts_list" | wc --lines)
	if [ "$scripts_list_length" -gt 1 ]; then
		local game_script archives_list archive_match archive_match_reference
		while read -r game_script; do
			# Get the list of supported archives of the given name from the current game script.
			archives_list=$(exec $game_script --list-archives)
			archive_match=$(printf '%s' "$archives_list" | grep --regexp=" | ${archive_name}$")
			if [ -z "${archive_match_reference:-}" ]; then
				archive_match_reference="$archive_match"
			fi
			# If two scripts support distinct lists of hashes for a given archive name, run a hash comparison.
			if [ "$archive_match" != "$archive_match_reference" ]; then
				game_find_script_by_hash "$scripts_list"
				return 0
			fi
		done <<- EOL
		$(printf '%s' "$scripts_list")
		EOL
	fi

	printf '%s' "$scripts_list" | head --lines=1
}

# Find the first game script in a list with support for a given archive hash.
# TODO: This should be replaced with a function filtering the list, returning all matching game scripts.
# USAGE: game_find_script_by_hash $scripts_list
game_find_script_by_hash() {
	# The list of scripts that should be parsed, separated by line breaks
	local scripts_list=$1

	# Compute the hash of the current archive.
	local archive_name archive_hash
	archive_name=$(archive_name 'PLAYIT_BASE_ARCHIVE')
	archive_hash=$(archive_hash_md5_computed "$archive_name")

	# Loop over the list of scripts, print the first one matching the archive hashs.
	local game_script archives_list archive_match
	for game_script in $scripts_list; do
		archives_list=$(exec $game_script --list-archives)
		# If no match is found, grep errors out. We ignore that error to try the next game script.
		archive_match=$(printf '%s' "$archives_list" | grep --regexp="^${archive_hash}" || true)
		if [ -n "$archive_match" ]; then
			printf '%s' "$game_script"
			return 0
		fi
	done
}

