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

###
# Infinity Engine games (Enhanced Edition):
# - Baldur's Gate Enhanced Edition
# - Baldur's Gate 2 Enhanced Edition
# - Icewind Dale Enhanced Edition
# - Planescape: Torment Enhanced Edition
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260310.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_BG1='baldurs-gate-1-enhanced-edition'
GAME_NAME_BG1='Baldurʼs Gate Enhanced Edition'

GAME_ID_BG2='baldurs-gate-2-enhanced-edition'
GAME_NAME_BG2='Baldurʼs Gate II Enhanced Edition'

GAME_ID_IWD1='icewind-dale-1-enhanced-edition'
GAME_NAME_IWD1='Icewind Dale Enhanced Edition'

GAME_ID_PST='planescape-torment-enhanced-edition'
GAME_NAME_PST='Planescape: Torment Enhanced Edition'

# Baldur's Gate Enhanced Edition

ARCHIVE_BASE_BG1_2_NAME='baldur_s_gate_enhanced_edition_2_6_6_0_p_81762.sh'
ARCHIVE_BASE_BG1_2_MD5='69a65780f1fe8292e47e922f9b16a4be'
ARCHIVE_BASE_BG1_2_SIZE='3263827'
ARCHIVE_BASE_BG1_2_VERSION='2.6.6.0-gog81762'
ARCHIVE_BASE_BG1_2_URL='https://www.gog.com/game/baldurs_gate_enhanced_edition'

ARCHIVE_BASE_BG1_1_NAME='baldur_s_gate_enhanced_edition_2_6_6_0_47291.sh'
ARCHIVE_BASE_BG1_1_MD5='6f7be163ebb80a0fbc9d6331f9c6f09c'
ARCHIVE_BASE_BG1_1_SIZE='3300000'
ARCHIVE_BASE_BG1_1_VERSION='2.6.6.0-gog47291'

ARCHIVE_BASE_BG1_0_NAME='baldur_s_gate_enhanced_edition_2_6_5_0_46477.sh'
ARCHIVE_BASE_BG1_0_MD5='a87444f36602b5059e3c885ec2ff50e1'
ARCHIVE_BASE_BG1_0_SIZE='3300000'
ARCHIVE_BASE_BG1_0_VERSION='2.6.5.0-gog46477'

ARCHIVE_OPTIONAL_ICONS_NAME_BG1='baldurs-gate-1-enhanced-edition_icons.tar.gz'
ARCHIVE_OPTIONAL_ICONS_MD5_BG1='58401cf80bc9f1a9e9a0896f5d74b02a'
ARCHIVE_OPTIONAL_ICONS_URL_BG1='https://downloads.dotslashplay.it/resources/baldurs-gate-1-enhanced-edition/'
CONTENT_ICONS_PATH_BG1='.'
CONTENT_ICONS_FILES_BG1='
16x16
24x42
32x32
48x48
64x64
256x256'

# Baldur's Gate 2 Enhanced Edition

ARCHIVE_BASE_BG2_2_NAME='baldur_s_gate_ii_enhanced_edition_2_6_6_0_p_81762.sh'
ARCHIVE_BASE_BG2_2_MD5='55a058ab9fbc40f78ce1d89bac7370a0'
ARCHIVE_BASE_BG2_2_SIZE='3721955'
ARCHIVE_BASE_BG2_2_VERSION='2.6.6.0-gog81762'
ARCHIVE_BASE_BG2_2_URL='https://www.gog.com/game/baldurs_gate_2_enhanced_edition'

ARCHIVE_BASE_BG2_1_NAME='baldur_s_gate_ii_enhanced_edition_2_6_6_0_47292.sh'
ARCHIVE_BASE_BG2_1_MD5='43b37a554ffb712176ea8709fc98ed84'
ARCHIVE_BASE_BG2_1_SIZE='380000'
ARCHIVE_BASE_BG2_1_VERSION='2.6.6.0-gog47292'

ARCHIVE_BASE_BG2_0_NAME='baldur_s_gate_ii_enhanced_edition_2_6_5_0_46477.sh'
ARCHIVE_BASE_BG2_0_MD5='aa62efd4b1c69f074a784e637234e7c4'
ARCHIVE_BASE_BG2_0_SIZE='3800000'
ARCHIVE_BASE_BG2_0_VERSION='2.6.5.0-gog46477'

