# If some early action is set, run it then exit.
# Early actions are actions that are not requiring a base archive.
# The following switches trigger an early action:
# --help
# --version
# --list-available-scripts
# --list-archives
# --list-supported-games
# USAGE: init_early_actions
init_early_actions() {
	# --help
	local option_help
	option_help=$(option_value 'help')
	if [ "$option_help" -eq 1 ]; then
		help
		exit 0
	fi

	# --version
	local option_version
	option_version=$(option_value 'version')
	if [ "$option_version" -eq 1 ]; then
		printf '%s - %s\n' "$PLAYIT_LIBRARY_VERSION" "$PLAYIT_LIBRARY_REVISION"
		exit 0
	fi

	# --list-available-scripts
	local option_list_available_scripts
	option_list_available_scripts=$(option_value 'list-available-scripts')
	if [ "$option_list_available_scripts" -eq 1 ]; then
		games_list_scripts_all
		exit 0
	fi

	# --list-archives
	init_archives_list

	# --list-supported-games
	init_games_list
}

# List supported archives then exit, if called with --list-archives
# USAGE: init_archives_list
init_archives_list() {
	## Return early if ./play.it has not been called in archives listing mode
	local option_list_archives
	option_list_archives=$(option_value 'list-archives')
	## If called before the options parsing step, the previous function might have returned an empty string.
	if [ "${option_list_archives:-0}" -eq 0 ]; then
		return 0
	fi

	## List the archives supported by the current game script, or all archives if called from the "play.it" wrapper.
	local script_name
	script_name=$(basename "$0")
	if [ "$script_name" = 'play.it' ]; then
		archives_list_supported_all
	else
		archives_list_supported
	fi

	## Exit after printing the archives listing, a no further action should be taken
	exit 0
}

# List supported games then exit, if called with --list-supported-games
# USAGE: init_games_list
init_games_list() {
	## Return early if ./play.it has not been called in games listing mode
	local option_list_supported_games
	option_list_supported_games=$(option_value 'list-supported-games')
	## If called before the options parsing step, the previous function might have returned an empty string.
	if [ "${option_list_supported_games:-0}" -eq 0 ]; then
		return 0
	fi

	## List the games supported by the current game script, or all games if called from the "play.it" wrapper.
	local script_name
	script_name=$(basename "$0")
	if [ "$script_name" = 'play.it' ]; then
		games_list_supported_all
	else
		games_list_supported
	fi

	## Exit after printing the games listing, a no further action should be taken
	exit 0
}

# If some no-op action is set, run it then exit.
# A no-op action requires a base archive to be set already.
# The following switches trigger a no-op action:
# - --list-packages
# - --list-requirements
# - --show-game-script
# USAGE: init_noop_actions
init_noop_actions() {
	init_list_packages
	init_list_requirements
	init_show_game_script
}

# Print the list of packages that would be generated from the current archive then exit, if called with --list-packages
# USAGE: init_list_packages
init_list_packages() {
	local option_list_packages
	option_list_packages=$(option_value 'list-packages')
	if [ "$option_list_packages" -eq 0 ]; then
		return 0
	fi

	local archive
	archive=$(current_archive)
	packages_print_list "$archive"

	# Delete temporary files
	working_directory_cleanup

	exit 0
}

# Print the list of commands required to run the current game script then exit, if called with --list-requirements
# USAGE: init_list_requirements
init_list_requirements() {
	local option_list_requirements
	option_list_requirements=$(option_value 'list-requirements')
	if [ "$option_list_requirements" -eq 0 ]; then
		return 0
	fi

	requirements_list

	# Delete temporary files
	working_directory_cleanup

	exit 0
}

# Display the path to the game script then exit, if called with --show-game-script
# USAGE: init_show_game_script
init_show_game_script() {
	local option_show_game_script
	option_show_game_script=$(option_value 'show-game-script')
	if [ "$option_show_game_script" -eq 0 ]; then
		return 0
	fi

	realpath "$0"

	# Delete temporary files
	working_directory_cleanup

	exit 0
}

# Exit early if all packages are already built
# USAGE: init_packages_already_built
init_packages_already_built() {
	local option_overwrite
	option_overwrite=$(option_value 'overwrite')
	if [ "$option_overwrite" -eq 1 ]; then
		return 0
	fi

	local option_output_dir archive generated_packages_list
	option_output_dir=$(option_value 'output-dir')
	archive=$(current_archive)
	generated_packages_list=$(packages_print_list "$archive")
	while read -r generated_package; do
		if [ ! -e "${option_output_dir}/${generated_package}" ]; then
			return 0
		fi
	done <<- EOL
	$(printf '%s' "$generated_packages_list")
	EOL

	info_all_packages_already_built

	# Delete temporary files
	working_directory_cleanup

	exit 0
}

