# Print a list of directories to scan for game scripts.
# Each path is printed on its own line.
# USAGE: games_list_sources
games_list_sources() {
	# If an explicit path has been set, limit the search to this path
	local option_collection_path
	option_collection_path=$(option_value 'collection-path')
	if [ -n "$option_collection_path" ]; then
		printf '%s' "$option_collection_path"
		return 0
	fi

	# Include the current user game scripts collections
	local user_collections_basedir
	user_collections_basedir="${XDG_DATA_HOME:=$HOME/.local/share}/play.it/collections"
	if [ -d "$user_collections_basedir" ]; then
		find "$user_collections_basedir" -mindepth 1 -maxdepth 1 -type d | sort
	fi
	# Include the system-provided game scripts collections
	local system_prefix system_collections_basedir
	for system_prefix in \
		'/usr/local/share/games' \
		'/usr/local/share' \
		'/usr/share/games' \
		'/usr/share'
	do
		system_collections_basedir="${system_prefix}/play.it/collections"
		if [ -d "$system_collections_basedir" ]; then
			find "$system_collections_basedir" -mindepth 1 -maxdepth 1 -type d | sort
		fi
	done

	# Look for game scripts in the legacy "games" paths
	## These have a lower priority than the "collections" paths.
	user_collections_basedir="${XDG_DATA_HOME:=$HOME/.local/share}/play.it/games"
	if [ -d "$user_collections_basedir" ]; then
		find "$user_collections_basedir" -mindepth 1 -maxdepth 1 -type d | sort
	fi
	for system_prefix in \
		'/usr/local/share/games' \
		'/usr/local/share' \
		'/usr/share/games' \
		'/usr/share'
	do
		system_collections_basedir="${system_prefix}/play.it/games"
		if [ -d "$system_collections_basedir" ]; then
			find "$system_collections_basedir" -mindepth 1 -maxdepth 1 -type d | sort
		fi
	done
}

# List all available game scripts.
# Absolute paths are printed, separated by line breaks.
# USAGE: games_list_scripts_all
games_list_scripts_all() {
	local games_sources
	games_sources=$(games_list_sources)

	# Throw an error if no source of game scripts is found
	if [ -z "$games_sources" ]; then
		error_no_collection
		return 1
	fi

	while read -r games_collection; do
		find "$games_collection" -name play-\*.sh | sort
	done <<- EOF
	$(printf '%s' "$games_sources")
	EOF
}

