# 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 with the given archive name
# USAGE: games_find_script_for_archive $archive_name
# RETURN: the path to a single game script
games_find_script_for_archive() {
	local archive_name
	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,
	# run some extra tests to find the correct one
	if [ "$(printf '%s' "$scripts_list" | wc --lines)" -gt 1 ]; then
		local hash_reference archive_identifiers archive_identifier archive_hash
		while read -r script; do
			# Parse the archive identifiers from the game script
			# Multiple identifiers can be found if several archives of the given name are supported by the current script
			archive_identifiers=$(sed --silent --regexp-extended "s/^(ARCHIVE_BASE(_[A-Z0-9]+)*_[0-9]+)_NAME=['\"]?${archive_name}['\"]?$/\\1/p" "$script")
			for archive_identifier in $archive_identifiers; do
				# Parse the archive hash from the game script
				## FIXME: Fix hash parsing for archives with multiple valid hashes.
				archive_hash=$(sed --silent --regexp-extended "s/^${archive_identifier}_MD5=['\"]?([0-9a-f]+)['\"]?$/\\1/p" "$script")
				# If this is the first archive of the list, set its hash as the reference
				if [ -z "${hash_reference:-}" ]; then
					hash_reference="$archive_hash"
				fi
				# If the hash is distinct from the reference, we need to run a hash comparison to find the correct script
				if [ "$archive_hash" != "$hash_reference" ]; then
					game_find_script_by_hash "$scripts_list"
					return 0
				fi
			done
		done <<- EOL
		$(printf '%s' "$scripts_list")
		EOL
	fi

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

# Discriminate between multiple game scripts using the archive hash
# USAGE: game_find_script_by_hash $scripts_list
# RETURN: the path to a single game script
game_find_script_by_hash() {
	# The list of scripts that should be parsed, separated by line breaks
	local scripts_list
	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")

	local regexp
	regexp="^ARCHIVE_BASE(_[A-Z0-9]+)*_[0-9]+_MD5=['\"]?${archive_hash}['\"]?$"
	printf '%s' "$scripts_list" | xargs grep \
		--files-with-matches \
		--extended-regexp --regexp="$regexp" |
		head --lines=1
}

