#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: © 2015 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2019 BetaRays <array@dotslashplay.it>
set -o errexit

###
# The Witcher
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260330.7

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='the-witcher-1'
GAME_NAME='The Witcher'

ARCHIVE_BASE_3_NAME='setup_the_witcher_enhanced_edition_directors_cut_1.5_(cs)_gog_0.2_(77554).exe'
ARCHIVE_BASE_3_MD5='
5b6f7dd2247e5fbb1d0049dcc5de6b63
97cffb60d12f41bf03c84165a79a9157
3d8467c76abe8cd16711ada268e1600f'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_the_witcher_enhanced_edition_directors_cut_1.5_(cs)_gog_0.2_(77554)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='
8d242faffa4b3b8dc09ff8697401ece3
12107f584732052c4e11e09657df655a
149adafe38e79c246103e664613558c7'
ARCHIVE_BASE_3_PART2_NAME='setup_the_witcher_enhanced_edition_directors_cut_1.5_(cs)_gog_0.2_(77554)-2.bin'
ARCHIVE_BASE_3_PART2_MD5='
869b510afebbd14e208a5252392662c7
ee77b189cf42bde0e89ea86c071b830e
baa94a6d83dc3a4703d8c414b4442ce5'
ARCHIVE_BASE_3_PART3_NAME='setup_the_witcher_enhanced_edition_directors_cut_1.5_(cs)_gog_0.2_(77554)-3.bin'
ARCHIVE_BASE_3_PART3_MD5='
d9fc78540f616d0deb1fe83a91032138
b5fd3359b7129903a13baf731496f0c7
5b74090cd3314dd0db1849c0dce5aa84'
ARCHIVE_BASE_3_SIZE='14785291'
ARCHIVE_BASE_3_VERSION='1.5.726-gog77554'
ARCHIVE_BASE_3_URL='https://www.gog.com/game/the_witcher'

