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

###
# Heroes of Might and Magic 5:
# - Heroes of Might and Magic 5 + Hammers of Fate
# - Tribes of the East
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260326.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='heroes-of-might-and-magic-5'
GAME_NAME='Heroes of Might and Magic V'

GAME_ID_HOMM5="$GAME_ID"
GAME_NAME_HOMM5="$GAME_NAME"

GAME_ID_TOTE="${GAME_ID}-tribes-of-the-east"
GAME_NAME_TOTE="${GAME_NAME} - Tribes of the East"

EXPANSION_ID_HOF='hammers-of-fate'
EXPANSION_NAME_HOF='Hammers of Fate'

EXPANSION_ID_DM='dark-messiah'
EXPANSION_NAME_DM='Dark Messiah'

EXPANSION_ID_SKILLS='skill-wheel'
EXPANSION_NAME_SKILLS='SkillWheel'

# Heroes of Might and Magic 5 + Hammers of Fate

ARCHIVE_BASE_HOMM5_FR_2_NAME='setup_heroes_of_might_and_magic_v_2.1_v3_gog_(french)_(83649).exe'
ARCHIVE_BASE_HOMM5_FR_2_MD5='d2c94ae2a26d86f9a6be45ea9c29a5ca'
ARCHIVE_BASE_HOMM5_FR_2_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_FR_2_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_v3_gog_(french)_(83649)-1.bin'
ARCHIVE_BASE_HOMM5_FR_2_PART1_MD5='dd17be763ec6f481dac5c6d1f8e7195c'
ARCHIVE_BASE_HOMM5_FR_2_SIZE='2502839'
ARCHIVE_BASE_HOMM5_FR_2_VERSION='2.1-gog83649'
ARCHIVE_BASE_HOMM5_FR_2_URL='https://www.gog.com/game/heroes_of_might_and_magic_5_bundle'

ARCHIVE_BASE_HOMM5_EN_2_NAME='setup_heroes_of_might_and_magic_v_2.1_v3_gog_(83649).exe'
ARCHIVE_BASE_HOMM5_EN_2_MD5='4d2f8ca3b0363e1e10c9cae967bae133'
ARCHIVE_BASE_HOMM5_EN_2_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_EN_2_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_v3_gog_(83649)-1.bin'
ARCHIVE_BASE_HOMM5_EN_2_PART1_MD5='5080cc5e93f4c5091c5f7c590880c805'
ARCHIVE_BASE_HOMM5_EN_2_SIZE='2503743'
ARCHIVE_BASE_HOMM5_EN_2_VERSION='2.1-gog83649'
ARCHIVE_BASE_HOMM5_EN_2_URL='https://www.gog.com/game/heroes_of_might_and_magic_5_bundle'

# Required with GOG.com build 83649, to prevent a crash of the base game
# during the loading time supposed to lead to actual gameplay.
# (Hammers of Fate is not affected)
ARCHIVE_REQUIRED_GOGFIX_NAME='heroes-of-might-and-magic-5.tar.xz'
ARCHIVE_REQUIRED_GOGFIX_MD5='a43368f367de935f914431a01ba35737'
ARCHIVE_REQUIRED_GOGFIX_SIZE='12591'
ARCHIVE_REQUIRED_GOGFIX_URL='http://downloads.dotslashplay.it/games/heroes-of-might-and-magic-5/'

ARCHIVE_BASE_HOMM5_FR_1_NAME='setup_heroes_of_might_and_magic_v_2.1_v2_(french)_(28567).exe'
ARCHIVE_BASE_HOMM5_FR_1_MD5='78e860af17d9ce220d8c60c1e594cf40'
ARCHIVE_BASE_HOMM5_FR_1_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_FR_1_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_v2_(french)_(28567)-1.bin'
ARCHIVE_BASE_HOMM5_FR_1_PART1_MD5='9e8017cc5d84231bf8eb9c8c757631f8'
ARCHIVE_BASE_HOMM5_FR_1_SIZE='2600000'
ARCHIVE_BASE_HOMM5_FR_1_VERSION='2.1-gog28567'

