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

###
# Vampire: The Masquerade - Stories of New York series:
# - Vampire: The Masquerade - Coteries of New York
# - Vampire: The Masquerade - Shadows of New York
# - Vampire: The Masquerade - Reckoning of New York
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260330.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_COTERIES='vampire-the-masquerade-coteries-of-new-york'
GAME_NAME_COTERIES='Vampire: The Masquerade - Coteries of New York'

GAME_ID_SHADOWS='vampire-the-masquerade-shadows-of-new-york'
GAME_NAME_SHADOWS='Vampire: The Masquerade - Shadows of New York'

GAME_ID_RECKONING='vampire-the-masquerade-reckoning-of-new-york'
GAME_NAME_RECKONING='Vampire: The Masquerade - Reckoning of New York'

ARCHIVE_BASE_COTERIES_2_NAME='vampire_the_masquerade_coteries_of_new_york_1_0_12_52098.sh'
ARCHIVE_BASE_COTERIES_2_MD5='035735d7a7717c4c31cce156511ec8b0'
ARCHIVE_BASE_COTERIES_2_SIZE='3300000'
ARCHIVE_BASE_COTERIES_2_VERSION='1.0.12-gog52098'
ARCHIVE_BASE_COTERIES_2_URL='https://www.gog.com/game/vampire_the_masquerade_coteries_of_new_york'

ARCHIVE_BASE_COTERIES_1_NAME='vampire_the_masquerade_coteries_of_new_york_1_0_9_45543.sh'
ARCHIVE_BASE_COTERIES_1_MD5='6310c15fabd1768d00faa57f83f2de5e'
ARCHIVE_BASE_COTERIES_1_SIZE='3300000'
ARCHIVE_BASE_COTERIES_1_VERSION='1.0.9-gog45543'

ARCHIVE_BASE_COTERIES_0_NAME='vampire_the_masquerade_coteries_of_new_york_1_0_7_40980.sh'
ARCHIVE_BASE_COTERIES_0_MD5='146113ea6b7295104413d8ce2b1fbf4e'
ARCHIVE_BASE_COTERIES_0_SIZE='3200000'
ARCHIVE_BASE_COTERIES_0_VERSION='1.0.7-gog40980'

ARCHIVE_BASE_SHADOWS_1_NAME='vampire_the_masquerade_shadows_of_new_york_1_0_1_51032.sh'
ARCHIVE_BASE_SHADOWS_1_MD5='0ec3a59f889b30e7d632f65dcdf0ac6b'
ARCHIVE_BASE_SHADOWS_1_SIZE='3319144'
ARCHIVE_BASE_SHADOWS_1_VERSION='1.0.1-gog51032'
ARCHIVE_BASE_SHADOWS_1_URL='https://www.gog.com/game/vampire_the_masquerade_shadows_of_new_york'

ARCHIVE_BASE_SHADOWS_0_NAME='vampire_the_masquerade_shadows_of_new_york_1_0_0_41075.sh'
ARCHIVE_BASE_SHADOWS_0_MD5='e7880d6e417f81f47b079c9cdc4fb907'
ARCHIVE_BASE_SHADOWS_0_SIZE='3300000'
ARCHIVE_BASE_SHADOWS_0_VERSION='1.0.0-gog41075'

ARCHIVE_BASE_RECKONING_1_NAME='setup_vtm_reckoning_of_new_york_testks_(85858).exe'
ARCHIVE_BASE_RECKONING_1_MD5='04996b98511c4c1555a8056339f800e5'
ARCHIVE_BASE_RECKONING_1_TYPE='innosetup'
ARCHIVE_BASE_RECKONING_1_SIZE='745188'
ARCHIVE_BASE_RECKONING_1_VERSION='1.0.2-gog85858'
ARCHIVE_BASE_RECKONING_1_URL='https://www.gog.com/game/vampire_the_masquerade_reckoning_of_new_york'

ARCHIVE_BASE_RECKONING_0_NAME='setup_vampire_the_masquerade_-_reckoning_of_new_york_1.0.1.1147_(64bit)_(76185).exe'
ARCHIVE_BASE_RECKONING_0_MD5='8dd698c31c0f26818b109543436ea94f'
ARCHIVE_BASE_RECKONING_0_TYPE='innosetup'
ARCHIVE_BASE_RECKONING_0_SIZE='792005'
ARCHIVE_BASE_RECKONING_0_VERSION='1.0.1-gog76185'

