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

###
# Shadow Gambit
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260315.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='shadow-gambit'
GAME_NAME='Shadow Gambit: The Cursed Crew'

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

ARCHIVE_BASE_2_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v3_(64bit)_(87352).exe'
ARCHIVE_BASE_2_MD5='c01bcb5a07d2fefce0104f95c124625a'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v3_(64bit)_(87352)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='2b59a0bc10ade138ef3f58dda56cd763'
ARCHIVE_BASE_2_PART2_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v3_(64bit)_(87352)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='08b3c436b8abec44f81ac38a427837db'
ARCHIVE_BASE_2_PART3_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v3_(64bit)_(87352)-3.bin'
ARCHIVE_BASE_2_PART3_MD5='4f9de90dbdf8638aab95803741c5cf3b'
ARCHIVE_BASE_2_PART4_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v3_(64bit)_(87352)-4.bin'
ARCHIVE_BASE_2_PART4_MD5='658f0cd24abd7b16445c52cf52f7bf14'
ARCHIVE_BASE_2_SIZE='28002952'
ARCHIVE_BASE_2_VERSION='1.2.133.f.r40893v3-gog87352'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/shadow_gambit_the_cursed_crew'

ARCHIVE_BASE_1_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438).exe'
ARCHIVE_BASE_1_MD5='9677874d7d448ff67c69b319c4862c09'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='daa2101dd1bf7b1bfbfc1399820b3eef'
ARCHIVE_BASE_1_PART2_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438)-2.bin'
ARCHIVE_BASE_1_PART2_MD5='5cfaffe56930d2be956b651419009ae4'
ARCHIVE_BASE_1_PART3_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438)-3.bin'
ARCHIVE_BASE_1_PART3_MD5='96600b962e5ebbf842cfc3cae9000d99'
ARCHIVE_BASE_1_PART4_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438)-4.bin'
ARCHIVE_BASE_1_PART4_MD5='630e2e7effb43806f2738c0e48bc450e'
ARCHIVE_BASE_1_PART5_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893v2_(64bit)_(86438)-5.bin'
ARCHIVE_BASE_1_PART5_MD5='374b8ceb48923a8928e43b925793e1f0'
ARCHIVE_BASE_1_SIZE='28679515'
ARCHIVE_BASE_1_VERSION='1.2.133.f.r40893v2-gog86438'

ARCHIVE_BASE_0_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893_(64bit)_(69829).exe'
ARCHIVE_BASE_0_MD5='b394c7866b3e2106b8659f76dcf3b814'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893_(64bit)_(69829)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='33d84cef13cae8cb1a27e03101cda2cc'
ARCHIVE_BASE_0_PART2_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893_(64bit)_(69829)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='aa475cf51a8a6b9f151a63e7f8acd226'
ARCHIVE_BASE_0_PART3_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893_(64bit)_(69829)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='8019f8a20edf33e894a49ccc84fdf867'
ARCHIVE_BASE_0_PART4_NAME='setup_shadow_gambit_the_cursed_crew_1.2.133.f.r40893_(64bit)_(69829)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='d499dd2ab3f31e1ddb7c6b6c6f9d35c7'
ARCHIVE_BASE_0_SIZE='27989056'
ARCHIVE_BASE_0_VERSION='1.2.133.f.r40893-gog69829'

ARCHIVE_BASE_DEMO_0_NAME='setup_shadow_gambit_the_cursed_crew_demo_1.0.46.r37840.d_(64bit)_(66564).exe'
ARCHIVE_BASE_DEMO_0_MD5='ad6c5d562cb5166301b82311495eb4fc'
ARCHIVE_BASE_DEMO_0_TYPE='innosetup'
ARCHIVE_BASE_DEMO_0_PART1_NAME='setup_shadow_gambit_the_cursed_crew_demo_1.0.46.r37840.d_(64bit)_(66564)-1.bin'
ARCHIVE_BASE_DEMO_0_PART1_MD5='2ed0f7c0fe3bcba3cfe562ff687743cc'
ARCHIVE_BASE_DEMO_0_PART2_NAME='setup_shadow_gambit_the_cursed_crew_demo_1.0.46.r37840.d_(64bit)_(66564)-2.bin'
ARCHIVE_BASE_DEMO_0_PART2_MD5='cf8746aaa4ff9b40366c562367155876'
ARCHIVE_BASE_DEMO_0_PART3_NAME='setup_shadow_gambit_the_cursed_crew_demo_1.0.46.r37840.d_(64bit)_(66564)-3.bin'
ARCHIVE_BASE_DEMO_0_PART3_MD5='190c8d547cd6f3217a10038079431c9c'
ARCHIVE_BASE_DEMO_0_SIZE='15729374'
ARCHIVE_BASE_DEMO_0_VERSION='1.0.46.r37840.d-gog66564'
ARCHIVE_BASE_DEMO_0_URL='https://www.gog.com/game/shadow_gambit_the_cursed_crew_demo'

