#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2022 Antoine Le Gonidec <vv221@dotslashplay.it>
set -o errexit

###
# Desperados 3
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260330.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='desperados-3'
GAME_NAME='Desperados III'

GAME_ID_DEMO="${GAME_ID}-demo"
GAME_NAME_DEMO="$GAME_NAME (demo)"

ARCHIVE_BASE_0_NAME='setup_desperados_3_1.7_(43311).exe'
ARCHIVE_BASE_0_MD5='27d07bcafb7b0a0c5bd0af5a0b91edb8'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_desperados_3_1.7_(43311)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='5554579fd67870ad94347b70df2743f6'
ARCHIVE_BASE_0_PART2_NAME='setup_desperados_3_1.7_(43311)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='f7f43119dda91362ec45b122e4ea641d'
ARCHIVE_BASE_0_PART3_NAME='setup_desperados_3_1.7_(43311)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='51948b7e8d6a9e45638b49988f86c5f9'
ARCHIVE_BASE_0_SIZE='24000000'
ARCHIVE_BASE_0_VERSION='1.5.8-gog43311'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/desperados_iii'

ARCHIVE_BASE_DEMO_0_NAME='setup_desperados_3_demo_1.0b_(38752).exe'
ARCHIVE_BASE_DEMO_0_MD5='2a856f6a70fb57b7095d829fe90f0e61'
ARCHIVE_BASE_DEMO_0_TYPE='innosetup'
ARCHIVE_BASE_DEMO_0_PART1_NAME='setup_desperados_3_demo_1.0b_(38752)-1.bin'
ARCHIVE_BASE_DEMO_0_PART1_MD5='fa829f953b1183dd8a41c12599c992fe'
ARCHIVE_BASE_DEMO_0_PART2_NAME='setup_desperados_3_demo_1.0b_(38752)-2.bin'
ARCHIVE_BASE_DEMO_0_PART2_MD5='77e764c9d83158abf31b7e98006cc5c9'
ARCHIVE_BASE_DEMO_0_SIZE='7537932'
ARCHIVE_BASE_DEMO_0_VERSION='1.0b-gog38752'
ARCHIVE_BASE_DEMO_0_URL='https://www.gog.com/game/desperados_iii_demo'

UNITY3D_NAME='Desperados III'

CONTENT_PATH_DEFAULT='.'
# Desperados III.exe requirements
CONTENT_LIBS_BIN_FILES='
UnityPlayer.dll'
# The game fails to start without the following libraries.
CONTENT_LIBS0_BIN_FILES='
THQNOnline/gog/Galaxy64.dll
steam_api64.dll'
# Prevent a critical error on launch.
CONTENT_LIBS1_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/mono-2.0-bdwgc.dll'
# The initial loading does not complete without the following library.
CONTENT_LIBS2_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/MonoPosixHelper.dll'
# Prevent a critical error on launch.
CONTENT_GAME0_BIN_FILES='
thqnocfg.dat'
CONTENT_GAME_DATA_GI_FILES="
${UNITY3D_NAME}_Data/GI"
CONTENT_GAME_DATA_STREAMINGASSETS_FILES="
${UNITY3D_NAME}_Data/StreamingAssets"
CONTENT_GAME_DATA_LEVELS_FILES_DEMO="
${UNITY3D_NAME}_Data/level*"
CONTENT_GAME_DATA_LEVELS_1_FILES="
${UNITY3D_NAME}_Data/level?
${UNITY3D_NAME}_Data/level?.resS
${UNITY3D_NAME}_Data/level??
${UNITY3D_NAME}_Data/level??.resS"
CONTENT_GAME_DATA_LEVELS_2_FILES="
${UNITY3D_NAME}_Data/level???
${UNITY3D_NAME}_Data/level???.resS"

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Local/Desperados III'

PACKAGES_LIST='
PKG_BIN
PKG_DATA_GI
PKG_DATA_STREAMINGASSETS
PKG_DATA_LEVELS_1
PKG_DATA_LEVELS_2
PKG_DATA'
PACKAGES_LIST_DEMO='
PKG_BIN
PKG_DATA_GI
PKG_DATA_STREAMINGASSETS
PKG_DATA_LEVELS
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/quicktime, variant=(string)iso'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_ID_DEMO="${GAME_ID_DEMO}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_GI
PKG_DATA_STREAMINGASSETS
PKG_DATA_LEVELS_1
PKG_DATA_LEVELS_2'
PKG_DATA_DEPENDENCIES_SIBLINGS_DEMO='
PKG_DATA_GI
PKG_DATA_STREAMINGASSETS
PKG_DATA_LEVELS'

PKG_DATA_GI_ID="${PKG_DATA_ID}-gi"
PKG_DATA_GI_ID_DEMO="${PKG_DATA_ID_DEMO}-gi"
PKG_DATA_GI_DESCRIPTION="$PKG_DATA_DESCRIPTION - gi"

PKG_DATA_STREAMINGASSETS_ID="${PKG_DATA_ID}-streamingassets"
PKG_DATA_STREAMINGASSETS_ID_DEMO="${PKG_DATA_ID_DEMO}-streamingassets"
PKG_DATA_STREAMINGASSETS_DESCRIPTION="$PKG_DATA_DESCRIPTION - streamingassets"

PKG_DATA_LEVELS_ID="${PKG_DATA_ID}-levels"
PKG_DATA_LEVELS_ID_DEMO="${PKG_DATA_ID_DEMO}-levels"
PKG_DATA_LEVELS_1_ID="${PKG_DATA_LEVELS_ID}-1"
PKG_DATA_LEVELS_2_ID="${PKG_DATA_LEVELS_ID}-2"
PKG_DATA_LEVELS_DESCRIPTION="$PKG_DATA_DESCRIPTION - levels"
PKG_DATA_LEVELS_1_DESCRIPTION="$PKG_DATA_LEVELS_DESCRIPTION - 1"
PKG_DATA_LEVELS_2_DESCRIPTION="$PKG_DATA_LEVELS_DESCRIPTION - 2"

# Load common functions.

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$PWD" \
		"${HOME}/.local/lib/play.it" \
		'/usr/local/lib/play.it' \
		'/usr/lib/play.it'
	do
		playit_lib_path="${playit_lib_dir}/libplayit.sh.2"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit2.sh not found.\n'
	exit 1
fi
# shellcheck source=libplayit2.sh
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Prevent inclusion of unwanted shipped libraries.
content_inclusion_default_libraries 'PKG_BIN'
rm --recursive \
	"$(path_archive_contents)/MonoBleedingEdge/EmbedRuntime" \
	"$(path_archive_contents)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
