#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2023 Fabien Givors <captnfab@debian-facile.org>
# SPDX-FileCopyrightText: © 2025 Antoine Le Gonidec <vv221@dotslashplay.it>
set -o errexit

###
# Kingdom Come: Deliverance
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260528.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='kingdom-come-deliverance'
GAME_NAME='Kingdom Come: Deliverance'

ARCHIVE_BASE_0_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297).exe'
ARCHIVE_BASE_0_MD5='de458c20cbcaadf8dfc92afe7c2e8284'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='c18fa417f8486275d78e01c1a62c8204'
ARCHIVE_BASE_0_PART2_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='d288e846badd8228ba0f78c6186731f2'
ARCHIVE_BASE_0_PART3_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='b6702c24ded1f8851cb297553cc9b71b'
ARCHIVE_BASE_0_PART4_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='b33e86210161017d399071fcf6c54f9f'
ARCHIVE_BASE_0_PART5_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-5.bin'
ARCHIVE_BASE_0_PART5_MD5='379faac2d6128f1a94275b9d9248c0de'
ARCHIVE_BASE_0_PART6_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-6.bin'
ARCHIVE_BASE_0_PART6_MD5='c5ffb38b7425cb0219fe70497bb09d9a'
ARCHIVE_BASE_0_PART7_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-7.bin'
ARCHIVE_BASE_0_PART7_MD5='594f01d2cbd5b2c5cc9442d644a8b335'
ARCHIVE_BASE_0_PART8_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-8.bin'
ARCHIVE_BASE_0_PART8_MD5='93896ac78ea2dddeb433ffa685a561c2'
ARCHIVE_BASE_0_PART9_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-9.bin'
ARCHIVE_BASE_0_PART9_MD5='b8cd2810be97f2f697f67e92c71c16b8'
ARCHIVE_BASE_0_PART10_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-10.bin'
ARCHIVE_BASE_0_PART10_MD5='a38ed45d417c2d2cef03b270a21da255'
ARCHIVE_BASE_0_PART11_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-11.bin'
ARCHIVE_BASE_0_PART11_MD5='671cc5add7a31e5efd46e2a03478cf5d'
ARCHIVE_BASE_0_PART12_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-12.bin'
ARCHIVE_BASE_0_PART12_MD5='9aec35e9d193044603095eb1961d8647'
ARCHIVE_BASE_0_PART13_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-13.bin'
ARCHIVE_BASE_0_PART13_MD5='cb65a86dd5cc63a66a18767c3a4c5378'
ARCHIVE_BASE_0_PART14_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-14.bin'
ARCHIVE_BASE_0_PART14_MD5='701d947164a1f4d541c985a7273b9552'
ARCHIVE_BASE_0_PART15_NAME='setup_kingdom_come_deliverance_1.9.6-404-504czj3_(62297)-15.bin'
ARCHIVE_BASE_0_PART15_MD5='e3bb56e79d9ff4f9be2ee2b321f10f68'
ARCHIVE_BASE_0_SIZE='62899673'
ARCHIVE_BASE_0_VERSION='1.9.6-404-504czj3-gog62297'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
PatchVersion.txt
whdlversions.txt
Engine
system.cfg'
CONTENT_GAME0_BIN_FILES='
bin/Win64/KingdomCome.exe
bin/Win64/version
bin/Win64Shared/sharedversion.txt'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME1_BIN_FILES='
bin/Win64/WHGame.dll
bin/Win64Shared/amd_ags_x64.dll
bin/Win64Shared/AnselSDK64.dll
bin/Win64Shared/d3dcompiler_46.dll
bin/Win64Shared/d3dcompiler_47.dll
bin/Win64Shared/fmod64.dll
bin/Win64Shared/fmodstudio64.dll
bin/Win64Shared/Galaxy64.dll
bin/Win64Shared/GfeSDK.dll
bin/Win64Shared/LightFX.dll
bin/Win64Shared/LightFX64.dll
bin/Win64Shared/LightFXConfigurator64.dll
bin/Win64Shared/sgup_api64.dll
bin/Win64Shared/solstice.dll
bin/Win64Shared/StovePCSDK.dll
bin/Win64Shared/Tobii.GameIntegration.dll'