UNITY3D_NAME_COTERIES='VtM Coteries of New York'
UNITY3D_NAME_SHADOWS='Vampire the Masquerade - Shadows of New York'
UNITY3D_NAME_RECKONING='VtM Reckoning of New York'
UNITY3D_PLUGINS_COTERIES='
libfmod.so
libfmodstudio.so'
UNITY3D_PLUGINS_SHADOWS='
libfmod.so
libfmodstudio.so'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# Prevent a freeze during the initial loading.
UNITY3D_PLUGINS_RECKONING='
GalaxyCSharpGlue.dll
fmodstudio.dll'
# GalaxyCSharpGlue.dll requirement
UNITY3D_PLUGINS_RECKONING="$UNITY3D_PLUGINS_RECKONING
Galaxy64.dll"

CONTENT_PATH_DEFAULT_LINUX='data/noarch/game'
CONTENT_PATH_DEFAULT_COTERIES="$CONTENT_PATH_DEFAULT_LINUX"
CONTENT_PATH_DEFAULT_SHADOWS="$CONTENT_PATH_DEFAULT_LINUX"
CONTENT_PATH_DEFAULT_RECKONING='.'
# VtM Reckoning of New York.exe requirements
CONTENT_LIBS_BIN_FILES_RECKONING='
UnityPlayer.dll'
# Prevent a fatal error on launch.
CONTENT_LIBS0_BIN_FILES_RECKONING='
GameAssembly.dll'
# GameAssembly.dll requirements
CONTENT_LIBS1_BIN_FILES_RECKONING='
baselib.dll'

WINE_PERSISTENT_DIRECTORIES_RECKONING='
users/${USER}/AppData/LocalLow/DrawDistance/VtM Reckoning of New York'

# Default to windowed mode on first launch.
APP_MAIN_PRERUN_COMMON='
config_file="${config_directory}/Settings.save"
if [ ! -e "$config_file" ]; then
	mkdir --parents "$config_directory"
	cat > "$config_file" <<- EOF
	{"FullScreenMode":false}
	EOF
fi
'
APP_MAIN_PRERUN_COTERIES='
# Default to windowed mode on first launch.
config_directory="${XDG_CONFIG_HOME:="$HOME/.config"}/unity3d/DrawDistance/VtM Coteries of New York/Saves"
'"${APP_MAIN_PRERUN_COMMON}"
APP_MAIN_PRERUN_SHADOWS='
# Default to windowed mode on first launch
config_directory="${XDG_CONFIG_HOME:="$HOME/.config"}/unity3d/DrawDistance/VtM Shadows of New York/Saves"
'"${APP_MAIN_PRERUN_COMMON}"

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_LIBRARIES_LINUX='
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libpthread.so.0
librt.so.1
libstdc++.so.6
libz.so.1'
PKG_BIN_DEPENDENCIES_LIBRARIES_COTERIES="$PKG_BIN_DEPENDENCIES_LIBRARIES_LINUX"
PKG_BIN_DEPENDENCIES_LIBRARIES_SHADOWS="$PKG_BIN_DEPENDENCIES_LIBRARIES_LINUX"

PKG_DATA_ID_COTERIES="${GAME_ID_COTERIES}-data"
PKG_DATA_ID_SHADOWS="${GAME_ID_SHADOWS}-data"
PKG_DATA_ID_RECKONING="${GAME_ID_RECKONING}-data"
PKG_DATA_DESCRIPTION='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 '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'
# Reckoning of New York - Include Unity3D plugins.
case "$(current_archive)" in
	('ARCHIVE_BASE_RECKONING_'*)
		unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins/x86_64" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
		# Prevent inclusion of unwanted shipped libraries.
		content_inclusion_default_libraries 'PKG_BIN'
		rm --recursive "$(path_archive_contents)/$(unity3d_name)_Data/Plugins"
	;;
esac
content_inclusion_default

# Write launchers.

# Reckoning of New York - 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
}

# Coteries of New York / Shadows of New York - Shipped libraries expect an executable stack.
case "$(current_archive)" in
	('ARCHIVE_BASE_COTERIES_'*|'ARCHIVE_BASE_SHADOWS_'*)
		game_exec_line() {
			# shellcheck disable=SC2086
			local application=$1

			local application_exe
			application_exe=$(application_exe "$application")
			cat <<- EOF
			GLIBC_TUNABLES=glibc.rtld.execstack=2 \
				"./${application_exe}" -logFile "./logs/\$(date +%F-%R).log" "\$@"
			EOF
		}
	;;
esac

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
