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

###
# Heroes of Might and Magic 3
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260317.5

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='heroes-of-might-and-magic-3'
GAME_NAME='Heroes of Might and Magic III'

ARCHIVE_BASE_EN_1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(77075).exe'
ARCHIVE_BASE_EN_1_MD5='b1c0f5b5d83c17bede6bdd97762b648a'
ARCHIVE_BASE_EN_1_TYPE='innosetup'
ARCHIVE_BASE_EN_1_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(77075)-1.bin'
ARCHIVE_BASE_EN_1_PART1_MD5='0f0aa941fdd99f78a1b58ff5e5d9eb74'
ARCHIVE_BASE_EN_1_SIZE='1028132'
ARCHIVE_BASE_EN_1_VERSION='4.0-gog77075'
ARCHIVE_BASE_EN_1_URL='https://www.gog.com/game/heroes_of_might_and_magic_3_complete_edition'

ARCHIVE_BASE_FR_1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(french)_(77075).exe'
ARCHIVE_BASE_FR_1_MD5='940d3b2dfc0c31427ec66d68058d73c5'
ARCHIVE_BASE_FR_1_TYPE='innosetup'
ARCHIVE_BASE_FR_1_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(french)_(77075)-1.bin'
ARCHIVE_BASE_FR_1_PART1_MD5='c1933c4f7009b3b9729160b9021d7863'
ARCHIVE_BASE_FR_1_SIZE='1024066'
ARCHIVE_BASE_FR_1_VERSION='4.0-gog77075'
ARCHIVE_BASE_FR_1_URL='https://www.gog.com/game/heroes_of_might_and_magic_3_complete_edition'

ARCHIVE_BASE_RU_1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(russian)_(77075).exe'
ARCHIVE_BASE_RU_1_MD5='77817c7c60d5980f5fcebd55e7dcb08c'
ARCHIVE_BASE_RU_1_TYPE='innosetup'
ARCHIVE_BASE_RU_1_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(3.2)_gog_0.1_(russian)_(77075)-1.bin'
ARCHIVE_BASE_RU_1_PART1_MD5='32af9457f179c3f4bf3b10f76cfde71f'
ARCHIVE_BASE_RU_1_SIZE='1000278'
ARCHIVE_BASE_RU_1_VERSION='4.0-gog77075'
ARCHIVE_BASE_RU_1_URL='https://www.gog.com/game/heroes_of_might_and_magic_3_complete_edition'

ARCHIVE_BASE_EN_0_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(28740).exe'
ARCHIVE_BASE_EN_0_MD5='8dcd6c4a8c72c65a6920665e28245c57'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(28740)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5='4285d54f27c40e815905c7069b6f9f84'
ARCHIVE_BASE_EN_0_SIZE='1100000'
ARCHIVE_BASE_EN_0_VERSION='4.0-gog28740'

ARCHIVE_BASE_FR_0_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(french)_(28740).exe'
ARCHIVE_BASE_FR_0_MD5='be4b59590146299dbe77bda7a4ea4178'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(french)_(28740)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5='88b71e0fd44e5be1ad6791e74120c61c'
ARCHIVE_BASE_FR_0_SIZE='1100000'
ARCHIVE_BASE_FR_0_VERSION='4.0-gog28740'

ARCHIVE_BASE_RU_0_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(russian)_(28740).exe'
ARCHIVE_BASE_RU_0_MD5='33018e8355f59b9e532d7429e1aaeeae'
ARCHIVE_BASE_RU_0_TYPE='innosetup'
ARCHIVE_BASE_RU_0_PART1_NAME='setup_heroes_of_might_and_magic_3_complete_4.0_(russian)_(28740)-1.bin'
ARCHIVE_BASE_RU_0_PART1_MD5='5d409ee2e2b3ec32fa9838ca1d601e02'
ARCHIVE_BASE_RU_0_SIZE='1100000'
ARCHIVE_BASE_RU_0_VERSION='4.0-gog28740'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_WINE_FILES='
MP3DEC.ASI
H3Blade.exe
Heroes3.exe'
# TODO: Move map/campaign editors into a dedicated package.
CONTENT_GAME0_BIN_WINE_FILES='
h3bmaped.exe
h3camped.exe
h3ccmped.exe
h3maped.exe'
# TODO: Move libraries to a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME1_BIN_WINE_FILES='
BINKW32.DLL
IFC20.dll
IFC21.dll
mcp.dll
MSS32.DLL
SMACKW32.DLL'
CONTENT_GAME_DATA_FILES='
Data
Maps
mp3'
CONTENT_DOC_DATA_FILES='
EULA
*.cnt
*.hlp
*.pdf
*.txt'

