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

###
# Vampire: The Masquerade - Bloodlines
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260228.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='vampire-the-masquerade-bloodlines'
GAME_NAME='Vampire: The Masquerade - Bloodlines'

# The DotEmu store closed down in 2017, since then they only advertise Steam.
ARCHIVE_BASE_DOTEMU_0_NAME='vampire_the_masquerade_bloodlines_v1.2.exe'
ARCHIVE_BASE_DOTEMU_0_MD5='8981da5fa644475583b2888a67fdd741'
ARCHIVE_BASE_DOTEMU_0_EXTRACTOR='unar'
ARCHIVE_BASE_DOTEMU_0_SIZE='5013863'
ARCHIVE_BASE_DOTEMU_0_VERSION='1.2-dotemu1'

ARCHIVE_BASE_GOG_EN_6_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.511.410.2)_(68368).exe'
ARCHIVE_BASE_GOG_EN_6_MD5='
553f6bdbd8b23fa143da22d9ea47a20b
b2c1eff2640a4b7c52922a3c1ce3f36b'
ARCHIVE_BASE_GOG_EN_6_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_6_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.511.410.2)_(68368)-1.bin'
ARCHIVE_BASE_GOG_EN_6_PART1_MD5='
f0ca81565507da32075e6334a96a173f
96b585f441224ab5f8658815b74a0763'
ARCHIVE_BASE_GOG_EN_6_SIZE='4152647'
ARCHIVE_BASE_GOG_EN_6_VERSION='1.2-gog68368'
ARCHIVE_BASE_GOG_EN_6_URL='https://www.gog.com/game/vampire_the_masquerade_bloodlines'

ARCHIVE_BASE_GOG_FR_6_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.511.410.2)_(french)_(68368).exe'
ARCHIVE_BASE_GOG_FR_6_MD5='
5bb92c9507ef442ec95dade41f940c48
ea6cec86b0eb1986164eefb6ddf458ef'
ARCHIVE_BASE_GOG_FR_6_TYPE='innosetup'
ARCHIVE_BASE_GOG_FR_6_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.511.410.2)_(french)_(68368)-1.bin'
ARCHIVE_BASE_GOG_FR_6_PART1_MD5='
c003bb4e2973fb7487bef0b50ed46c6a
01194704a7c9b1e7c67e4743bcdb81f7'
ARCHIVE_BASE_GOG_FR_6_SIZE='4184265'
ARCHIVE_BASE_GOG_FR_6_VERSION='1.2-gog68368'
ARCHIVE_BASE_GOG_FR_6_URL='https://www.gog.com/game/vampire_the_masquerade_bloodlines'

ARCHIVE_BASE_GOG_EN_4_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_hotfix_(55110).exe'
ARCHIVE_BASE_GOG_EN_4_MD5='9a1c0824945008a3d520103002479fbc'
ARCHIVE_BASE_GOG_EN_4_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_4_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_hotfix_(55110)-1.bin'
ARCHIVE_BASE_GOG_EN_4_PART1_MD5='50cbbf0ad7063e089ba4663cb599b7ac'
ARCHIVE_BASE_GOG_EN_4_SIZE='4300000'
ARCHIVE_BASE_GOG_EN_4_VERSION='1.2-gog55110'

ARCHIVE_BASE_GOG_FR_4_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_hotfix_(french)_(55110).exe'
ARCHIVE_BASE_GOG_FR_4_MD5='2cf2aa09a1bfbd3151ded2457037f2ab'
ARCHIVE_BASE_GOG_FR_4_TYPE='innosetup'
ARCHIVE_BASE_GOG_FR_4_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_hotfix_(french)_(55110)-1.bin'
ARCHIVE_BASE_GOG_FR_4_PART1_MD5='3925827a5aa7dfcd53609130ef128a18'
ARCHIVE_BASE_GOG_FR_4_SIZE='4300000'
ARCHIVE_BASE_GOG_FR_4_VERSION='1.2-gog55110'

ARCHIVE_BASE_GOG_EN_3_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_(54206).exe'
ARCHIVE_BASE_GOG_EN_3_MD5='97cc63738b2eab02140c38883d82fa8b'
ARCHIVE_BASE_GOG_EN_3_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_3_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_(54206)-1.bin'
ARCHIVE_BASE_GOG_EN_3_PART1_MD5='5024eb1ddef8ec13c2ed433d399193cf'
ARCHIVE_BASE_GOG_EN_3_SIZE='4300000'
ARCHIVE_BASE_GOG_EN_3_VERSION='1.2-gog54206'

