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

###
# Heroes Chronicles series:
# - Chapter 1: Warlords of the Wasteland
# - Chapter 2: Conquest of the Underworld
# - Chapter 3: Masters of the Elements
# - Chapter 4: Clash of the Dragons
# - Chapter 5: The World Tree
# - Chapter 6: The Fiery Moon
# - Chapter 7: Revolt of the Beastmasters
# - Chapter 8: The Sword of Frost
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260319.11

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='heroes-chronicles'
GAME_ID_CHAPTER1="${GAME_ID}-1"
GAME_ID_CHAPTER2="${GAME_ID}-2"
GAME_ID_CHAPTER3="${GAME_ID}-3"
GAME_ID_CHAPTER4="${GAME_ID}-4"
GAME_ID_CHAPTER5="${GAME_ID}-5"
GAME_ID_CHAPTER6="${GAME_ID}-6"
GAME_ID_CHAPTER7="${GAME_ID}-7"
GAME_ID_CHAPTER8="${GAME_ID}-8"
GAME_NAME='Heroes Chronicles'
GAME_NAME_CHAPTER1="${GAME_NAME}: Chapter 1 - Warlords of the Wasteland"
GAME_NAME_CHAPTER2="${GAME_NAME}: Chapter 2 - Conquest of the Underworld"
GAME_NAME_CHAPTER3="${GAME_NAME}: Chapter 3 - Masters of the Elements"
GAME_NAME_CHAPTER4="${GAME_NAME}: Chapter 4 - Clash of the Dragons"
GAME_NAME_CHAPTER5="${GAME_NAME}: Chapter 5 - The World Tree"
GAME_NAME_CHAPTER6="${GAME_NAME}: Chapter 6 - The Fiery Moon"
GAME_NAME_CHAPTER7="${GAME_NAME}: Chapter 7 - Revolt of the Beastmasters"
GAME_NAME_CHAPTER8="${GAME_NAME}: Chapter 8 - The Sword of Frost"

ARCHIVE_BASE_CHAPTER1_1_NAME='setup_heroes_chronicles_chapter_1_-_warlords_of_the_wasteland_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER1_1_MD5='d8747a20805326399d106c72bedf3772'
ARCHIVE_BASE_CHAPTER1_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER1_1_SIZE='503959'
ARCHIVE_BASE_CHAPTER1_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER1_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER1_0_NAME='setup_heroes_chronicles_chapter1_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER1_0_MD5='f584d6e11ed47d1d40e973a691adca5d'
ARCHIVE_BASE_CHAPTER1_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER1_0_SIZE='500000'
ARCHIVE_BASE_CHAPTER1_0_VERSION='1.0-gog2.1.0.42'

ARCHIVE_BASE_CHAPTER2_1_NAME='setup_heroes_chronicles_chapter_2_-_conquest_of_the_underworld_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER2_1_MD5='c1f2c515e4bb9a8e9c6e135afc47794d'
ARCHIVE_BASE_CHAPTER2_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER2_1_SIZE='507316'
ARCHIVE_BASE_CHAPTER2_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER2_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER2_0_NAME='setup_heroes_chronicles_chapter2_2.1.0.43.exe'
ARCHIVE_BASE_CHAPTER2_0_MD5='0d240bc0309814ba251c2d9b557cf69f'
ARCHIVE_BASE_CHAPTER2_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER2_0_SIZE='510000'
ARCHIVE_BASE_CHAPTER2_0_VERSION='1.0-gog2.1.0.43'

ARCHIVE_BASE_CHAPTER3_1_NAME='setup_heroes_chronicles_chapter_3_-_masters_of_the_elements_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER3_1_MD5='9bc95741969ac36e036b2906df3d55d5'
ARCHIVE_BASE_CHAPTER3_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER3_1_SIZE='487639'
ARCHIVE_BASE_CHAPTER3_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER3_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER3_0_NAME='setup_heroes_chronicles_chapter3_2.1.0.41.exe'
ARCHIVE_BASE_CHAPTER3_0_MD5='cb21751572960d47a259efc17b92c88c'
ARCHIVE_BASE_CHAPTER3_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER3_0_SIZE='490000'
ARCHIVE_BASE_CHAPTER3_0_VERSION='1.0-gog2.1.0.41'