UNITY3D_NAME='ShadowGambit_TCC'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# Required to prevent an unendin loading screen.
UNITY3D_PLUGINS='
GalaxyCSharpGlue.dll'
# GalaxyCSharpGlue.dll requirement
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll"

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_DATA_EXPANSION_YUKI_FILES="
${UNITY3D_NAME}_Data/StreamingAssets/aa/StandaloneWindows64/dlctrp_content_00"
CONTENT_GAME_DATA_EXPANSION_ZAGAN_FILES="
${UNITY3D_NAME}_Data/StreamingAssets/aa/StandaloneWindows64/dlcchc_content_00"
CONTENT_GAME_DATA_BUNDLES1_FILES="
${UNITY3D_NAME}_Data/StreamingAssets/aa/StandaloneWindows64/mis_*"
CONTENT_GAME_DATA_BUNDLES2_FILES="
${UNITY3D_NAME}_Data/StreamingAssets/aa/StandaloneWindows64/main_*"
CONTENT_GAME_DATA_BUNDLES3_FILES="
${UNITY3D_NAME}_Data/StreamingAssets/aa/StandaloneWindows64"

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Local/ShadowGambit_TCC'
WINE_PERSISTENT_DIRECTORIES_DEMO='
users/${USER}/AppData/Local/ShadowGambit_TCC_Demo'

PACKAGES_LIST='
PKG_BIN
PKG_DATA_EXPANSION_YUKI
PKG_DATA_EXPANSION_ZAGAN
PKG_DATA_BUNDLES1
PKG_DATA_BUNDLES2
PKG_DATA_BUNDLES3
PKG_DATA'
PACKAGES_LIST_DEMO='
PKG_BIN
PKG_DATA_BUNDLES1
PKG_DATA_BUNDLES2
PKG_DATA_BUNDLES3
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_EXPANSION_YUKI
PKG_DATA_EXPANSION_ZAGAN
PKG_DATA_BUNDLES1
PKG_DATA_BUNDLES2
PKG_DATA_BUNDLES3'
PKG_DATA_DEPENDENCIES_SIBLINGS_DEMO='
PKG_DATA_BUNDLES1
PKG_DATA_BUNDLES2
PKG_DATA_BUNDLES3'

PKG_DATA_EXPANSION_ID="${PKG_DATA_ID}-expansion"
PKG_DATA_EXPANSION_YUKI_ID="${PKG_DATA_EXPANSION_ID}-yuki"
PKG_DATA_EXPANSION_ZAGAN_ID="${PKG_DATA_EXPANSION_ID}-zagan"
PKG_DATA_EXPANSION_DESCRIPTION="$PKG_DATA_DESCRIPTION - expansion"
PKG_DATA_EXPANSION_YUKI_DESCRIPTION="$PKG_DATA_EXPANSION_DESCRIPTION - Yuki's Wish"
PKG_DATA_EXPANSION_ZAGAN_DESCRIPTION="$PKG_DATA_EXPANSION_DESCRIPTION - Zagan's Ritual"

PKG_DATA_BUNDLES_ID="${PKG_DATA_ID}-bundles"
PKG_DATA_BUNDLES1_ID="${PKG_DATA_BUNDLES_ID}-1"
PKG_DATA_BUNDLES2_ID="${PKG_DATA_BUNDLES_ID}-2"
PKG_DATA_BUNDLES3_ID="${PKG_DATA_BUNDLES_ID}-3"
PKG_DATA_BUNDLES_ID_DEMO="${PKG_DATA_ID_DEMO}-bundles"
PKG_DATA_BUNDLES1_ID_DEMO="${PKG_DATA_BUNDLES_ID_DEMO}-1"
PKG_DATA_BUNDLES2_ID_DEMO="${PKG_DATA_BUNDLES_ID_DEMO}-2"
PKG_DATA_BUNDLES3_ID_DEMO="${PKG_DATA_BUNDLES_ID_DEMO}-3"
PKG_DATA_BUNDLES_DESCRIPTION="$PKG_DATA_DESCRIPTION - bundles"
PKG_DATA_BUNDLES1_DESCRIPTION="$PKG_DATA_BUNDLES_DESCRIPTION - 1"
PKG_DATA_BUNDLES2_DESCRIPTION="$PKG_DATA_BUNDLES_DESCRIPTION - 2"
PKG_DATA_BUNDLES3_DESCRIPTION="$PKG_DATA_BUNDLES_DESCRIPTION - 3"

# 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'
# Include Unity3D plugins.
unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins/x86_64" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Store the game prefix into the WINE prefix, instead of using a symbolic link.
# This is required for the expansions to be correctly detected.
# The generated launchers will ignore $PLAYIT_PREFIX_PATH.
launcher_path_prefix() {
	local game_id
	game_id=$(game_id)
	cat <<- EOF
	# Set the path to the volatile game prefix.
	# This launcher ignores \$PLAYIT_PREFIX_PATH.
	path_prefix() {
	    # Store the game prefix into the WINE prefix, instead of using a symbolic link.
	    # This is required for the expansions to be correctly detected.
	    printf '%s/drive_c/%s' \\
	        "\$PATH_WINEPREFIX" \\
	        '$(game_id)'
	}
	PATH_PREFIX=\$(path_prefix)

	EOF
}
wine_wineprefix_init_actions() {
	cat <<- 'EOF'
	# Do not link the game prefix into the WINE prefix, as its real path is already there.

	# Remove most links pointing outside of the WINE prefix.
	rm "$WINEPREFIX/dosdevices/z:"
	find "$WINEPREFIX/drive_c/users/$(whoami)" -type l | while read -r directory; do
	    rm "$directory"
	    mkdir "$directory"
	done
	unset directory

	# Set symbolic links to the legacy paths.
	wineprefix_legacy_link 'AppData/Roaming' 'Application Data'
	wineprefix_legacy_link 'AppData/Local' 'Local Settings/Application Data'
	wineprefix_legacy_link 'Documents' 'My Documents'

	EOF
	wineprefix_init_custom
}

# Install Unity3D plugins.
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