ARCHIVE_BASE_GOG_FR_3_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_(french)_(54206).exe'
ARCHIVE_BASE_GOG_FR_3_MD5='80bb9fab0f97230989b5f15aa5118f51'
ARCHIVE_BASE_GOG_FR_3_TYPE='innosetup'
ARCHIVE_BASE_GOG_FR_3_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_11.111.010.2)_(french)_(54206)-1.bin'
ARCHIVE_BASE_GOG_FR_3_PART1_MD5='3624eb7399c9d260889ab245f9bd43b1'
ARCHIVE_BASE_GOG_FR_3_SIZE='4300000'
ARCHIVE_BASE_GOG_FR_3_VERSION='1.2-gog54206'

ARCHIVE_BASE_GOG_EN_2_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.2)_(28160).exe'
ARCHIVE_BASE_GOG_EN_2_MD5='8c1907871d2ded8afda77d5b570d5383'
ARCHIVE_BASE_GOG_EN_2_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_2_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.2)_(28160)-1.bin'
ARCHIVE_BASE_GOG_EN_2_PART1_MD5='a28edc25dc3c0f818673196852490628'
ARCHIVE_BASE_GOG_EN_2_SIZE='4100000'
ARCHIVE_BASE_GOG_EN_2_VERSION='1.2-gog28160'

ARCHIVE_BASE_GOG_FR_2_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.2)_(french)_(28160).exe'
ARCHIVE_BASE_GOG_FR_2_MD5='8877c5ab14363b249e72034fe5333921'
ARCHIVE_BASE_GOG_FR_2_TYPE='innosetup'
ARCHIVE_BASE_GOG_FR_2_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.2)_(french)_(28160)-1.bin'
ARCHIVE_BASE_GOG_FR_2_PART1_MD5='0dddbbcd2dee5474066b4863c56aa5f0'
ARCHIVE_BASE_GOG_FR_2_SIZE='4200000'
ARCHIVE_BASE_GOG_FR_2_VERSION='1.2-gog28160'

ARCHIVE_BASE_GOG_EN_1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.0)_(22135).exe'
ARCHIVE_BASE_GOG_EN_1_MD5='095771daf8fd1b26d34a099f182c8d4a'
ARCHIVE_BASE_GOG_EN_1_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_1_PART1_NAME='setup_vampire_the_masquerade_-_bloodlines_1.2_(up_10.0)_(22135)-1.bin'
ARCHIVE_BASE_GOG_EN_1_PART1_MD5='ef8a3fe212da189d811fcf6bc70a1e40'
ARCHIVE_BASE_GOG_EN_1_SIZE='4100000'
ARCHIVE_BASE_GOG_EN_1_VERSION='1.2-gog22135'

ARCHIVE_BASE_GOG_EN_0_NAME='setup_vtmb_1.2_(up_9.7_basic)_(11362).exe'
ARCHIVE_BASE_GOG_EN_0_MD5='62b8db3b054595fb46bd8eaa5f8ae7bc'
ARCHIVE_BASE_GOG_EN_0_TYPE='innosetup'
ARCHIVE_BASE_GOG_EN_0_PART1_NAME='setup_vtmb_1.2_(up_9.7_basic)_(11362)-1.bin'
ARCHIVE_BASE_GOG_EN_0_PART1_MD5='4177042d5a6e03026d52428e900e6137'
ARCHIVE_BASE_GOG_EN_0_SIZE='4100000'
ARCHIVE_BASE_GOG_EN_0_VERSION='1.2-gog11362'