ARCHIVE_BASE_CHAPTER4_1_NAME='setup_heroes_chronicles_chapter_4_-_clash_of_the_dragons_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER4_1_MD5='75f7ae76e481c470019b7788420359b7'
ARCHIVE_BASE_CHAPTER4_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER4_1_SIZE='482938'
ARCHIVE_BASE_CHAPTER4_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER4_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER4_0_NAME='setup_heroes_chronicles_chapter4_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER4_0_MD5='922291e16176cb4bd37ca88eb5f3a19e'
ARCHIVE_BASE_CHAPTER4_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER4_0_SIZE='490000'
ARCHIVE_BASE_CHAPTER4_0_VERSION='1.0-gog2.1.0.42'

ARCHIVE_BASE_CHAPTER5_1_NAME='setup_heroes_chronicles_chapter_5_-_the_world_tree_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER5_1_MD5='7d39a4b8068399065536ca360db6d5e6'
ARCHIVE_BASE_CHAPTER5_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER5_1_SIZE='463534'
ARCHIVE_BASE_CHAPTER5_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER5_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER5_0_NAME='setup_heroes_chronicles_chapter5_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER5_0_MD5='57b3ec588e627a2da30d3bc80ede5b1d'
ARCHIVE_BASE_CHAPTER5_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER5_0_SIZE='470000'
ARCHIVE_BASE_CHAPTER5_0_VERSION='1.0-gog2.1.0.42'

ARCHIVE_BASE_CHAPTER6_1_NAME='setup_heroes_chronicles_chapter_6_-_the_fiery_moon_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER6_1_MD5='86749a928d5d6fbdfe5e8a9e04ab595b'
ARCHIVE_BASE_CHAPTER6_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER6_1_SIZE='465968'
ARCHIVE_BASE_CHAPTER6_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER6_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER6_0_NAME='setup_heroes_chronicles_chapter6_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER6_0_MD5='64becfde1882eecd93fb02bf215eff11'
ARCHIVE_BASE_CHAPTER6_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER6_0_SIZE='470000'
ARCHIVE_BASE_CHAPTER6_0_VERSION='1.0-gog2.1.0.42'

ARCHIVE_BASE_CHAPTER7_1_NAME='setup_heroes_chronicles_chapter_7_-_revolt_of_the_beastmasters_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER7_1_MD5='4e9cabbb311ba9ba7b80d28226d6183b'
ARCHIVE_BASE_CHAPTER7_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER7_1_SIZE='499938'
ARCHIVE_BASE_CHAPTER7_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER7_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER7_0_NAME='setup_heroes_chronicles_chapter7_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER7_0_MD5='07c189a731886b2d3891ac1c65581d40'
ARCHIVE_BASE_CHAPTER7_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER7_0_SIZE='500000'
ARCHIVE_BASE_CHAPTER7_0_VERSION='1.0-gog2.1.0.42'

ARCHIVE_BASE_CHAPTER8_1_NAME='setup_heroes_chronicles_chapter_8_-_the_sword_of_frost_1.0_gog_v2_(83337).exe'
ARCHIVE_BASE_CHAPTER8_1_MD5='ebceb89113e7dd31942c3971122cbd73'
ARCHIVE_BASE_CHAPTER8_1_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER8_1_SIZE='480343'
ARCHIVE_BASE_CHAPTER8_1_VERSION='1.0-gog83337'
ARCHIVE_BASE_CHAPTER8_1_URL='https://www.gog.com/game/heroes_chronicles_all_chapters'

ARCHIVE_BASE_CHAPTER8_0_NAME='setup_heroes_chronicles_chapter8_2.1.0.42.exe'
ARCHIVE_BASE_CHAPTER8_0_MD5='2b3e4c366db0f7e3e8b15b0935aad528'
ARCHIVE_BASE_CHAPTER8_0_TYPE='innosetup'
ARCHIVE_BASE_CHAPTER8_0_SIZE='480000'
ARCHIVE_BASE_CHAPTER8_0_VERSION='1.0-gog2.1.0.42'