ARCHIVE_OPTIONAL_ICONS_NAME_BG2='baldurs-gate-2-enhanced-edition_icons.tar.gz'
ARCHIVE_OPTIONAL_ICONS_MD5_BG2='36055600f9461793995a318e74b133e8'
ARCHIVE_OPTIONAL_ICONS_URL_BG2='https://downloads.dotslashplay.it/games/baldurs-gate-2-enhanced-edition/'
CONTENT_ICONS_PATH_BG2='.'
CONTENT_ICONS_FILES_BG2='
16x16
32x32
48x48
64x64
128x128
256x256'

# Icewind Dale Enhanced Edition

ARCHIVE_BASE_IWD1_1_NAME='icewind_dale_enhanced_edition_2_6_6_0_47290.sh'
ARCHIVE_BASE_IWD1_1_MD5='fd7721f10a6d39c545ef46c58d80e2cc'
ARCHIVE_BASE_IWD1_1_SIZE='3000000'
ARCHIVE_BASE_IWD1_1_VERSION='2.6.6.0-gog47290'
ARCHIVE_BASE_IWD1_1_URL='https://www.gog.com/game/icewind_dale_enhanced_edition'

ARCHIVE_BASE_IWD1_0_NAME='icewind_dale_enhanced_edition_2_6_5_0_46474.sh'
ARCHIVE_BASE_IWD1_0_MD5='7fa481705b5d5b7f5d714a6a19c856e0'
ARCHIVE_BASE_IWD1_0_SIZE='3000000'
ARCHIVE_BASE_IWD1_0_VERSION='2.6.5.0-gog46474'

ARCHIVE_OPTIONAL_ICONS_NAME_IWD1='icewind-dale-1-enhanced-edition_icons.tar.gz'
ARCHIVE_OPTIONAL_ICONS_MD5_IWD1='2e7db406aca79f9182c4efa93df80bf4'
ARCHIVE_OPTIONAL_ICONS_URL_IWD1='https://downloads.dotslashplay.it/resources/icewind-dale-1-enhanced-edition/'
CONTENT_ICONS_PATH_IWD1='.'
CONTENT_ICONS_FILES_IWD1='
16x16
32x32
48x48
64x64
128x128
256x256'

# Planescape: Torment

ARCHIVE_BASE_PST_1_NAME='planescape_torment_enhanced_edition_3_1_4_26532.sh'
ARCHIVE_BASE_PST_1_MD5='7d42ae99df8c0a4ff460235ae406f2d6'
ARCHIVE_BASE_PST_1_SIZE='1800000'
ARCHIVE_BASE_PST_1_VERSION='3.1.4-gog26532'
ARCHIVE_BASE_PST_1_URL='https://www.gog.com/game/planescape_torment_enhanced_edition'

ARCHIVE_BASE_PST_0_NAME='gog_planescape_torment_enhanced_edition_2.1.0.3.sh'
ARCHIVE_BASE_PST_0_MD5='649c1bf9d7ccd81553c574ff1bec2cef'
ARCHIVE_BASE_PST_0_SIZE='1800000'
ARCHIVE_BASE_PST_0_VERSION='3.1.3-gog2.1.0.3'

ARCHIVE_OPTIONAL_ICONS_NAME_PST='planescape-torment-enhanced-edition_icons.tar.gz'
ARCHIVE_OPTIONAL_ICONS_MD5_PST='ed1c9961e2ecfa401af825d75723fe9c'
ARCHIVE_OPTIONAL_ICONS_URL_PST='https://downloads.dotslashplay.it/games/planescape-torment-enhanced-edition/'
CONTENT_ICONS_PATH_PST='.'
CONTENT_ICONS_FILES_PST='
16x16
32x32
48x48
64x64
96x96
128x128
256x256'


CONTENT_PATH_DEFAULT='data/noarch/game'
CONTENT_GAME_BIN_FILES_BG1='
BaldursGate'
CONTENT_GAME_BIN_FILES_BG2='
BaldursGateII'
CONTENT_GAME_BIN_FILES_IWD1='
IcewindDale'
CONTENT_GAME_BIN_FILES_PST='
Torment64'
CONTENT_GAME_L10N_FR_FILES='
lang/fr_FR'
CONTENT_GAME_L10N_DE_FILES='
lang/de_DE'
CONTENT_GAME_DATA_FILES='
chitin.key
engine.lua
Manuals
movies
music
scripts
data'
# The English localisation files are always included,
# so they can be used as a fallback for incomplete localisations.
CONTENT_GAME0_DATA_FILES='
lang/en_US'