CONTENT_PATH_DEFAULT_DOTEMU='.'
CONTENT_PATH_DEFAULT_GOG='.'
CONTENT_PATH_DEFAULT_GOG_EN_0='app'
# The game fails to launch if any of the following libraries is missing.
CONTENT_LIBS_BIN_FILES='
bin/launcher.dll
bin/filesystem_stdio.dll
bin/materialsystem.dll
bin/engine.dll
bin/vgui2.dll
bin/vguimatsurface.dll
bin/shaderapidx9.dll
bin/VPHYSICS.DLL
bin/StudioRender.dll
vampire/cl_dlls/GameUI.dll'
# loader.dll is only used by the GOG build.
CONTENT_LIBS_BIN_FILES_GOG="$CONTENT_LIBS_BIN_FILES
loader.dll"
# launcher.dll requirements
CONTENT_LIBS0_BIN_FILES='
bin/vstdlib.dll
bin/tier0.dll'
# engine.dll requirements
CONTENT_LIBS1_BIN_FILES='
bin/binkw32.dll
bin/vampire_python21.dll'
# The game crashes after an initial loading screen if the following library is missing.
CONTENT_LIBS2_BIN_FILES='
bin/vaudio_miles.dll'
# vaudio_miles.dll requirements
CONTENT_LIBS3_BIN_FILES='
bin/mss32.dll'
# The game errors out after an initial loading screen if the following library is missing.
CONTENT_LIBS4_BIN_FILES='
vampire/cl_dlls/client.dll'
# The game menu is displayed as wireframe if any of the following libraries is missing.
CONTENT_LIBS5_BIN_FILES='
bin/stdshader_dx6.dll
bin/stdshader_dx7.dll
bin/stdshader_dx8.dll
bin/stdshader_dx9.dll'
# The game crashes when trying to start a new game if the following library is missing.
CONTENT_LIBS6_BIN_FILES='
vampire/dlls/vampire.dll'
CONTENT_GAME_BIN_FILES_DOTEMU='
bin/mssmp3.asi
bin/mssvoice.asi
vampire.exe'
CONTENT_GAME_BIN_FILES_GOG='
bin/loader
bin/mssmp3.asi
bin/mssvoice.asi
loader.exe
vampire.exe'
# These paths are created at the "Extract game data" step.
CONTENT_GAME_L10N_EN_PATH_DOTEMU='l10n-en'
CONTENT_GAME_L10N_FR_PATH_DOTEMU='l10n-fr'
CONTENT_GAME_L10N_FILES='
vampire/pack101.vpk
vampire/pack103.vpk'
CONTENT_GAME_L10N_EN_FILES="$CONTENT_GAME_L10N_FILES"
CONTENT_GAME_L10N_FR_FILES="$CONTENT_GAME_L10N_FILES"
CONTENT_GAME_DATA_FILES='
vampire/maps
vampire/media
vampire/pack000.vpk
vampire/pack001.vpk
vampire/pack002.vpk
vampire/pack003.vpk
vampire/pack004.vpk
vampire/pack005.vpk
vampire/pack006.vpk
vampire/pack007.vpk
vampire/pack008.vpk
vampire/pack009.vpk
vampire/pack010.vpk
vampire/pack100.vpk
vampire/pack102.vpk
vampire/python
vampire/resource
vampire/sound
vtmbup-loader.txt
vtmbup-readme.txt
*.dat
*.mpg
*.tth'
# These paths are created at the "Extract game data" step.
CONTENT_DOC_L10N_EN_PATH_DOTEMU='l10n-en'
CONTENT_DOC_L10N_FR_PATH_DOTEMU='l10n-fr'
CONTENT_DOC_L10N_FILES='
docs
*.pdf'
CONTENT_DOC_L10N_EN_FILES="$CONTENT_DOC_L10N_FILES"
CONTENT_DOC_L10N_FR_FILES="$CONTENT_DOC_L10N_FILES"

USER_PERSISTENT_DIRECTORIES='
vampire/cfg
vampire/maps/graphs
vampire/python
vampire/save'
USER_PERSISTENT_FILES='
vampire/vidcfg.bin'

WINE_VIRTUAL_DESKTOP='auto'

APP_MAIN_EXE_DOTEMU='vampire.exe'
# Using vampire.exe directly with the GOG build leads to lingering processes.
# cf. https://bugs.winehq.org/show_bug.cgi?id=56713
# (WINE 9.9)
APP_MAIN_EXE_GOG='loader.exe'
APP_MAIN_ICON='vampire.exe'
# Work around mysterious disappearance of zlib1.dll.
# (WINE 10.0)
APP_MAIN_PRERUN='
# Work around mysterious disappearance of zlib1.dll.
system_library="${WINEPREFIX}/drive_c/windows/system32/zlib1.dll"
if \
	[ ! -e "$system_library" ] &&
	[ -e "${system_library}.bak" ]
then
	mv "${system_library}.bak" "$system_library"
fi
'
# Keep hold of the terminal until the game process actually ends,
# instead of returning as soon as the game is started.
APP_MAIN_POSTRUN='
# Keep hold of the terminal until the game process actually ends,
# instead of returning as soon as the game is started.
sleep 10s
pidwait --euid "$USER" --ignore-case vampire.exe
'