CONTENT_INNER_PATH_CHAPTER1='Warlords of the Wasteland'
CONTENT_INNER_PATH_CHAPTER2='Conquest of the Underworld'
CONTENT_INNER_PATH_CHAPTER3='Masters of the Elements'
CONTENT_INNER_PATH_CHAPTER4='Clash of the Dragons'
CONTENT_INNER_PATH_CHAPTER5='The World Tree'
CONTENT_INNER_PATH_CHAPTER6='The Fiery Moon'
CONTENT_INNER_PATH_CHAPTER7='Revolt of the Beastmasters'
CONTENT_INNER_PATH_CHAPTER8='The Sword of Frost'
CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_CHAPTER1_0='app'
CONTENT_PATH_DEFAULT_CHAPTER2_0='app'
CONTENT_PATH_DEFAULT_CHAPTER3_0='app'
CONTENT_PATH_DEFAULT_CHAPTER4_0='app'
CONTENT_PATH_DEFAULT_CHAPTER5_0='app'
CONTENT_PATH_DEFAULT_CHAPTER6_0='app'
CONTENT_PATH_DEFAULT_CHAPTER7_0='app'
CONTENT_PATH_DEFAULT_CHAPTER8_0='app'
CONTENT_GAME_BIN_FILES_CHAPTER1="
${CONTENT_INNER_PATH_CHAPTER1}/Warlords.exe"
CONTENT_GAME_BIN_FILES_CHAPTER2="
${CONTENT_INNER_PATH_CHAPTER2}/Underworld.exe"
CONTENT_GAME_BIN_FILES_CHAPTER3="
${CONTENT_INNER_PATH_CHAPTER3}/Elements.exe"
CONTENT_GAME_BIN_FILES_CHAPTER4="
${CONTENT_INNER_PATH_CHAPTER4}/Dragons.exe"
CONTENT_GAME_BIN_FILES_CHAPTER5="
${CONTENT_INNER_PATH_CHAPTER5}/WorldTree.exe"
CONTENT_GAME_BIN_FILES_CHAPTER6="
${CONTENT_INNER_PATH_CHAPTER6}/FieryMoon.exe"
CONTENT_GAME_BIN_FILES_CHAPTER7="
${CONTENT_INNER_PATH_CHAPTER7}/Beastmaster.exe"
CONTENT_GAME_BIN_FILES_CHAPTER8="
${CONTENT_INNER_PATH_CHAPTER8}/Sword.exe"
# *.exe requirements
CONTENT_LIBS_BIN_FILES='
*/mss32.dll
*/smackw32.dll
*/binkw32.dll
*/IFC20.dll'
CONTENT_GAME0_BIN_FILES='
*/MP3DEC.ASI'
CONTENT_GAME_DATA_FILES='
*/Data'
CONTENT_GAME_COMMON_FILES='
Data
MP3'
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER1="$CONTENT_INNER_PATH_CHAPTER1"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER2="$CONTENT_INNER_PATH_CHAPTER2"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER3="$CONTENT_INNER_PATH_CHAPTER3"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER4="$CONTENT_INNER_PATH_CHAPTER4"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER5="$CONTENT_INNER_PATH_CHAPTER5"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER6="$CONTENT_INNER_PATH_CHAPTER6"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER7="$CONTENT_INNER_PATH_CHAPTER7"
CONTENT_DOC_DATA_RELATIVE_PATH_CHAPTER8="$CONTENT_INNER_PATH_CHAPTER8"
CONTENT_DOC_DATA_FILES='
*.pdf
*.txt'