ARCHIVE_BASE_0_NAME='setup_the_witcher_enhanced_edition_1.5_(a)_(10712).exe'
ARCHIVE_BASE_0_MD5='2440cfb5fb4890ff4b9bc4b88b434d38'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_the_witcher_enhanced_edition_1.5_(a)_(10712)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='e530a1a2e86094740b45a14f63260804'
ARCHIVE_BASE_0_PART2_NAME='setup_the_witcher_enhanced_edition_1.5_(a)_(10712)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='fb3a478bcb6e4702e1e8d392cb55391d'
ARCHIVE_BASE_0_PART3_NAME='setup_the_witcher_enhanced_edition_1.5_(a)_(10712)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='2df8369af401815a736f5d88f85fbf8d'
ARCHIVE_BASE_0_SIZE='15000000'
ARCHIVE_BASE_0_VERSION='1.5.726-gog10712'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/the_witcher'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_GAME_BIN_FILES='
System/witcher.exe
System/witcher.ini'
# witcher.exe requirements
CONTENT_LIBS_BIN_FILES='
System/binkw32.dll
System/mss32.dll
System/CommonLibs.dll'
CONTENT_GAME0_BIN_FILES='
System/Miles
System/Scripts'
CONTENT_GAME_L10N_VOICES_PL_FILES='
Data/voices/M?_5_00.bif
Data/voices_5_00.bif'
CONTENT_GAME_L10N_VOICES_FR_FILES='
Data/voices/M?_11_00.bif
Data/voices_11_00.bif'
CONTENT_GAME_L10N_VOICES_EN_FILES='
Data/voices/M?_3_00.bif
Data/voices_3_00.bif'
CONTENT_GAME_DATA_FILES='
Data/Cutscenes
Data/Dialogues
Data/Meshes
Data/modules
Data/Movies
Data/Music
Data/Scripts
Data/Templates
Data/attackeffects.2da
Data/languages.2da
Data/2da00.bif
Data/cutscenes00.bif
Data/dialogues00.bif
Data/fx00.bif
Data/items00.bif
Data/legacy00.bif
Data/localized00.bif
Data/materials00.bif
Data/meshes00.bif
Data/mg_poker00.bif
Data/music00.bif
Data/physics00.bif
Data/quests00.bif
Data/scripts00.bif
Data/sounds00.bif
Data/templates00.bif
Data/textures00.bif
Data/textures01.bif
Data/trees00.bif
Data/*.bik
Data/*.key
Data/*.tlk'
# These files are required for text display.
CONTENT_GAME0_DATA_FILES='
System/__cache/*.fontcache'
CONTENT_DOC_DATA_FILES='
manual.pdf
readme.rtf
release.txt'
# "Enhanced Edition" add-ons
CONTENT_ADDONS_RELATIVE_PATH='__support/add/The Witcher'
CONTENT_ADDONS_FILES='
*.adv'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/The Witcher'
WINE_REGEDIT_PERSISTENT_KEYS='
HKEY_CURRENT_USER\Software\CD Projekt RED\Witcher\Bindings
HKEY_CURRENT_USER\Software\CD Projekt RED\Witcher\Settings'
install_folder="C:\\\\${GAME_ID}\\\\"
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\CD Projekt Red\The Witcher]

"InstallFolder"="'"${install_folder}"'"
"IsDjinniInstalled"=dword:00000000
"Language"="3"
"RegionVersion"="WE"
'
WINE_REGISTRY_INIT_L10N_VOICES_PL="${WINE_REGISTRY_INIT}"'
[HKEY_CURRENT_USER\Software\CD Projekt RED\Witcher\Settings]

"VoiceLanguage"="5"
'
WINE_REGISTRY_INIT_L10N_VOICES_FR="${WINE_REGISTRY_INIT}"'
[HKEY_CURRENT_USER\Software\CD Projekt RED\Witcher\Settings]

"VoiceLanguage"="11"
'
WINE_REGISTRY_INIT_L10N_VOICES_EN="${WINE_REGISTRY_INIT}"'
[HKEY_CURRENT_USER\Software\CD Projekt RED\Witcher\Settings]

"VoiceLanguage"="3"
'
# Work around texture display problems.
# A visible symptom in early game is lightning shifts based on distance,
# with far objects showing too much light until the player gets close enough.
# cf. https://bugs.winehq.org/show_bug.cgi?id=46553
# (WINE 11.5)
WINE_WINETRICKS_VERBS='d3dx9_35'

APP_MAIN_EXE='System/witcher.exe'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_VOICES_PL
PKG_L10N_VOICES_FR
PKG_L10N_VOICES_EN
PKG_DATA
PKG_ADDONS'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_VOICES
PKG_DATA'

PKG_L10N_VOICES_ID="${GAME_ID}-l10n-voices"
PKG_L10N_VOICES_PL_ID="${PKG_L10N_VOICES_ID}-pl"
PKG_L10N_VOICES_FR_ID="${PKG_L10N_VOICES_ID}-fr"
PKG_L10N_VOICES_EN_ID="${PKG_L10N_VOICES_ID}-en"
PKG_L10N_VOICES_PROVIDES="
$PKG_L10N_VOICES_ID"
PKG_L10N_VOICES_PL_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_FR_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_EN_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_PL_DESCRIPTION='Polish voices'
PKG_L10N_VOICES_FR_DESCRIPTION='French voices'
PKG_L10N_VOICES_EN_DESCRIPTION='English voices'
# Easier upgrades from pre-20260329.3 packages
PKG_L10N_VOICES_PROVIDES="${PKG_L10N_VOICES_PROVIDES}
the-witcher-1-voices
the-witcher-1-data-voices"
PKG_L10N_VOICES_PL_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_FR_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_EN_PROVIDES="$PKG_L10N_VOICES_PROVIDES"

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

PKG_ADDONS_ID="${GAME_ID}-addons"
PKG_ADDONS_DESCRIPTION='Enhanced Edition add-ons'
PKG_ADDONS_DEPENDENCIES_SIBLINGS='
PKG_BIN'

# 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'
# Include "Enhanced Edition" add-ons.
content_inclusion 'ADDONS' 'PKG_ADDONS' "$(path_game_data)/addons"
content_inclusion_default

# Write launchers.

# Set voices localisation on first launch.
for package in \
	'PKG_L10N_VOICES_PL' \
	'PKG_L10N_VOICES_FR' \
	'PKG_L10N_VOICES_EN'
do (
	set_current_package "$package"
	# shellcheck disable=SC2218
	wine_registry_script_write "$package"
) done
wine_registry_script_write() { return 0; }

# Custom WINEPREFIX actions:
# - Install shipped libraries.
# - Include "Enhanced Edition" add-ons.
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 --force --symbolic "$dll_source" "$dll_destination"
	done
	EOF
	cat <<- 'EOF'
	# Include "Enhanced Edition" add-ons.
	if [ -e "${PATH_GAME_DATA}/addons" ]; then
	    install -D --mode=644 \
	        --target-directory="${WINEPREFIX}/drive_c/users/Public/Documents/The Witcher" \
	        "${PATH_GAME_DATA}/addons"/*.adv
	fi
	EOF
}

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		lang_string='voix %s :'
		lang_pl='polonaises'
		lang_fr='françaises'
		lang_en='anglaises'
	;;
	('en'|*)
		lang_string='%s voices:'
		lang_pl='Polish'
		lang_fr='French'
		lang_en='English'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_pl"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_ADDONS' 'PKG_L10N_VOICES_PL'
printf "$lang_string" "$lang_fr"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_ADDONS' 'PKG_L10N_VOICES_FR'
printf "$lang_string" "$lang_en"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_ADDONS' 'PKG_L10N_VOICES_EN'

# Clean up.

working_directory_cleanup

exit 0
