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

###
# Impression Games city builders:
# - Zeus: Master of Olympus
# - Emperor: Rise of the Middle Kingdom
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260329.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_ZEUS='zeus-master-of-olympus'
GAME_NAME_ZEUS='Zeus: Master of Olympus'

GAME_ID_EMPEROR='emperor-rise-of-the-middle-kingdom'
GAME_NAME_EMPEROR='Emperor: Rise of the Middle Kingdom'

ARCHIVE_BASE_ZEUS_0_NAME='setup_zeus_and_poseidon_2.1.0.10.exe'
ARCHIVE_BASE_ZEUS_0_MD5='f26f9ed5ecaa4e58fca64acb88255107'
# Convert file paths to lower case, for easier installation of localisations.
ARCHIVE_BASE_ZEUS_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_ZEUS_0_EXTRACTOR_OPTIONS='--lowercase'
ARCHIVE_BASE_ZEUS_0_SIZE='800000'
ARCHIVE_BASE_ZEUS_0_VERSION='2.1-gog2.1.0.10'
ARCHIVE_BASE_ZEUS_0_URL='https://www.gog.com/game/zeus_poseidon'

ARCHIVE_BASE_EMPEROR_0_NAME='setup_emperor_rise_of_the_middle_kingdom_2.0.0.2.exe'
ARCHIVE_BASE_EMPEROR_0_MD5='5e50e84c028a85eafe5dd5f2aa277fea'
# Convert file paths to lower case, for easier installation of localisations.
ARCHIVE_BASE_EMPEROR_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_EMPEROR_0_EXTRACTOR_OPTIONS='--lowercase'
ARCHIVE_BASE_EMPEROR_0_SIZE='820000'
ARCHIVE_BASE_EMPEROR_0_VERSION='1.0.1.0-gog2.0.0.2'
ARCHIVE_BASE_EMPEROR_0_URL='https://www.gog.com/game/emperor_rise_of_the_middle_kingdom'

CONTENT_PATH_DEFAULT='app'
CONTENT_GAME_BIN_FILES_ZEUS='
Zeus.exe
Zeus.ini
Zeus.inf'
CONTENT_GAME_BIN_FILES_EMPEROR='
Emperor.exe
Emperor.ini
Emperor.inf'
# Zeus.exe / Emperor.exe requirements
CONTENT_LIBS_BIN_FILES='
mss32.dll
binkw32.dll
ijl10.dll'
CONTENT_GAME0_BIN_FILES='
mp3dec.asi
mssrsx.m3d
mssds3dh.m3d'
CONTENT_GAME_EDITOR_FILES_EMPEROR='
EmperorEdit.exe
EmperorEdit.inf'
CONTENT_GAME_DATA_FILES='
Audio
Binks
DATA
Model'
CONTENT_GAME0_DATA_FILES_ZEUS='
Adventures
zeus_mm.eng
zeus_text.eng
zeusmap.inf'
CONTENT_GAME0_DATA_FILES_EMPEROR='
Campaigns
Cities
Res
emperormm.eng
emperortext.eng
emperormap.inf'
CONTENT_DOC_EDITOR_FILES_EMPEROR='
emperorcampaigncreator.pdf'
CONTENT_DOC_DATA_FILES_ZEUS='
zeus - manual.pdf
zeus - poseidon - manual.pdf
zeus - quick reference card.pdf
readme.txt'
CONTENT_DOC_DATA_FILES_EMPEROR='
emperormanual.pdf
readme.txt
bonuscampreadme.txt'

USER_PERSISTENT_DIRECTORIES='
Save'
USER_PERSISTENT_FILES='
*.ini'

# Prevent graphics flickering.
# cf. https://bugs.winehq.org/show_bug.cgi?id=39351
# (WINE 11.5)
WINE_WINETRICKS_VERBS='csmt=off'
# The game window fails to render anything unless the game runs in a WINE virtual desktop.
# (WINE 11.5)
WINE_VIRTUAL_DESKTOP='1024x768'

# Without an explicit list, APP_EDIT would be picked up for both games.
APPLICATIONS_LIST_ZEUS='
APP_MAIN'
APPLICATIONS_LIST_EMPEROR='
APP_MAIN
APP_EDIT'

APP_MAIN_EXE_ZEUS='zeus.exe'
APP_MAIN_EXE_EMPEROR='emperor.exe'
APP_MAIN_ICON_ZEUS='poseidon.ico'
APP_MAIN_ICON_EMPEROR='dragon.ico'

APP_EDITOR_ID_EMPEROR="${GAME_ID_EMPEROR}-editor"
APP_EDITOR_NAME_EMPEROR="$GAME_NAME_EMPEROR - Editor"
APP_EDITOR_EXE_EMPEROR='emperoredit.exe'

PACKAGES_LIST_ZEUS='
PKG_BIN
PKG_DATA'
PACKAGES_LIST_EMPEROR='
PKG_BIN
PKG_EDITOR
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_EDITOR_ID_EMPEROR="${GAME_ID_EMPEROR}-editor"
PKG_EDITOR_ARCH='32'
PKG_EDITOR_DEPENDENCIES_SIBLINGS='
PKG_BIN'

PKG_DATA_ID_ZEUS="${GAME_ID_ZEUS}-data"
PKG_DATA_ID_EMPEROR="${GAME_ID_EMPEROR}-data"
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

# Include game data.

content_inclusion_icons 'PKG_DATA' 'APP_MAIN'
content_inclusion_default

# Write launchers.

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

# Work around missing animations on high uptime.
game_exec_line() {
	# shellcheck disable=SC2086
	local application=$1

	local application_exe
	application_exe=$(application_exe "$application")
	cat <<- EOF
	# Work around missing animations on high uptime.
	unshare --map-current-user --time --boottime -\$((\$(cut --delimiter=. --fields=1 /proc/uptime)-42)) -- \\
	    \$(wine_command) "$application_exe" "\$@"
	EOF
}

launchers_generation 'PKG_BIN' 'APP_MAIN'

# Emperor - Generate a launcher for the editor.
case "$(current_archive)" in
	('ARCHIVE_BASE_EMPEROR_'*)
		# Use the main game icon.
		desktop_field_icon() { printf '%s' "$(game_id)"; }
		launchers_generation 'PKG_EDITOR' 'APP_EDITOR'
	;;
esac

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
