# WINE launcher - Print the script content
# USAGE: wine_launcher $application
wine_launcher() {
	local application
	application="$1"

	local prefix_type
	prefix_type=$(application_prefix_type "$application")
	case "$prefix_type" in
		('symlinks')
			launcher_headers

			# Set the paths that should be available to the generated launcher
			launcher_init_paths "$application"

			# Include the wrappers that are useful for the current game
			wine_launcher_wrappers

			# Generate the game prefix
			prefix_symlinks_generate

			# Setup the paths diversion to persistent storage.
			persistent_storage_prerun "$application"

			# Generate the WINE prefix.
			wine_launcher_wineprefix_environment
			wine_launcher_wineprefix_generate
			wine_launcher_wineprefix_persistent

			# Handle persistent storage of registry keys.
			wine_launcher_regedit_environment
			wine_launcher_regedit_load

			wine_launcher_run "$application"

			# Handle persistent storage of registry keys.
			wine_launcher_regedit_store

			# Update persistent storage from current prefix.
			persistent_storage_postrun

			launcher_exit
		;;
		(*)
			error_launchers_prefix_type_unsupported "$application"
			return 1
		;;
	esac
}

# WINE - Set the wrappers around WINE-related commands.
# USAGE: wine_launcher_wrappers
wine_launcher_wrappers() {
	cat <<- 'EOF'
	# Set the wrappers

	# Print the path to the `wine` command
	wine_command() {
	    if [ -z "$PLAYIT_WINE_CMD" ]; then
	        command -v wine
	        return 0
	    fi
	    printf '%s' "$PLAYIT_WINE_CMD"
	}

	EOF

	# Include the terminal and winetricks wrappers
	local winetricks_verbs renderer_name
	winetricks_verbs=$(wine_winetricks_verbs)
	renderer_name=$(wine_renderer_name)
	if
		[ -n "$winetricks_verbs" ] ||
		[ "$renderer_name" != 'default' ]
	then
		launcher_wrapper_terminal
		wine_winetricks_wrapper
	fi

	# Include the screen resolution detection function if it is required to set a virtual desktop
	local virtual_desktop
	virtual_desktop=$(wine_virtual_desktop)
	if [ "$virtual_desktop" = 'auto' ]; then
		wine_snippet_screen_resolution
	fi
}

# WINE - Print the snippet handling the actual run of the game
# USAGE: wine_launcher_run $application
wine_launcher_run() {
	local application
	application="$1"

	local game_id
	game_id=$(game_id)
	cat <<- EOF
	# Run the game

	cd "\${WINEPREFIX}/drive_c/${game_id}"

	EOF

	local game_engine
	game_engine=$(game_engine)
	case "$game_engine" in
		('visionaire')
			# Prevent the use of wayland SDL video driver
			visionaire_tweak_sdl_wine
		;;
	esac

	application_prerun "$application"

	cat <<- 'EOF'

	## Do not exit on application failure,
	## to ensure post-run commands are run.
	set +o errexit

	EOF
	game_exec_line "$application"
	cat <<- 'EOF'

	game_exit_status=$?
	set -o errexit

	EOF

	application_postrun "$application"
}

# WINE - Print the line starting the game
# USAGE: wine_exec_line $application
# RETURN: the command to execute, including its command line options
wine_exec_line() {
	local application
	application="$1"

	local application_exe application_options
	application_exe=$(application_exe "$application")
	application_options=$(application_options "$application")
	cat <<- EOF
	\$(wine_command) "${application_exe}" $application_options "\$@"
	EOF
}