APP_MAIN_EXE_BG1='BaldursGate'
APP_MAIN_EXE_BG2='BaldursGateII'
APP_MAIN_EXE_IWD1='IcewindDale'
APP_MAIN_EXE_PST='Torment64'
APP_MAIN_ICON='../support/icon.png'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_FR
PKG_L10N_DE
PKG_L10N_EN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N
PKG_DATA'
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libexpat.so.1
libgcc_s.so.1
libGL.so.1
libm.so.6
libopenal.so.1
libpthread.so.0
librt.so.1
libssl.so.1.0.0
libstdc++.so.6
libX11.so.6'

PKG_L10N_ID_BG1="${GAME_ID_BG1}-l10n"
PKG_L10N_ID_BG2="${GAME_ID_BG2}-l10n"
PKG_L10N_ID_IWD1="${GAME_ID_IWD1}-l10n"
PKG_L10N_ID_PST="${GAME_ID_PST}-l10n"
PKG_L10N_FR_ID_BG1="${PKG_L10N_ID_BG1}-fr"
PKG_L10N_FR_ID_BG2="${PKG_L10N_ID_BG2}-fr"
PKG_L10N_FR_ID_IWD1="${PKG_L10N_ID_IWD1}-fr"
PKG_L10N_FR_ID_PST="${PKG_L10N_ID_PST}-fr"
PKG_L10N_DE_ID_BG1="${PKG_L10N_ID_BG1}-de"
PKG_L10N_DE_ID_BG2="${PKG_L10N_ID_BG2}-de"
PKG_L10N_DE_ID_IWD1="${PKG_L10N_ID_IWD1}-de"
PKG_L10N_DE_ID_PST="${PKG_L10N_ID_PST}-de"
PKG_L10N_EN_ID_BG1="${PKG_L10N_ID_BG1}-en"
PKG_L10N_EN_ID_BG2="${PKG_L10N_ID_BG2}-en"
PKG_L10N_EN_ID_IWD1="${PKG_L10N_ID_IWD1}-en"
PKG_L10N_EN_ID_PST="${PKG_L10N_ID_PST}-en"
PKG_L10N_PROVIDES_BG1="
$PKG_L10N_ID_BG1"
PKG_L10N_PROVIDES_BG2="
$PKG_L10N_ID_BG2"
PKG_L10N_PROVIDES_IWD1="
$PKG_L10N_ID_IWD1"
PKG_L10N_PROVIDES_PST="
$PKG_L10N_ID_PST"
PKG_L10N_DE_DESCRIPTION='German localisation'
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'

PKG_DATA_ID_BG1="${GAME_ID_BG1}-data"
PKG_DATA_ID_BG2="${GAME_ID_BG2}-data"
PKG_DATA_ID_IWD1="${GAME_ID_IWD1}-data"
PKG_DATA_ID_PST="${GAME_ID_PST}-data"
PKG_DATA_DESCRIPTION='data'
# Easier upgrades from pre-20250103.1 packages
PKG_DATA_PROVIDES_BG1="${PKG_DATA_PROVIDES_BG1:-}
baldurs-gate-1-enhanced-edition-l10n-en"
PKG_DATA_PROVIDES_BG2="${PKG_DATA_PROVIDES_BG2:-}
baldurs-gate-2-enhanced-edition-l10n-en"
PKG_DATA_PROVIDES_IWD1="${PKG_DATA_PROVIDES_IWD1:-}
icewind-dale-1-enhanced-edition-l10n-en"
PKG_DATA_PROVIDES_PST="${PKG_DATA_PROVIDES_PST:-}
planescape-torment-enhanced-edition-l10n-en"

# 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'
content_inclusion_default

# Write launchers.

# Force the use of system-provided SDL.
# Prevent a segmentation fault on wayland:
# #0  0x00005555558d999f in DrawInit_GL(void*) ()
# #1  0x00005555559188ee in CVidMode::SetDisplayMode() ()
# #2  0x00005555558f9585 in CChitin::InitializeServices() ()
# #3  0x00005555558fd75d in CChitin::WinMain() ()
# #4  0x00005555554988d8 in main ()
# (sdl2-compat 2.32.64 + SDL 3.4.2)
APP_MAIN_PRERUN="$(application_prerun 'APP_MAIN')
$(launcher_tweak_sdl_override)
$(launcher_tweak_sdl_force_x11)"
dependencies_add_native_libraries 'PKG_BIN' 'libSDL2-2.0.so.0'

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		lang_string='version %s :'
		lang_fr='française'
		lang_de='allemande'
		lang_en='anglaise'
	;;
	('en'|*)
		lang_string='%s version:'
		lang_fr='French'
		lang_de='German'
		lang_en='English'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_L10N_FR'
printf "$lang_string" "$lang_de"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_L10N_DE'
printf "$lang_string" "$lang_en"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_L10N_EN'

# Clean up.

working_directory_cleanup

exit 0