ARCHIVE_BASE_HOMM5_EN_1_NAME='setup_heroes_of_might_and_magic_v_2.1_v2_(28567).exe'
ARCHIVE_BASE_HOMM5_EN_1_MD5='657775b4eb545150f5895e61e67eda73'
ARCHIVE_BASE_HOMM5_EN_1_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_EN_1_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_v2_(28567)-1.bin'
ARCHIVE_BASE_HOMM5_EN_1_PART1_MD5='bb4dd38f472fd94f82aa22cb256f4b9c'
ARCHIVE_BASE_HOMM5_EN_1_SIZE='2600000'
ARCHIVE_BASE_HOMM5_EN_1_VERSION='2.1-gog28567'

ARCHIVE_BASE_HOMM5_FR_0_NAME='setup_heroes_of_might_and_magic_v_2.1_(french)_(25025).exe'
ARCHIVE_BASE_HOMM5_FR_0_MD5='b9e278ee60d574b89068479a4e6c84c1'
ARCHIVE_BASE_HOMM5_FR_0_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_FR_0_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_(french)_(25025)-1.bin'
ARCHIVE_BASE_HOMM5_FR_0_PART1_MD5='57ca61178fca9ed2e50a5dc667f6d565'
ARCHIVE_BASE_HOMM5_FR_0_SIZE='2600000'
ARCHIVE_BASE_HOMM5_FR_0_VERSION='2.1-gog25025'

ARCHIVE_BASE_HOMM5_EN_0_NAME='setup_heroes_of_might_and_magic_v_2.1_(25025).exe'
ARCHIVE_BASE_HOMM5_EN_0_MD5='6e36b7fb9f1e8362326688d383e4bdb9'
ARCHIVE_BASE_HOMM5_EN_0_TYPE='innosetup'
ARCHIVE_BASE_HOMM5_EN_0_PART1_NAME='setup_heroes_of_might_and_magic_v_2.1_(25025)-1.bin'
ARCHIVE_BASE_HOMM5_EN_0_PART1_MD5='3e38f48f450f58833728cd73e9266d2d'
ARCHIVE_BASE_HOMM5_EN_0_SIZE='2600000'
ARCHIVE_BASE_HOMM5_EN_0_VERSION='2.1-gog25025'

# Tribes of the East

ARCHIVE_BASE_TOTE_FR_2_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v3_gog_(french)_(83650).exe'
ARCHIVE_BASE_TOTE_FR_2_MD5='9ce0709d5e873e386cddd46fe89c3d92'
ARCHIVE_BASE_TOTE_FR_2_TYPE='innosetup'
ARCHIVE_BASE_TOTE_FR_2_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v3_gog_(french)_(83650)-1.bin'
ARCHIVE_BASE_TOTE_FR_2_PART1_MD5='afe312964bdebe966bf81d2dfb4c00ec'
ARCHIVE_BASE_TOTE_FR_2_SIZE='2286756'
ARCHIVE_BASE_TOTE_FR_2_VERSION='3.1-gog83650'
ARCHIVE_BASE_TOTE_FR_2_URL='https://www.gog.com/game/heroes_of_might_and_magic_5_bundle'

ARCHIVE_BASE_TOTE_EN_2_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v3_gog_(83650).exe'
ARCHIVE_BASE_TOTE_EN_2_MD5='77a1eaedb5139409e0f6a4c163927a84'
ARCHIVE_BASE_TOTE_EN_2_TYPE='innosetup'
ARCHIVE_BASE_TOTE_EN_2_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v3_gog_(83650)-1.bin'
ARCHIVE_BASE_TOTE_EN_2_PART1_MD5='2c1dfe395fa5dbb1d652428b88a4ca11'
ARCHIVE_BASE_TOTE_EN_2_SIZE='2273833'
ARCHIVE_BASE_TOTE_EN_2_VERSION='3.1-gog83650'
ARCHIVE_BASE_TOTE_EN_2_URL='https://www.gog.com/game/heroes_of_might_and_magic_5_bundle'

ARCHIVE_BASE_TOTE_FR_1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v2_(french)_(28569).exe'
ARCHIVE_BASE_TOTE_FR_1_MD5='6a1a915180d1cee32e78419f6917be87'
ARCHIVE_BASE_TOTE_FR_1_TYPE='innosetup'
ARCHIVE_BASE_TOTE_FR_1_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v2_(french)_(28569)-1.bin'
ARCHIVE_BASE_TOTE_FR_1_PART1_MD5='f48ed6725126696bf3e67ce327db6263'
ARCHIVE_BASE_TOTE_FR_1_SIZE='2300000'
ARCHIVE_BASE_TOTE_FR_1_VERSION='3.1-gog28569'