USER_PERSISTENT_DIRECTORIES_CHAPTER1="
${CONTENT_INNER_PATH_CHAPTER1}/games
${CONTENT_INNER_PATH_CHAPTER1}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER2="
${CONTENT_INNER_PATH_CHAPTER2}/games
${CONTENT_INNER_PATH_CHAPTER2}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER3="
${CONTENT_INNER_PATH_CHAPTER3}/games
${CONTENT_INNER_PATH_CHAPTER3}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER4="
${CONTENT_INNER_PATH_CHAPTER4}/games
${CONTENT_INNER_PATH_CHAPTER4}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER5="
${CONTENT_INNER_PATH_CHAPTER5}/games
${CONTENT_INNER_PATH_CHAPTER5}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER6="
${CONTENT_INNER_PATH_CHAPTER6}/games
${CONTENT_INNER_PATH_CHAPTER6}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER7="
${CONTENT_INNER_PATH_CHAPTER7}/games
${CONTENT_INNER_PATH_CHAPTER7}/Maps"
USER_PERSISTENT_DIRECTORIES_CHAPTER8="
${CONTENT_INNER_PATH_CHAPTER8}/games
${CONTENT_INNER_PATH_CHAPTER8}/Maps"
USER_PERSISTENT_FILES='
Data/*.lod
*/Data/*.lod'

# Allow to skip intro video on first launch + set default settings.
# TODO: Drop unrequired settings.
WINE_REGISTRY_INIT='
"Animate SpellBook"=dword:00000001
"Autosave"=dword:00000001
"Bink Video"=dword:00000000
"Blackout Computer"=dword:00000000
"Combat Army Info Level"=dword:00000000
"Combat Auto Creatures"=dword:00000001
"Combat Auto Spells"=dword:00000001
"Combat Ballista"=dword:00000001
"Combat Catapult"=dword:00000001
"Combat First Aid Tent"=dword:00000001
"Combat Shade Level"=dword:00000000
"Combat Speed"=dword:00000000
"Computer Walk Speed"=dword:00000003
"First Time"=dword:00000000
"Last Music Volume"=dword:00000005
"Last Sound Volume"=dword:00000005
"Main Game Full Screen"=dword:00000001
"Main Game Show Menu"=dword:00000001
"Main Game X"=dword:0000000a
"Main Game Y"=dword:0000000a
"Move Reminder"=dword:00000001
"Music Volume"=dword:00000005
"Quick Combat"=dword:00000000
"Show Combat Grid"=dword:00000000
"Show Combat Mouse Hex"=dword:00000000
"Show Intro"=dword:00000001
"Show Route"=dword:00000001
"Sound Volume"=dword:00000005
"Test Blit"=dword:00000000
"Test Decomp"=dword:00000000
"Test Read"=dword:00000000
"Town Outlines"=dword:00000001
"Video Subtitles"=dword:00000001
"Walk Speed"=dword:00000002
"Window Scroll Speed"=dword:00000001'
WINE_REGISTRY_INIT_CHAPTER1="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Warlords]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER1}\\\\${CONTENT_INNER_PATH_CHAPTER1}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER2="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Underworld]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER2}\\\\${CONTENT_INNER_PATH_CHAPTER2}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER3="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Elements]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER3}\\\\${CONTENT_INNER_PATH_CHAPTER3}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER4="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Dragons]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER4}\\\\${CONTENT_INNER_PATH_CHAPTER4}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER5="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\WorldTree]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER5}\\\\${CONTENT_INNER_PATH_CHAPTER5}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER6="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\FieryMoon]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER6}\\\\${CONTENT_INNER_PATH_CHAPTER6}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER7="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Beastmaster]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER7}\\\\${CONTENT_INNER_PATH_CHAPTER7}\\\\\"
$WINE_REGISTRY_INIT"
WINE_REGISTRY_INIT_CHAPTER8="
[HKEY_LOCAL_MACHINE\\Software\\New World Computing\\Heroes Chronicles\\Sword]
\"AppPath\"=\"C:\\\\${GAME_ID_CHAPTER8}\\\\${CONTENT_INNER_PATH_CHAPTER8}\\\\\"
$WINE_REGISTRY_INIT"

APP_MAIN_EXE_CHAPTER1="${CONTENT_INNER_PATH_CHAPTER1}/Warlords.exe"
APP_MAIN_EXE_CHAPTER2="${CONTENT_INNER_PATH_CHAPTER2}/Underworld.exe"
APP_MAIN_EXE_CHAPTER3="${CONTENT_INNER_PATH_CHAPTER3}/Elements.exe"
APP_MAIN_EXE_CHAPTER4="${CONTENT_INNER_PATH_CHAPTER4}/Dragons.exe"
APP_MAIN_EXE_CHAPTER5="${CONTENT_INNER_PATH_CHAPTER5}/WorldTree.exe"
APP_MAIN_EXE_CHAPTER6="${CONTENT_INNER_PATH_CHAPTER6}/FieryMoon.exe"
APP_MAIN_EXE_CHAPTER7="${CONTENT_INNER_PATH_CHAPTER7}/Beastmaster.exe"
APP_MAIN_EXE_CHAPTER8="${CONTENT_INNER_PATH_CHAPTER8}/Sword.exe"

PACKAGES_LIST='
PKG_BIN
PKG_COMMON
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_COMMON_ID="${GAME_ID}-common"
PKG_COMMON_DESCRIPTION="data shared between all $GAME_NAME games"

PKG_DATA_ID_CHAPTER1="${GAME_ID_CHAPTER1}-data"
PKG_DATA_ID_CHAPTER2="${GAME_ID_CHAPTER2}-data"
PKG_DATA_ID_CHAPTER3="${GAME_ID_CHAPTER3}-data"
PKG_DATA_ID_CHAPTER4="${GAME_ID_CHAPTER4}-data"
PKG_DATA_ID_CHAPTER5="${GAME_ID_CHAPTER5}-data"
PKG_DATA_ID_CHAPTER6="${GAME_ID_CHAPTER6}-data"
PKG_DATA_ID_CHAPTER7="${GAME_ID_CHAPTER7}-data"
PKG_DATA_ID_CHAPTER8="${GAME_ID_CHAPTER8}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_COMMON'

# 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

# Enforce consistent case for all games.
# While the content inclusion step is case-agnostic,
# the list of user-writable paths is case-specific.

inner_game_path="$(path_archive_contents)/$(context_value 'CONTENT_INNER_PATH')"
if [ -e "${inner_game_path}/data" ]; then
	mv "${inner_game_path}/data" "${inner_game_path}/Data"
fi

# Use the original game binary, without DDrawCompat hack applied.

path_exe_patched="$(path_archive_contents)/$(application_exe 'APP_MAIN')"
path_exe_original="${path_exe_patched%.exe}_og.exe"
mv "$path_exe_original" "$path_exe_patched"

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Use a common directory and symbolic links to handle data shared between all Heroes Chronicles games.
PATH_GAME_COMMON=$(
	unset "$(context_name_archive 'GAME_ID')"
	path_game_data
)
content_inclusion 'GAME_COMMON' 'PKG_COMMON' "$PATH_GAME_COMMON"
content_inclusion_default

# Write launchers

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	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
	EOF
}

# Run the game binary from its parent directory.
game_exec_line() {
	# shellcheck disable=SC2086
	local application=$1

	local application_exe
	application_exe=$(application_exe "$application")
	cat <<- EOF
	cd "$(dirname "$application_exe")"
	\$(wine_command) "$(basename "$application_exe")" "\$@"
	EOF
}

launchers_generation

# Build packages.

# Use a common directory and symbolic links to handle data shared between all Heroes Chronicles games.
PKG_DATA_POSTINST_RUN="$(package_postinst_actions 'PKG_DATA')
# Link common files shared by the games series.
ln --symbolic '${PATH_GAME_COMMON}/Data' '$(path_game_data)'
ln --symbolic '${PATH_GAME_COMMON}/MP3' '$(path_game_data)'"
PKG_DATA_PRERM_RUN="$(package_prerm_actions 'PKG_DATA')
# Delete links to common files shared by the games series.
rm '$(path_game_data)/Data'
rm '$(path_game_data)/MP3'"
(
	# Use the generic values of GAME_ID and GAME_NAME for the shared data package.
	unset "$(context_name_archive 'GAME_ID')"
	unset "$(context_name_archive 'GAME_NAME')"
	packages_generation 'PKG_COMMON'
)
packages_generation 'PKG_BIN' 'PKG_DATA'
print_instructions

# Clean up.

working_directory_cleanup

exit 0
