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

###
# Tomb Raider Remastered:
# - Tomb Raider
# - Tomb Raider 2
# - Tomb Raider 3
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260413.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='tomb-raider-remastered-1-2-3'
GAME_NAME='Tomb Raider I-III Remastered'

# WARNING: This build of the game seems to include AI-regurgitated content.
ARCHIVE_BASE_3_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.1_(64bit)_(89708).exe'
ARCHIVE_BASE_3_MD5='2ebf80db873be9840c044be14b7ac533'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.1_(64bit)_(89708)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='9ebe08c1db65847350ec2fbee6738d09'
ARCHIVE_BASE_3_PART2_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.1_(64bit)_(89708)-2.bin'
ARCHIVE_BASE_3_PART2_MD5='b7da8b538417cc60ab2863fba9ded8fd'
ARCHIVE_BASE_3_SIZE='6453552'
ARCHIVE_BASE_3_VERSION='1.1.1-gog89708'
ARCHIVE_BASE_3_URL='https://www.gog.com/game/tomb_raider_i_to_iii_remastered'

# WARNING: This build of the game seems to include AI-regurgitated content.
ARCHIVE_BASE_2_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.0_dlc_(64bit)_(89347).exe'
ARCHIVE_BASE_2_MD5='2e88812609e6bd4ebadad90425e96582'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.0_dlc_(64bit)_(89347)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='cb9cd7fd3d7615bc3bd324763c8661c7'
ARCHIVE_BASE_2_PART2_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.1.0_dlc_(64bit)_(89347)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='f0c6a1f7e8c096f759e2e256392e3d42'
ARCHIVE_BASE_2_SIZE='6218538'
ARCHIVE_BASE_2_VERSION='1.1.0-gog89347'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/tomb_raider_i_to_iii_remastered'

ARCHIVE_BASE_1_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.0.5d_(64bit)_(88648).exe'
ARCHIVE_BASE_1_MD5='335b1010593a34048fbb23bd7a74c1e3'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_tomb_raider_i-iii_remastered_starring_lara_croft_1.0.5d_(64bit)_(88648)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='160ddec95d0870bf63d5699999b7827d'
ARCHIVE_BASE_1_SIZE='6105792'
ARCHIVE_BASE_1_VERSION='1.0.5d-gog88648'

ARCHIVE_BASE_0_NAME='setup_tomb_raider_i-iii_remastered_1.01_patch_4_(77558).exe'
ARCHIVE_BASE_0_MD5='9172c7570c8c0dfd47c3925aaecf360d'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_tomb_raider_i-iii_remastered_1.01_patch_4_(77558)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='dbcec0aba1dd597efe96bace51e79140'
ARCHIVE_BASE_0_SIZE='6109084'
ARCHIVE_BASE_0_VERSION='1.01-gog77558'

CONTENT_PATH_DEFAULT='.'
# The game crashes on launch if any of the following libraries is missing.
CONTENT_LIBS_BIN_FILES='
1/tomb1.dll
2/tomb2.dll
3/tomb3.dll'
CONTENT_GAME_BIN_FILES='
tomb123.exe'
CONTENT_GAME_DATA_FILES='
?/CUTS
?/DATA
?/FMV
?/ITEM
?/PIX
?/SFX
?/TEX
?/TEXT
?/TRACKS'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Roaming/TRX'

APP_MAIN_EXE='tomb123.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
# Easier upgrades from pre-20251113.1 packages
PKG_DATA_PROVIDES='
tomb-raider-remastered-1-2-3-data-episode-1
tomb-raider-remastered-1-2-3-data-episode-2
tomb-raider-remastered-1-2-3-data-episode-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 '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.

# 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
