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

###
# King's Bounty: The Legend
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260406.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='kings-bounty-the-legend'
GAME_NAME='King’s Bounty: The Legend'

ARCHIVE_BASE_0_NAME='setup_kings_bounty_the_legend_1.7.35.398_(39945).exe'
ARCHIVE_BASE_0_MD5='1357aae6054406ce9d3d77b0601b045a'
# "--collisions rename-all" is used to extract all localised files, not only the English ones.
ARCHIVE_BASE_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_0_EXTRACTOR_OPTIONS='--collisions rename-all --lowercase'
ARCHIVE_BASE_0_PART1_NAME='setup_kings_bounty_the_legend_1.7.35.398_(39945)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='4647d2dd1486833c135393e2ca1c0d2e'
ARCHIVE_BASE_0_SIZE='6500991'
ARCHIVE_BASE_0_VERSION='1.7.35.398-gog39945'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/kings_bounty_the_legend'

ARCHIVE_BASE_FR_0_NAME='setup_kings_bounty_the_legend_french_1.7_(15542).exe'
ARCHIVE_BASE_FR_0_MD5='646fdfacadc498826be127fe6703f259'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_PART1_NAME='setup_kings_bounty_the_legend_french_1.7_(15542)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5='907882679fb7050e172994d36730454a'
ARCHIVE_BASE_FR_0_SIZE='6000000'
ARCHIVE_BASE_FR_0_VERSION='1.7-gog15542'

ARCHIVE_BASE_EN_0_NAME='setup_kings_bounty_the_legend_1.7_(15542).exe'
ARCHIVE_BASE_EN_0_MD5='f7a9defe0fd96a7f8d6dff6ed7828242'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_PART1_NAME='setup_kings_bounty_the_legend_1.7_(15542)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5='04fb818107e4bfe7aeae449778e88dd9'
ARCHIVE_BASE_EN_0_SIZE='6000000'
ARCHIVE_BASE_EN_0_VERSION='1.7-gog15542'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_FR='app'
CONTENT_PATH_DEFAULT_EN='app'
CONTENT_GAME_BIN_FILES='
kb.exe
data/fonts.cfg
data/default.ini
data/game.ini'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME0_BIN_FILES='
binkw32.dll
kbvistashellsupport.dll
luaide.dll
scilexer.dll'
CONTENT_GAME_L10N_FILES='
data/app.ini
data/loc_data.kfs
sessions/base/loc_ses.kfs'
CONTENT_GAME_L10N_FR_FILES='
data/app.ini@fr-FR
data/loc_data.kfs@fr-FR
sessions/base/loc_ses.kfs@fr-FR'
CONTENT_GAME_L10N_EN_FILES='
data/app.ini@en-US
data/loc_data.kfs@en-US
sessions/base/loc_ses.kfs@en-US'
CONTENT_GAME_DATA_FILES='
data
sessions
curver.txt'
CONTENT_DOC_L10N_FILES='
readme.rtf
manual.pdf'
CONTENT_DOC_L10N_FR_FILES='
manual.pdf@fr-FR'
CONTENT_DOC_L10N_EN_FILES='
manual.pdf@en-US
readme.rtf@en-US'

USER_PERSISTENT_FILES='
data/*.ini
data/fonts.cfg'

# With WineD3D, performance is very poor both on the map and during battles.
# (WINE 11.6)
WINE_DIRECT3D_RENDERER='dxvk'

APP_MAIN_EXE='kb.exe'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_FR
PKG_L10N_EN
PKG_DATA'
PACKAGES_LIST_FR='
PKG_BIN
PKG_L10N
PKG_DATA'
PACKAGES_LIST_EN='
PKG_BIN
PKG_L10N
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'

PKG_L10N_BASE_ID="${GAME_ID}-l10n"
PKG_L10N_FR_ID="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_EN_ID="${PKG_L10N_BASE_ID}-en"
PKG_L10N_ID_FR="$PKG_L10N_FR_ID"
PKG_L10N_ID_EN="$PKG_L10N_EN_ID"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'
PKG_L10N_DESCRIPTION_FR="$PKG_L10N_FR_DESCRIPTION"
PKG_L10N_DESCRIPTION_EN="$PKG_L10N_EN_DESCRIPTION"

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

# The "find" and "rename" commands are used to handle localised files.

REQUIREMENTS_LIST='
find
rename'

# 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
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default
case "$(current_archive)" in
	('ARCHIVE_BASE_FR_'*|'ARCHIVE_BASE_EN_'*)
		# Old language-specific archives do not use localisation suffixes.
	;;
	(*)
		path_archive_contents=$(path_archive_contents)
		# Delete unsupported localisations.
		find "${path_archive_contents}" -name '*@??-*' ! -name '*@fr-FR' ! -name '*@en-US' -delete
		# Rename some localized files that do not differ between the supported localisations.
		rename 's/\@fr-FR$//' \
			"${path_archive_contents}/data/fonts.cfg@fr-FR" \
			"${path_archive_contents}/data/video/1c_logo_eng.ogm@fr-FR" \
			"${path_archive_contents}/data/video/kbtl-intro-800x600-eng.ogm@fr-FR" \
			"${path_archive_contents}/kb.exe@fr-FR"
		# Delete some redundant files duplicated between the supported localisations.
		rm \
			"${path_archive_contents}/data/fonts.cfg@en-US" \
			"${path_archive_contents}/data/video/1c_logo_eng.ogm@en-US" \
			"${path_archive_contents}/data/video/kbtl-intro-800x600-eng.ogm@en-US" \
			"${path_archive_contents}/kb.exe@en-US"
	;;
esac

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Drop the localisation suffixes.

case "$(current_archive)" in
	('ARCHIVE_BASE_EN_'*|'ARCHIVE_BASE_FR_'*)
		# Old language-specific archives do not use localization suffixes.
	;;
	(*)
		find \
			"$(package_path 'PKG_L10N_FR')$(path_game_data)" \
			"$(package_path 'PKG_L10N_FR')$(path_documentation)" \
			-name '*@fr-FR' -exec rename 's/\@fr-FR$//' {} +
		find \
			"$(package_path 'PKG_L10N_EN')$(path_game_data)" \
			"$(package_path 'PKG_L10N_EN')$(path_documentation)" \
			-name '*@en-US' -exec rename 's/\@en-US$//' {} +
	;;
esac

# Write launchers.

launchers_generation

# Build packages.

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

# Clean up.

working_directory_cleanup

exit 0
