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

###
# Dungeon Keeper 2
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260224.1

PLAYIT_COMPATIBILITY_LEVEL='2.33'

GAME_ID='dungeon-keeper-2'
GAME_NAME='Dungeon Keeper II'

ARCHIVE_BASE_EN_2_NAME='setup_dungeon_keepertm_2_1.7_alttab_hotfix_(77775).exe'
ARCHIVE_BASE_EN_2_MD5='250ba86776e434e2fc65534a97e68e41'
ARCHIVE_BASE_EN_2_TYPE='innosetup'
ARCHIVE_BASE_EN_2_PART1_NAME='setup_dungeon_keepertm_2_1.7_alttab_hotfix_(77775)-1.bin'
ARCHIVE_BASE_EN_2_PART1_MD5='4cf9a2db8dfba58d6e869e2b124edbe0'
ARCHIVE_BASE_EN_2_SIZE='514101'
ARCHIVE_BASE_EN_2_VERSION='1.7-gog77775'
ARCHIVE_BASE_EN_2_URL='https://www.gog.com/game/dungeon_keeper_2'

ARCHIVE_BASE_FR_2_NAME='setup_dungeon_keepertm_2_1.7_alttab_hotfix_(french)_(77775).exe'
ARCHIVE_BASE_FR_2_MD5='1ce059082cddb831ea25e571c571eaef'
ARCHIVE_BASE_FR_2_TYPE='innosetup'
ARCHIVE_BASE_FR_2_PART1_NAME='setup_dungeon_keepertm_2_1.7_alttab_hotfix_(french)_(77775)-1.bin'
ARCHIVE_BASE_FR_2_PART1_MD5='5cd909d382a35f22ad93470e5281fa2e'
ARCHIVE_BASE_FR_2_SIZE='512981'
ARCHIVE_BASE_FR_2_VERSION='1.7-gog77775'
ARCHIVE_BASE_FR_2_URL='https://www.gog.com/game/dungeon_keeper_2'

ARCHIVE_BASE_EN_1_NAME='setup_dungeon_keeper_2_1.7_(22280).exe'
ARCHIVE_BASE_EN_1_MD5='eeb2229ee49518504884a9ac65f611ad'
ARCHIVE_BASE_EN_1_TYPE='innosetup'
ARCHIVE_BASE_EN_1_SIZE='500537'
ARCHIVE_BASE_EN_1_VERSION='1.7-gog22280'

ARCHIVE_BASE_EN_0_NAME='setup_dungeon_keeper2_2.0.0.32.exe'
ARCHIVE_BASE_EN_0_MD5='92d04f84dd870d9624cd18449d3622a5'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_SIZE='510000'
ARCHIVE_BASE_EN_0_VERSION='1.7-gog2.0.0.32'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_LIBS_BIN_FILES='
aweman32.dll
patch.dll
qmixer.dll
sfman32.dll
weanetr.dll'
CONTENT_GAME_BIN_FILES='
dkii.exe
dkii-dx.exe'
CONTENT_GAME_L10N_FILES='
data/sound/sfx/speech_*
data/text/default'
CONTENT_GAME_DATA_FILES='
data
dk2texturecache'
CONTENT_DOC_L10N_FILES='
eamseula.txt
readme.txt'
CONTENT_DOC_DATA_FILES='
manual.pdf
reference_card.pdf'

USER_PERSISTENT_DIRECTORIES='
data/settings
data/save'

WINE_REGEDIT_PERSISTENT_KEYS='
HKEY_CURRENT_USER\Software\Bullfrog Productions Ltd\Dungeon Keeper II'
# Without a virtual desktop, many 3D models disappear on loss of focus.
# (WINE 10.0)
WINE_VIRTUAL_DESKTOP='auto'

APP_MAIN_EXE='dkii-dx.exe'
APP_MAIN_ICON='dkii.exe'

PACKAGES_LIST='
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_ID_EN="${PKG_L10N_BASE_ID}-en"
PKG_L10N_ID_FR="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_DESCRIPTION_EN='English localisation'
PKG_L10N_DESCRIPTION_FR='French localisation'

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

# Load common functions.

# Scan library paths used by ./play.it ≥ 2.35.
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
# Scan library paths used by ./play.it < 2.35.
# This can go away when PLAYIT_COMPATIBILITY_LEVEL is bumped to 2.35 or higher.
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
# Error out if the library can not be found.
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.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	ln --force --symbolic $(path_libraries)/* "\${PATH_PREFIX}"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