PACKAGES_LIST_DOTEMU='
PKG_BIN
PKG_L10N_EN
PKG_L10N_FR
PKG_DATA'
PACKAGES_LIST_GOG_EN='
PKG_BIN
PKG_L10N_EN
PKG_DATA'
PACKAGES_LIST_GOG_FR='
PKG_BIN
PKG_L10N_FR
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'
PKG_BIN_DEPENDENCIES_COMMANDS='
pidwait'

PKG_L10N_BASE_ID="${GAME_ID}-l10n"
PKG_L10N_EN_ID="${PKG_L10N_BASE_ID}-en"
PKG_L10N_FR_ID="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_EN_ENSCRIPTION='English localization'
PKG_L10N_FR_FRSCRIPTION='French localization'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'

# DotEmu archive
# - unzip is required to extract the content of inner archives.
# - convmv is used to convert file paths to lower case.
REQUIREMENTS_LIST_DOTEMU='
unzip
convmv'

# Load common functions.

# Scan library paths used by ./play.it ≥ 2.35.
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
# Scan library paths used by ./play.it < 2.35.
# This can go away when PLAYIT_COMPATIBILITY_LEVEL is bumped to 2.35 or higher.
if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$PWD" \
		"${XDG_DATA_HOME:="${HOME}/.local/share"}/play.it" \
		'/usr/local/share/games/play.it' \
		'/usr/local/share/play.it' \
		'/usr/share/games/play.it' \
		'/usr/share/play.it'
	do
		playit_lib_path="${playit_lib_dir}/libplayit2.sh"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
# Error out if the library can not be found.
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
case "$(current_archive)" in
	('ARCHIVE_BASE_DOTEMU_'*)
		(
			cd "${PLAYIT_WORKDIR}/gamedata/$(content_path_default)"

			localised_paths='
			Docs
			Vampire/pack101.vpk
			Vampire/pack103.vpk'
			localised_paths_en="
			$localised_paths
			manual_en.pdf"
			localised_paths_fr="
			$localised_paths
			Manual_fr.pdf"

			ARCHIVE_COMMON1_PATH="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/common1.zip"
			archive_extraction 'ARCHIVE_COMMON1'
			rm "$(archive_path 'ARCHIVE_COMMON1')"

			ARCHIVE_COMMON2_PATH="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/common2.zip"
			archive_extraction 'ARCHIVE_COMMON2'
			rm "$(archive_path 'ARCHIVE_COMMON2')"

			ARCHIVE_EN_PATH="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/en.zip"
			archive_extraction 'ARCHIVE_EN'
			rm "$(archive_path 'ARCHIVE_EN')"
			# Localised paths are moved out of the way to prevent them from being overwritten.
			mkdir 'l10n-en'
			# Silence a ShellCheck false positive, word splitting is expected here.
			# shellcheck disable=SC2086
			cp --link --parents --recursive $localised_paths_en 'l10n-en'
			# shellcheck disable=SC2086
			rm --recursive $localised_paths
	
			ARCHIVE_FR_PATH="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/fr.zip"
			archive_extraction 'ARCHIVE_FR'
			rm "$(archive_path 'ARCHIVE_FR')"
			# Localised paths are moved out of the way to prevent them from being overwritten.
			mkdir 'l10n-fr'
			# Silence a ShellCheck false positive, word splitting is expected here.
			# shellcheck disable=SC2086
			cp --link --parents --recursive $localised_paths_fr 'l10n-fr'
			# shellcheck disable=SC2086
			rm --recursive $localised_paths

			tolower .
		)
	;;
esac

# Include game data

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Give access to character histories (backgrounds).

config_file="$(package_path 'PKG_BIN')$(path_game_data)/vampire/cfg/config.cfg"
mkdir --parents "$(dirname "$config_file")"
cat > "$config_file" <<- EOF
vchar_edit_histories "1"
EOF

# 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
case "$(current_archive)" in
	('ARCHIVE_BASE_DOTEMU_'*)
		case "$(messages_language)" in
			('fr')
				lang_string='version %s :'
				lang_en='anglaise'
				lang_fr='française'
			;;
			('en'|*)
				lang_string='%s version:'
				lang_en='English'
				lang_fr='French'
			;;
		esac
		printf '\n'
		printf "$lang_string" "$lang_en"
		print_instructions 'PKG_BIN' 'PKG_L10N_EN' 'PKG_DATA'
		printf "$lang_string" "$lang_fr"
		print_instructions 'PKG_BIN' 'PKG_L10N_FR' 'PKG_DATA'
	;;
	(*)
		print_instructions
	;;
esac

# Clean up.

working_directory_cleanup

exit 0
