#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: © 2015 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2016 Mopi
# SPDX-FileCopyrightText: © 2018 Phil Morrell
set -o errexit

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

script_version=20260712.2

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=the-witcher-2
GAME_NAME='The Witcher 2: Assassins of Kings'

ARCHIVE_BASE_1_NAME='the_witcher_2_assassins_of_kings_enhanced_edition_en_release_3_20150306204412_20992.sh'
ARCHIVE_BASE_1_MD5=fd7b85d44e3da7fdf860ab4267574b36
ARCHIVE_BASE_1_SIZE=23575753
ARCHIVE_BASE_1_VERSION=1.3-2

ARCHIVE_BASE_0_NAME='gog_the_witcher_2_assassins_of_kings_enhanced_edition_2.2.0.8.sh'
ARCHIVE_BASE_0_MD5=3fff5123677a7be2023ecdb6af3b82b6
ARCHIVE_BASE_0_SIZE=24000000
ARCHIVE_BASE_0_VERSION=1.3-1

CONTENT_PATH_DEFAULT='data/noarch/game'
# TODO: Can a regular OpenAL build be used instead?
CONTENT_LIBS_MAIN_FILES='
libopenal-eon.so.1'
CONTENT_GAME_MAIN_FILES='
witcher2'
CONTENT_GAME0_MAIN_FILES='
bin
configurator
fontconfig
linux
sdlinput
eONprecompiledShaders32.dat
icudt52l.dat
SDLGamepad.config
witcher2.vpfs
VPFS_registry.vpfsdb'
CONTENT_GAME1_MAIN_FILES='
CookedPC/globals
CookedPC/movies
CookedPC/sounds
CookedPC/*.cache
CookedPC/*.dzip
CookedPC/*.w2scripts'
# TODO: Move the text localisations to dedicated packages.
CONTENT_GAME2_MAIN_FILES='
CookedPC/*.w2strings'
CONTENT_GAME_VOICES_FR_FILES='
CookedPC/fr0.w2speech'
CONTENT_GAME_VOICES_PL_FILES='
CookedPC/pl0.w2speech'
CONTENT_DOC_MAIN_FILES='
linux/licenses
Linux Release Notes.txt
readme_??.rtf'
CONTENT_DOC0_MAIN_PATH='linux/licenses'
CONTENT_DOC0_MAIN_FILES='
licenses.txt
freetype.txt
glew.txt
ICU License - ICU 1.8.1 and later.html
LGPL v2.txt
libcurl.txt
M+ Fonts License.txt
zlib.txt'

APP_MAIN_EXE='witcher2'
APP_MAIN_ICON='linux/icons/witcher2-icon.png'
# TODO: Set speech language on launch, based on the available CookedPC/*.w2speech file.
# This should be set by updating the "Speech" field in
# ~/.local/share/cdprojektred/witcher2/GameDocuments/Witcher 2/config/User.ini
# TODO: Set text language on launch, based on the available CookedPC/*.w2strings file.
# This should be set by updating the "Language" field in
# ~/.local/share/cdprojektred/witcher2/GameDocuments/Witcher 2/config/User.ini

# FIXME: Launch failure
# terminate called after throwing an instance of 'std::logic_error'
#   what():  basic_string::_S_construct null not valid
# Aborted
# TODO: Move to a dedicated package, until it can be fixed.
APP_CONFIG_ID=${GAME_ID}-config
APP_CONFIG_NAME="${GAME_NAME} - configuration"
APP_CONFIG_CAT='Settings'
APP_CONFIG_EXE='configurator'
APP_CONFIG_ICON='linux/icons/witcher2-configurator.png'

PACKAGES_LIST='
PKG_MAIN
PKG_VOICES_FR
PKG_VOICES_PL'

PKG_MAIN_ARCH=32
PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_VOICES'
PKG_MAIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libfreetype.so.6
libgcc_s.so.1
libGL.so.1
libm.so.6
libpthread.so.0
librt.so.1
libSDL2-2.0.so.0
libSDL2_image-2.0.so.0
libstdc++.so.6
libX11.so.6'
# Easier upgrades from pre-20260710.1 packages
PKG_MAIN_PROVIDES='
the-witcher-2-data
the-witcher-2-data-pack1
the-witcher-2-data-pack2'

PKG_VOICES_ID="${GAME_ID}-voices"
PKG_VOICES_FR_ID="${PKG_VOICES_ID}-fr"
PKG_VOICES_PL_ID="${PKG_VOICES_ID}-pl"
PKG_VOICES_PROVIDES=$PKG_VOICES_ID
PKG_VOICES_FR_PROVIDES=$PKG_VOICES_PROVIDES
PKG_VOICES_PL_PROVIDES=$PKG_VOICES_PROVIDES
PKG_VOICES_FR_DESCRIPTION='French voices'
PKG_VOICES_PL_DESCRIPTION='Polish voices'

# 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

# Build pack0.dzip from its chunks.

pack0_path=$(path_archive_contents)/CookedPC/pack0.dzip
mv "${pack0_path}.split00" "$pack0_path"
cat "${pack0_path}.split01" >> "$pack0_path"
rm "${pack0_path}.split01"
cat "${pack0_path}.split02" >> "$pack0_path"
rm "${pack0_path}.split02"

# Include game data.

content_inclusion_icons
content_inclusion_default

# Write launchers.

# Silent load failure with SDL wayland backend
APP_MAIN_PRERUN="$(application_prerun APP_MAIN)
$(launcher_tweak_sdl_force_x11)"

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	(fr)
		lang_string='voix %s :'
		lang_fr='françaises'
		lang_pl='polonaises'
	;;
	(en|*)
		lang_string='%s voices:'
		lang_fr='French'
		lang_pl='Polish'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions PKG_MAIN PKG_VOICES_FR
printf "$lang_string" "$lang_pl"
print_instructions PKG_MAIN PKG_VOICES_PL

# Clean up.

working_directory_cleanup

exit 0
