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

###
# Thief: Deadly Shadows
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260417.4

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='thief-3'
GAME_NAME='Thief: Deadly Shadows'

ARCHIVE_BASE_1_NAME='setup_thief_-_deadly_shadows_1.1_(21683).exe'
ARCHIVE_BASE_1_MD5='153723f2908242cf27a23ad58d0608b5'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_thief_-_deadly_shadows_1.1_(21683)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='8c549826345776df192fb1721740096f'
ARCHIVE_BASE_1_SIZE='2300000'
ARCHIVE_BASE_1_VERSION='1.1-gog2.21683'
ARCHIVE_BASE_1_URL='https://www.gog.com/game/thief_3'

ARCHIVE_BASE_0_NAME='setup_thief3_2.0.0.6.exe'
ARCHIVE_BASE_0_MD5='e5b84de58a1037f3e8aa3a1bb2a982be'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='2300000'
ARCHIVE_BASE_0_VERSION='1.1-gog2.0.0.6'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_GAME_BIN_FILES='
System/Ion Launcher.exe
System/t3.exe
System/T3Main.exe'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries0
CONTENT_GAME0_BIN_FILES='
System/binkw32.dll
System/eax.dll'
CONTENT_GAME1_BIN_FILES='
System/*.ini
System/*.int
System/*.t3u'
CONTENT_GAME2_BIN_PATH='__support/app'
CONTENT_GAME2_BIN_PATH_0='app'
CONTENT_GAME2_BIN_FILES='
user.ini'
CONTENT_GAME_DATA_FILES='
CONTENT
System/Data'
# TODO: Localised versions of these files are provided,
# and could be extracted with innoextract --collisions.
CONTENT_DOC_DATA_FILES='
manual.pdf
readme.rtf'

USER_PERSISTENT_DIRECTORIES='
saves'
USER_PERSISTENT_FILES='
*.ini'

# Set up required registry keys.
ion_root="C:\\\\${GAME_ID}"
save_game_path="C:\\\\${GAME_ID}\\\\saves"
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\Ion Storm\Thief - Deadly Shadows]
"ION_ROOT"="'${ion_root}'"
"SaveGamePath"="'${save_game_path}'"
'

APP_MAIN_EXE='System/t3.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
# Easier upgrades from pre-20201031.16 packages
PKG_BIN_PROVIDES='
thief3'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
# Easier upgrades from pre-20201031.16 packages
PKG_DATA_PROVIDES='
thief3-data'

# 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 'libplayit.sh.2 not found.\n'
	exit 1
fi
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

# Ensure correct audio playback.
wineprefix_init_custom() {
	cat <<- 'EOF'
	# The following empty file is required for audio playback.
	touch "${WINEPREFIX}/drive_c/thief-3/CONTENT/T3/Sounds/SchemaMetafile_Memory.csc"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