USER_PERSISTENT_DIRECTORIES='
config
games
Maps
random_maps'
USER_PERSISTENT_FILES='
Data/*.lod'

# Allow to skip intro video on first launch + set default settings.
# TODO: Check if all settings are required.
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\New World Computing\Heroes of Might and Magic® III\1.0]
"Animate SpellBook"=dword:00000001
"Autosave"=dword:00000001
"Bink Video"=dword:00000001
"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
"Network Default Name"="Player"
"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'

APPLICATIONS_LIST_BIN_WINE='
APP_MAIN
APP_EDITOR_MAP
APP_EDITOR_CAMPAIGN'
APPLICATIONS_LIST_BIN_VCMI='
APP_MAIN'

APP_MAIN_TYPE_BIN_VCMI='custom'
APP_MAIN_EXE_BIN_WINE='Heroes3.exe'
APP_MAIN_ICON='Heroes3.exe'

APP_EDITOR_MAP_ID="${GAME_ID}-map-editor"
APP_EDITOR_MAP_EXE='h3maped.exe'
APP_EDITOR_MAP_NAME="$GAME_NAME - Map editor"

APP_EDITOR_CAMPAIGN_ID="${GAME_ID}-campaign-editor"
APP_EDITOR_CAMPAIGN_EXE='h3ccmped.exe'
APP_EDITOR_CAMPAIGN_NAME="$GAME_NAME - Campaign editor"

PACKAGES_LIST='
PKG_BIN_WINE
PKG_BIN_VCMI
PKG_DATA'

PKG_BIN_ID="$GAME_ID"

PKG_BIN_WINE_ID="${PKG_BIN_ID}-wine"
PKG_BIN_WINE_ID_EN="${PKG_BIN_WINE_ID}-en"
PKG_BIN_WINE_ID_FR="${PKG_BIN_WINE_ID}-fr"
PKG_BIN_WINE_ID_RU="${PKG_BIN_WINE_ID}-ru"
PKG_BIN_WINE_PROVIDES="
$PKG_BIN_ID"
PKG_BIN_WINE_ARCH='32'
PKG_BIN_WINE_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_BIN_VCMI_ID="${PKG_BIN_ID}-vcmi"
PKG_BIN_VCMI_PROVIDES="
$PKG_BIN_ID"
PKG_BIN_VCMI_DEPENDENCIES_SIBLINGS='
PKG_DATA_BASE'
PKG_BIN_VCMI_DEPENDENCIES_COMMANDS='
vcmilauncher'

PKG_DATA_BASE_ID="${GAME_ID}-data"
PKG_DATA_ID_EN="${PKG_DATA_BASE_ID}-en"
PKG_DATA_ID_FR="${PKG_DATA_BASE_ID}-fr"
PKG_DATA_ID_RU="${PKG_DATA_BASE_ID}-ru"
PKG_DATA_PROVIDES="
$PKG_DATA_BASE_ID"
PKG_DATA_DESCRIPTION='data'

# 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

# Fix the ability to run the game through WINE, by reverting a GOG patch.
# This replaces a call to "xdd.dll" with the original call to "ddraw.dll".
# cf. https://www.gog.com/forum/heroes_of_might_and_magic_series/if_you_cant_get_homm3_to_work_after_the_latest_update_on_linux_with_wine_read_this/post1

case "$(current_archive)" in
	('ARCHIVE_BASE_'??'_1')
		sed --in-place 's/xdd\.dll../ddraw.dll/' "$(path_archive_contents)/Heroes3.exe"
	;;
esac

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

launchers_generation 'PKG_BIN_WINE'
# WARNING: VCMI has been polluted by generative AI.
custom_launcher() {
	launcher_headers
	cat <<- EOF
	GAME_PATH="$(path_game_data)"
	EOF
	cat <<- 'EOF'
	VCMI_DATA="${XDG_DATA_HOME:="${HOME}/.local/share"}/vcmi"

	for directory in Data Maps mp3; do
	    if [ ! -e "${VCMI_DATA}/${directory}" ]; then
	        mkdir --parents "$VCMI_DATA"
	        cp --recursive --remove-destination --symbolic-link "${GAME_PATH}/${directory}" "$VCMI_DATA"
	    fi
	done

	# VCMI crashes on launch when SDL_VIDEODRIVER is set to "wayland".
	if [ "${SDL_VIDEODRIVER:-}" = "wayland" ]; then
	    unset SDL_VIDEODRIVER
	fi
	vcmilauncher

	exit 0
	EOF
}
launchers_generation 'PKG_BIN_VCMI'

# Build packages.

packages_generation
printf '\n'
printf 'WINE:'
print_instructions 'PKG_DATA' 'PKG_BIN_WINE'
case "$(messages_language)" in
	('fr')
		message='VCMI a été pollué par des IA génératives.'
	;;
	('en'|*)
		message='VCMI has been polluted by generative AI.'
	;;
esac
print_message 'warning' '%s\n' "$message"
printf 'VCMI:'
print_instructions 'PKG_DATA' 'PKG_BIN_VCMI'

# Clean up.

working_directory_cleanup

exit 0