ARCHIVE_BASE_TOTE_EN_1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v2_(28569).exe'
ARCHIVE_BASE_TOTE_EN_1_MD5='9593ad538a39638bacb4d7ef45368ce2'
ARCHIVE_BASE_TOTE_EN_1_TYPE='innosetup'
ARCHIVE_BASE_TOTE_EN_1_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_v2_(28569)-1.bin'
ARCHIVE_BASE_TOTE_EN_1_PART1_MD5='8e03271dc4aff5834110664b5d6eefde'
ARCHIVE_BASE_TOTE_EN_1_SIZE='2300000'
ARCHIVE_BASE_TOTE_EN_1_VERSION='3.1-gog28569'

ARCHIVE_BASE_TOTE_FR_0_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_(french)_(25025).exe'
ARCHIVE_BASE_TOTE_FR_0_MD5='a2b5d18f34d3fa1a760de4fa63aa3819'
ARCHIVE_BASE_TOTE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_TOTE_FR_0_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_(french)_(25025)-1.bin'
ARCHIVE_BASE_TOTE_FR_0_PART1_MD5='08a5ec9aaf674235db4d96072bf373fc'
ARCHIVE_BASE_TOTE_FR_0_SIZE='2300000'
ARCHIVE_BASE_TOTE_FR_0_VERSION='3.1-gog25025'