CONTENT_GAME_L10N_VOICES_CZ_FILES='
Localization/Czech-part?.pak
Localization/IPL_Czech.pak'
CONTENT_GAME_L10N_VOICES_JA_FILES='
Localization/Japanese-part?.pak
Localization/IPL_Japanese.pak'
CONTENT_GAME_L10N_VOICES_DE_FILES='
Localization/German.pak
Localization/IPL_German.pak
Localization/patch/german_patch_*.pak
Localization/patch/ipl_german_patch_*.pak'
CONTENT_GAME_L10N_VOICES_EN_FILES='
Localization/English.pak
Localization/IPL_English.pak
Localization/patch/english_patch_*.pak
Localization/patch/ipl_english_patch_*.pak'
CONTENT_GAME_L10N_VOICES_FR_FILES='
Localization/French.pak
Localization/IPL_French.pak
Localization/patch/french_patch_*.pak
Localization/patch/ipl_french_patch_*.pak'

CONTENT_GAME_DATA_FILES='
Data/gamedata.pak
Data/pak.cfg
Data/music.pak
Data/sounds.pak
Data/cinematics.pak
Data/ipl_cinematics.pak
Data/_fastload/startup_animations.pak
Data/animations-part0.pak
Data/animations-part1.pak
Localization/*_xml.pak
Localization/common.pak'

CONTENT_GAME_DATA_PATCHES_1_FILES='
Data/patch/ipl_patch_010[3-6]??.pak
Data/patch/patch_010[3-6]??.pak'

CONTENT_GAME_DATA_PATCHES_2_FILES='
Data/patch/ipl_patch_010[7-9]??.pak
Data/patch/patch_010[7-9]??.pak'

CONTENT_GAME_DATA_WORLD_FILES='
Data/_fastload/startup_graphics.pak
Data/buildings.pak
Data/engineassets
Data/engineassets/sky
Data/engineassets/sky/optical.lut
Data/geomcaches.pak
Data/levels
Data/objects.pak
Data/scripts.pak
Data/tables.pak
Data/textures.pak'

CONTENT_GAME_DATA_VIDEOS_FILES='
Data/ipl_videos.pak
Data/videos-part0.pak
Data/videos-part1.pak
Data/videos-part2.pak
Data/videos-part3.pak
Data/videos-part4.pak'

CONTENT_GAME_DATA_VIDEOSB_FILES='
Data/ipl_videosb.pak
Data/videosb-part0.pak
Data/videosb-part1.pak'

CONTENT_GAME_DATA_CHARACTERS_FILES='
Data/_fastload/startup_characters.pak
Data/characters.pak
Data/heads.pak
Data/ipl_heads.pak
Data/cloth-part?.pak'

# TODO: Check if DXVK is actually required.
WINE_DIRECT3D_RENDERER='dxvk'
WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Saved Games/kingdomcome/profiles
users/${USER}/Saved Games/kingdomcome/saves'

USER_PERSISTENT_DIRECTORIES='
mods'
USER_PERSISTENT_FILES='
system.cfg
user.cfg.keeptextures
user.cfg.perf
user.cfg.reticle
user.cfg'

APP_MAIN_EXE='bin/Win64/KingdomCome.exe'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_VOICES_EN
PKG_L10N_VOICES_FR
PKG_L10N_VOICES_CZ
PKG_L10N_VOICES_JA
PKG_L10N_VOICES_DE
PKG_DATA
PKG_DATA_PATCHES_1
PKG_DATA_PATCHES_2
PKG_DATA_WORLD
PKG_DATA_VIDEOS
PKG_DATA_VIDEOSB
PKG_DATA_CHARACTERS'

# Binaries packages

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_VOICES
PKG_DATA'

# Localisation packages

PKG_L10N_VOICES_ID="${GAME_ID}-l10n-voices"
PKG_L10N_VOICES_CZ_ID="${PKG_L10N_VOICES_ID}-cz"
PKG_L10N_VOICES_DE_ID="${PKG_L10N_VOICES_ID}-de"
PKG_L10N_VOICES_EN_ID="${PKG_L10N_VOICES_ID}-en"
PKG_L10N_VOICES_FR_ID="${PKG_L10N_VOICES_ID}-fr"
PKG_L10N_VOICES_JA_ID="${PKG_L10N_VOICES_ID}-ja"
PKG_L10N_VOICES_PROVIDES="
$PKG_L10N_VOICES_ID"
PKG_L10N_VOICES_CZ_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_DE_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_EN_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_FR_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_JA_PROVIDES="$PKG_L10N_VOICES_PROVIDES"
PKG_L10N_VOICES_DESCRIPTION="localisations - voices"
PKG_L10N_VOICES_CZ_DESCRIPTION="$PKG_L10N_VOICES_DESCRIPTION - Czech"
PKG_L10N_VOICES_DE_DESCRIPTION="$PKG_L10N_VOICES_DESCRIPTION - Deutch"
PKG_L10N_VOICES_EN_DESCRIPTION="$PKG_L10N_VOICES_DESCRIPTION - English"
PKG_L10N_VOICES_FR_DESCRIPTION="$PKG_L10N_VOICES_DESCRIPTION - French"
PKG_L10N_VOICES_JA_DESCRIPTION="$PKG_L10N_VOICES_DESCRIPTION - Japanese"

# Data packages

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_PATCHES_1
PKG_DATA_PATCHES_2
PKG_DATA_WORLD
PKG_DATA_VIDEOS
PKG_DATA_VIDEOSB
PKG_DATA_CHARACTERS'
# Easier upgrades from pre-20260527.1 packages
PKG_DATA_PROVIDES='
kingdom-come-deliverance-data-base
kingdom-come-deliverance-data-audio
kingdom-come-deliverance-data-cinematics
kingdom-come-deliverance-data-animations'
# Easier upgrades from pre-20260528.1 packages
PKG_DATA_PROVIDES="$PKG_DATA_PROVIDES
kingdom-come-deliverance-l10n-common"

PKG_DATA_WORLD_ID="${PKG_DATA_ID}-world"
PKG_DATA_WORLD_DESCRIPTION='data world'

PKG_DATA_VIDEOS_ID="${PKG_DATA_ID}-videos"
PKG_DATA_VIDEOS_DESCRIPTION='data videos'

PKG_DATA_VIDEOSB_ID="${PKG_DATA_ID}-videosb"
PKG_DATA_VIDEOSB_DESCRIPTION='data videosb'

PKG_DATA_CHARACTERS_ID="${PKG_DATA_ID}-characters"
PKG_DATA_CHARACTERS_DESCRIPTION='data characters and clothes'
# Easier upgrades from pre-20260527.1 packages
PKG_DATA_CHARACTERS_PROVIDES='
kingdom-come-deliverance-data-clothes'

PKG_DATA_PATCHES_ID="${PKG_DATA_ID}-patches"
PKG_DATA_PATCHES_1_ID="${PKG_DATA_PATCHES_ID}-1"
PKG_DATA_PATCHES_2_ID="${PKG_DATA_PATCHES_ID}-2"
PKG_DATA_PATCHES_1_DESCRIPTION='data patches 1.3 up to 1.6'
PKG_DATA_PATCHES_2_DESCRIPTION='data patches 1.7 up to 1.9'
# Easier upgrades from pre-20260527.1 packages
PKG_DATA_PATCHES_1_PROVIDES='
kingdom-come-deliverance-data-patch-103
kingdom-come-deliverance-data-patch-104
kingdom-come-deliverance-data-patch-105
kingdom-come-deliverance-data-patch-106'
PKG_DATA_PATCHES_2_PROVIDES='
kingdom-come-deliverance-data-patch-107
kingdom-come-deliverance-data-patch-108
kingdom-come-deliverance-data-patch-109'

# 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

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

# Run the game binary from its parent directory.
game_exec_line() {
	cat <<- 'EOF'
	cd bin/Win64
	$(wine_command) KingdomCome.exe "$@"
	EOF
}

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		message='voix %s :'
		lang_fr='françaises'
		lang_cz='tchèques'
		lang_de='allemandes'
		lang_ja='japonaises'
		lang_en='anglaises'
	;;
	('en'|*)
		message='%s voices:'
		lang_fr='French'
		lang_cz='Czech'
		lang_de='German'
		lang_ja='Japanese'
		lang_en='English'
	;;
esac
printf '\n'
printf "$message" "$lang_fr"
print_instructions $(packages_list | grep --invert-match --fixed-strings --regexp='PKG_L10N') 'PKG_L10N_FR'
printf "$message" "$lang_cz"
print_instructions $(packages_list | grep --invert-match --fixed-strings --regexp='PKG_L10N') 'PKG_L10N_CZ'
printf "$message" "$lang_de"
print_instructions $(packages_list | grep --invert-match --fixed-strings --regexp='PKG_L10N') 'PKG_L10N_DE'
printf "$message" "$lang_ja"
print_instructions $(packages_list | grep --invert-match --fixed-strings --regexp='PKG_L10N') 'PKG_L10N_JA'
printf "$message" "$lang_en"
print_instructions $(packages_list | grep --invert-match --fixed-strings --regexp='PKG_L10N') 'PKG_L10N_EN'

# Clean up.

working_directory_cleanup

exit 0
