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

###
# Vampire: The Masquerade - Redemption
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260708.4

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID='vampire-the-masquerade-redemption'
GAME_NAME='Vampire: The Masquerade - Redemption'

ARCHIVE_BASE_1_NAME='setup_vampire_the_masquerade_-_redemption_1.1_gog_v4_(80265).exe'
ARCHIVE_BASE_1_MD5=b34e87d4d5fd0fe31d03853fe15f6644
ARCHIVE_BASE_1_TYPE=innosetup
ARCHIVE_BASE_1_SIZE=1262578
ARCHIVE_BASE_1_VERSION=1.1-2

ARCHIVE_BASE_0_NAME='setup_vampire_the_masquerade_redemption_2.0.0.3.exe'
ARCHIVE_BASE_0_MD5=96c19b0ce487e53dd8fe24ef963d31fc
ARCHIVE_BASE_0_TYPE=innosetup
ARCHIVE_BASE_0_SIZE=1263478
ARCHIVE_BASE_0_VERSION=1.1-1

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_GAME_MAIN_FILES='
Vampire.exe
Vampire.isu
anet.inf
Chronicles
Sounds
Video
texture.dat
kver.pub
classes.zip
*.nob'
# TODO: Is this binary required?
CONTENT_GAME0_MAIN_FILES='
Ws2setup.Exe'
# TODO: Is the icon required?
CONTENT_GAME1_MAIN_FILES='
vampireAnkh.ico'
# TODO: Is any of this stuff required?
CONTENT_GAME2_MAIN_FILES='
Extras/cs/anet.inf
Extras/cs/sysinfo.exe
Extras/Atvi.ico
Extras/_nihilistic.ico
Extras/nihilistic.url
Extras/vampireAnkh.ico
Extras/whitewolf.ico
Extras/white-wolf.url'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME3_MAIN_FILES='
binkw32.dll
GETINFO.DLL
javai.dll
mss32.dll
rlDirect.dll
WONDLL.dll
Miles/MP3DEC.ASI
Miles/Mssa3d2.m3d
Miles/Mssa3d.m3d
Miles/MSSDS3DH.M3D
Miles/MSSEAX2.M3D
Miles/MSSEAX.M3D
Miles/MSSFAST.M3D'
# TODO: Is any of this stuff required?
CONTENT_GAME4_MAIN_FILES='
Extras/cs/GETINFO.DLL
Extras/cs/Sysinv.dll'
CONTENT_DOC_MAIN_FILES='
manual.pdf
Readme.rtf
ReadmePatch.rtf'

USER_PERSISTENT_DIRECTORIES='
savegames'
USER_PERSISTENT_FILES='
*.ini'

# Set required registry keys.
installdir=C:\\\\${GAME_ID}
installdirexe=${installdir}\\\\vampire.exe
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\Activision\Vampire: The Masquerade - Redemption\v1.0]
"CDDRIVE"="c:"
"INSTALLDIR"="'${installdir}'"
"INSTALLDIREXE"="'${installdirexe}'"
"INSTALLTYPE"=dword:00000002
"Version"="0.1"
'

APP_MAIN_EXE='Vampire.exe'
APP_MAIN_ICON='vampireAnkh.ico'

PKG_MAIN_ARCH=32
# Easier upgrades from pre-20260708.2 packages
PKG_MAIN_PROVIDES=vampire-the-masquerade-redemption-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
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Extract and store the game CD-key.

cdkey_destination=$(package_path PKG_MAIN)$(path_game_data)
mkdir --parents "$cdkey_destination"
case $(current_archive) in
	(ARCHIVE_BASE_1)
		# The source is a base64-encoded string, that must be converted to an hexadecimal representation.
		sed --silent --expression='99s/.*"valueData": "\(.*\)",/\1/p' \
			"$(path_archive_contents)/goggame-1207658831.script" |
			base64 --decode |
			od --format=x1 --address-radix=n |
			tr --delete ' ' \
			> "${cdkey_destination}/cdkey"
	;;
	(ARCHIVE_BASE_0)
		cat > "${cdkey_destination}/cdkey" <<- EOF
		4e074abf8e4dc1719cde2bb9912ea3e9
		EOF
	;;
esac

# Include game data.

content_inclusion_icons
content_inclusion_default

# Write launchers.

# Register the game CD-key.
wineprefix_init_custom() {
	cat <<- 'EOF'
	# Register the game CD-key.
	cdkey=$(cat "${PATH_GAME_DATA}/cdkey")
	$(wine_command) reg add 'HKLM\Software\WON\CDKeys' /v 'Vampire' /t REG_BINARY /d "$cdkey" /f
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