ARCHIVE_BASE_TOTE_EN_0_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_(25025).exe'
ARCHIVE_BASE_TOTE_EN_0_MD5='3096f296d5d8b6cb0b4ab479fc06474b'
ARCHIVE_BASE_TOTE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_TOTE_EN_0_PART1_NAME='setup_heroes_of_might_and_magic_v_-_tribes_of_the_east_3.1_(25025)-1.bin'
ARCHIVE_BASE_TOTE_EN_0_PART1_MD5='5f4840b0105bd6b4228ff9b707bc0434'
ARCHIVE_BASE_TOTE_EN_0_SIZE='2300000'
ARCHIVE_BASE_TOTE_EN_0_VERSION='3.1-gog25025'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_MAIN_FILES='
bin/H5_Game.exe'
# H5_Game.exe requirements (base game + Tribes of the East)
CONTENT_LIBS_MAIN_FILES='
bin/UbiStats.dll
bin/libcurl.dll
bin/fmod.dll
bin/granny2.dll
bin/zlib1.dll'
# H5_Game.exe extra requirements (Tribes of the East)
CONTENT_LIBS_MAIN_FILES_TOTE="$CONTENT_LIBS_MAIN_FILES
bin/um.dll"
# libcurl.dll requirements
CONTENT_LIBS0_MAIN_FILES='
bin/zlibwapi.dll'
CONTENT_GAME_HOF_FILES='
bina1/H5_Game.exe'
# H5_Game.exe requirements (Hammers of Fate)
CONTENT_LIBS_HOF_FILES='
bina1/UbiStats.dll
bina1/libcurl.dll
bina1/fmod.dll
bina1/granny2.dll
bina1/zlib1.dll'
# libcurl.dll requirements
CONTENT_LIBS0_HOF_FILES='
bina1/zlibwapi.dll'
CONTENT_GAME_DM_FILES='
binDM/H5_Game.exe'
# H5_Game.exe requirements (Dark Messiah)
CONTENT_LIBS_DM_FILES='
binDM/UbiStats.dll
binDM/libcurl.dll
binDM/fmod.dll
binDM/granny2.dll
binDM/zlib1.dll'
# libcurl.dll requirements
CONTENT_LIBS0_DM_FILES='
binDM/zlibwapi.dll'
CONTENT_GAME_EDITOR_FILES='
bin/H5_MapEditor.exe
profile/editor.cfg'
CONTENT_GAME_EDITOR_FILES_TOTE="$CONTENT_GAME_EDITOR_FILES
profile/editor_a2.cfg"
# H5_MapEditor.exe requirements
CONTENT_LIBS_EDITOR_FILES='
bin/MFC71.DLL'
CONTENT_GAME_HOF_EDITOR_FILES='
bina1/H5_MapEditor.exe
profile/editor_a1.cfg'
# H5_MapEditor.exe requirements
CONTENT_LIBS_HOF_EDITOR_FILES='
bina1/MFC71.DLL'
CONTENT_GAME_SKILLS_RELATIVE_PATH='FanDocuments'
CONTENT_GAME_SKILLS_FILES='
Skillwheel.exe'
CONTENT_GAME_L10N_FILES='
music/cs/death-berein.ogg
music/cs/death-nico.ogg
music/cs/heart-griffin.ogg
music/cs/isabel-trap.ogg
music/cs/nico-vampire.ogg
music/cs/ritual-isabel.ogg
video/intro.ogg
video/outro.ogg
data/sound.pak
data/texts.pak
data/*-texts.pak
datals/*-texts.pak'
CONTENT_GAME_L10N_HOF_FILES='
dataa1/texts.pak
dataa1/*-sound.pak
dataa1/*-texts.pak'
# TODO: Some content is duplicated between the base game and Tribes of the East,
#       and could be moved to a common package.
CONTENT_GAME_DATA_FILES='
hwcursors
data/data.pak
data/*-data.pak
music/advmap-campaign
music/advmap-heroes
music/advmap-terrain
music/ai-themes
music/battle-themes
music/credits
music/cs
music/ds
music/dwellings
music/grail-theme
music/lose-battle
music/lose-campaign
music/mainmenu
music/retreat-battle
music/siege-battle
music/tavern-themes
music/town-themes
music/win-battle
music/win-campaign
profiles/default_profile
profiles/autoexec.cfg
profiles/p2pdir.cfg
profiles/start.cfg
video/logo-nival.ogg
video/logo-ubi6.ogg'
CONTENT_GAME_DATA_FILES_HOMM5="${CONTENT_GAME_DATA_FILES}
duelpresets
data/lights
data/maps
datals/*-data.pak
video/intro.xml
video/outro.xml
splash.bmp"
CONTENT_GAME_DATA_FILES_TOTE="${CONTENT_GAME_DATA_FILES}
data/soundsfx.pak
music/a1
music/a2
music/dm
profiles/autoexec_a2.cfg
video/intro_a1.xml
video/outro_a1.xml
splasha2.bmp"
CONTENT_GAME_DATA_HOF_FILES='
dataa1/*-data.pak
hwcursors/a1_*
music/a1
profiles/autoexec_a1.cfg
video/intro_a1.xml
video/outro_a1.xml
splasha1.bmp'
CONTENT_GAME_DATA_DM_FILES='
customcontentdm/dm.map
profiles/autoexec_a2_dm.cfg'
CONTENT_GAME_EDITOR_DATA_FILES='
Editor'
CONTENT_DOC_L10N_FILES='
*.pdf
*.txt'
CONTENT_DOC_EDITOR_L10N_FILES='
Editor Documentation'

APPLICATIONS_LIST_HOMM5='
APP_MAIN
APP_EDITOR
APP_HOF
APP_HOF_EDITOR'
APPLICATIONS_LIST_TOTE='
APP_MAIN
APP_EDITOR
APP_DM
APP_SKILLS'

USER_PERSISTENT_DIRECTORIES='
Maps
Editor/H5MMods'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/Heroes of Might and Magic V'
WINE_PERSISTENT_DIRECTORIES_TOTE='
users/${USER}/Documents/My Games/Heroes of Might and Magic V - Tribes of the East'

APP_MAIN_EXE='bin/H5_Game.exe'

# The map editor is not really usable with current WINE, because of the following bug:
# "HOMMV Map Editor pane shrinks on every refresh (Unhandled query type 4)"
# https://bugs.winehq.org/show_bug.cgi?id=20190
# (WINE 10.0)
APP_EDITOR_ID="${GAME_ID}-editor"
APP_EDITOR_ID_TOTE="${GAME_ID_TOTE}-editor"
APP_EDITOR_NAME="${GAME_NAME} - Map Editor"
APP_EDITOR_NAME_TOTE="${GAME_NAME_TOTE} - Map Editor"
APP_EDITOR_EXE='bin/H5_MapEditor.exe'
APP_EDITOR_ICON_WRESTOOL_OPTIONS='--type=14 --name=128'

# Hammers of Fate

APP_HOF_ID="${GAME_ID}-${EXPANSION_ID_HOF}"
APP_HOF_NAME="${GAME_NAME} - ${EXPANSION_NAME_HOF}"
APP_HOF_EXE='bina1/H5_Game.exe'

# The map editor is not really usable with current WINE, because of the following bug:
# "HOMMV Map Editor pane shrinks on every refresh (Unhandled query type 4)"
# https://bugs.winehq.org/show_bug.cgi?id=20190
# (WINE 10.0)
APP_HOF_EDITOR_ID="${GAME_ID}-${EXPANSION_ID_HOF}-editor"
APP_HOF_EDITOR_NAME="${GAME_NAME} - ${EXPANSION_NAME_HOF} - Map Editor"
APP_HOF_EDITOR_EXE='bina1/H5_MapEditor.exe'
APP_HOF_EDITOR_ICON_WRESTOOL_OPTIONS='--type=14 --name=128'

# Tribes of the East

APP_DM_ID="${GAME_ID}-${EXPANSION_ID_DM}"
APP_DM_NAME="${GAME_NAME} - ${EXPANSION_NAME_DM}"
APP_DM_EXE='binDM/H5_Game.exe'
APP_DM_ICON_WRESTOOL_OPTIONS='--type=14 --name=101'

APP_SKILLS_ID="${GAME_ID}-${EXPANSION_ID_SKILLS}"
APP_SKILLS_NAME="${GAME_NAME} - ${EXPANSION_NAME_SKILLS}"
APP_SKILLS_EXE='Skillwheel.exe'
APP_SKILLS_ICON='FanDocuments/Skillwheel.exe'
APP_SKILLS_ICON_WRESTOOL_OPTIONS='--type=14 --name=200'

# PKG_DATA_HOF must be listed before PKG_DATA,
# to ensure correct hwcursors split between packages.
PACKAGES_LIST_HOMM5='
PKG_MAIN
PKG_EDITOR
PKG_EDITOR_L10N
PKG_EDITOR_DATA
PKG_HOF
PKG_HOF_EDITOR
PKG_L10N
PKG_L10N_HOF
PKG_DATA_HOF
PKG_DATA'
PACKAGES_LIST_TOTE='
PKG_MAIN
PKG_EDITOR
PKG_EDITOR_L10N
PKG_EDITOR_DATA
PKG_DM
PKG_SKILLS
PKG_L10N
PKG_DATA
PKG_DATA_DM'

PKG_MAIN_ARCH='32'
PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'

PKG_EDITOR_ID_HOMM5="${GAME_ID_HOMM5}-editor"
PKG_EDITOR_ID_TOTE="${GAME_ID_TOTE}-editor"
PKG_EDITOR_DESCRIPTION='Map Editor (binaries)'
PKG_EDITOR_ARCH='32'
PKG_EDITOR_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_EDITOR_L10N
PKG_EDITOR_DATA'

PKG_EDITOR_L10N_BASE_ID_HOMM5="${PKG_EDITOR_ID_HOMM5}-l10n"
PKG_EDITOR_L10N_BASE_ID_TOTE="${PKG_EDITOR_ID_TOTE}-l10n"
PKG_EDITOR_L10N_ID_HOMM5_FR="${PKG_EDITOR_L10N_BASE_ID_HOMM5}-fr"
PKG_EDITOR_L10N_ID_HOMM5_EN="${PKG_EDITOR_L10N_BASE_ID_HOMM5}-en"
PKG_EDITOR_L10N_ID_TOTE_FR="${PKG_EDITOR_L10N_BASE_ID_TOTE}-fr"
PKG_EDITOR_L10N_ID_TOTE_EN="${PKG_EDITOR_L10N_BASE_ID_TOTE}-en"
PKG_EDITOR_L10N_PROVIDES_HOMM5="
$PKG_EDITOR_L10N_BASE_ID_HOMM5"
PKG_EDITOR_L10N_PROVIDES_TOTE="
$PKG_EDITOR_L10N_BASE_ID_TOTE"
PKG_EDITOR_L10N_DESCRIPTION_FR='Map Editor (French documentation)'
PKG_EDITOR_L10N_DESCRIPTION_EN='Map Editor (English documentation)'
PKG_EDITOR_L10N_DESCRIPTION_HOMM5_FR="$PKG_EDITOR_L10N_DESCRIPTION_FR"
PKG_EDITOR_L10N_DESCRIPTION_HOMM5_EN="$PKG_EDITOR_L10N_DESCRIPTION_EN"
PKG_EDITOR_L10N_DESCRIPTION_TOTE_FR="$PKG_EDITOR_L10N_DESCRIPTION_FR"
PKG_EDITOR_L10N_DESCRIPTION_TOTE_EN="$PKG_EDITOR_L10N_DESCRIPTION_EN"

PKG_EDITOR_DATA_ID_HOMM5="${PKG_EDITOR_ID_HOMM5}-data"
PKG_EDITOR_DATA_ID_TOTE="${PKG_EDITOR_ID_TOTE}-data"
PKG_EDITOR_DATA_DESCRIPTION='Map Editor (data)'

PKG_HOF_ID="${GAME_ID}-${EXPANSION_ID_HOF}"
PKG_HOF_DESCRIPTION="${EXPANSION_HOF_DM}"
PKG_HOF_ARCH='32'
# TODO: Try to drop the dependency on the base game,
#       maybe using a common data package.
PKG_HOF_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_L10N_HOF_BASE
PKG_DATA_HOF'

PKG_HOF_EDITOR_ID="${GAME_ID}-${EXPANSION_ID_HOF}-editor"
PKG_HOF_EDITOR_DESCRIPTION="${EXPANSION_ID_HOF} - Map Editor (binaries)"
PKG_HOF_EDITOR_ARCH='32'
PKG_HOF_EDITOR_DEPENDENCIES_SIBLINGS='
PKG_HOF
PKG_EDITOR_L10N
PKG_EDITOR_DATA'

PKG_DM_ID="${GAME_ID}-${EXPANSION_ID_DM}"
PKG_DM_DESCRIPTION="${EXPANSION_NAME_DM}"
PKG_DM_ARCH='32'
# TODO: Try to drop the dependency on the base game,
#       maybe using a common data package.
PKG_DM_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_DATA_DM'

PKG_SKILLS_ID="${GAME_ID}-${EXPANSION_ID_SKILLS}"
PKG_SKILLS_DESCRIPTION="${EXPANSION_NAME_SKILLS}"
PKG_SKILLS_ARCH='32'

PKG_L10N_BASE_ID_HOMM5="${GAME_ID_HOMM5}-l10n"
PKG_L10N_BASE_ID_TOTE="${GAME_ID_TOTE}-l10n"
PKG_L10N_ID_HOMM5_EN="${PKG_L10N_BASE_ID_HOMM5}-en"
PKG_L10N_ID_HOMM5_FR="${PKG_L10N_BASE_ID_HOMM5}-fr"
PKG_L10N_ID_TOTE_EN="${PKG_L10N_BASE_ID_TOTE}-en"
PKG_L10N_ID_TOTE_FR="${PKG_L10N_BASE_ID_TOTE}-fr"
PKG_L10N_PROVIDES_HOMM5="
$PKG_L10N_BASE_ID_HOMM5"
PKG_L10N_PROVIDES_TOTE="
$PKG_L10N_BASE_ID_TOTE"
PKG_L10N_DESCRIPTION_EN='English localisation'
PKG_L10N_DESCRIPTION_FR='French localisation'
PKG_L10N_DESCRIPTION_HOMM5_EN="$PKG_L10N_DESCRIPTION_EN"
PKG_L10N_DESCRIPTION_HOMM5_FR="$PKG_L10N_DESCRIPTION_FR"
PKG_L10N_DESCRIPTION_TOTE_EN="$PKG_L10N_DESCRIPTION_EN"
PKG_L10N_DESCRIPTION_TOTE_FR="$PKG_L10N_DESCRIPTION_FR"

PKG_L10N_HOF_BASE_ID="${GAME_ID}-${EXPANSION_ID_HOF}-l10n"
PKG_L10N_HOF_ID_HOMM5_EN="${PKG_L10N_HOF_BASE_ID}-en"
PKG_L10N_HOF_ID_HOMM5_FR="${PKG_L10N_HOF_BASE_ID}-fr"
PKG_L10N_HOF_PROVIDES="
$PKG_L10N_HOF_BASE_ID"
PKG_L10N_HOF_DESCRIPTION_HOMM5_EN="${EXPANSION_NAME_HOF} - English localisation"
PKG_L10N_HOF_DESCRIPTION_HOMM5_FR="${EXPANSION_NAME_HOF} - French localisation"

PKG_DATA_ID_HOMM5="${GAME_ID_HOMM5}-data"
PKG_DATA_ID_TOTE="${GAME_ID_TOTE}-data"
PKG_DATA_DESCRIPTION='data'

PKG_DATA_HOF_ID="${GAME_ID}-${EXPANSION_ID_HOF}-data"
PKG_DATA_HOF_DESCRIPTION="${EXPANSION_NAME_HOF} - data"

PKG_DATA_DM_ID="${GAME_ID}-${EXPANSION_ID_DM}-data"
PKG_DATA_DM_DESCRIPTION="${EXPANSION_NAME_DM} - data"

# Load common functions.

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$PWD" \
		"${XDG_DATA_HOME:="${HOME}/.local/share"}/play.it" \
		'/usr/local/share/games/play.it' \
		'/usr/local/share/play.it' \
		'/usr/share/games/play.it' \
		'/usr/share/play.it'
	do
		playit_lib_path="${playit_lib_dir}/libplayit2.sh"
		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 "$@"

# GOG.com build 83649 - Check for presence of fixed .exe.

case "$(current_archive)" in
	('ARCHIVE_BASE_HOMM5_'??'_2')
		archive_initialize_required \
			'ARCHIVE_GOGFIX' \
			'ARCHIVE_REQUIRED_GOGFIX'
	;;
esac

# Extract game data.

archive_extraction_default
if archive_is_available 'ARCHIVE_GOGFIX'; then
	archive_extraction 'ARCHIVE_GOGFIX'
	path_archive_contents="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)"
	mv \
		"${path_archive_contents}/h5_game.exe" \
		"${path_archive_contents}/bin/H5_Game.exe"
fi

# Include game data.

content_inclusion_icons 'PKG_DATA' 'APP_MAIN'
content_inclusion_icons 'PKG_EDITOR_DATA' 'APP_EDITOR'
case "$(current_archive)" in
	('ARCHIVE_BASE_HOMM5_'*)
		content_inclusion_icons 'PKG_DATA_HOF' 'APP_HOF'
		content_inclusion_icons 'PKG_EDITOR_DATA' 'APP_HOF_EDITOR'
	;;
	('ARCHIVE_BASE_TOTE_'*)
		content_inclusion_icons 'PKG_SKILLS' 'APP_SKILLS'
		content_inclusion_icons 'PKG_DATA_DM' 'APP_DM'
	;;
esac
content_inclusion_default

# Write launchers.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	if [ -d "$PATH_LIBRARIES_SYSTEM" ]; then
	    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 --symbolic "$dll_source" "$dll_destination"
	    done
	fi
	EOF
}

# Run the game binary from its parent directory.
# Work around choppy animations on high uptime.
game_exec_line() {
	# shellcheck disable=SC2086
	local application=$1

	local application_exe
	application_exe=$(application_exe "$application")
	cat <<- EOF
	# Run the game binary from its parent directory.
	cd "$(dirname "$application_exe")"
	# Work around choppy animations on high uptime.
	unshare --map-current-user --time --monotonic -\$((\$(cut --delimiter=. --fields=1 /proc/uptime)-42)) -- \\
	    \$(wine_command) "$(basename "$application_exe")" "\$@"
	EOF
}

launchers_generation 'PKG_MAIN'   'APP_MAIN'
launchers_generation 'PKG_EDITOR' 'APP_EDITOR'
case "$(current_archive)" in
	('ARCHIVE_BASE_HOMM5_'*)
		launchers_generation 'PKG_HOF'        'APP_HOF'
		launchers_generation 'PKG_HOF_EDITOR' 'APP_HOF_EDITOR'
	;;
	('ARCHIVE_BASE_TOTE_'*)
		launchers_generation 'PKG_SKILLS' 'APP_SKILLS'
		launchers_generation 'PKG_DM'     'APP_DM'
	;;
esac

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
