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

###
# Empire Earth
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260319.6

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='empire-earth-1'
GAME_NAME='Empire Earth'

GAME_ID_CONQUEST='the-art-of-conquest'
GAME_NAME_CONQUEST='The Art of Conquest'

ARCHIVE_BASE_1_NAME='setup_empire_earth_gold_2.0.0.2974_gog_v3_(78415).exe'
ARCHIVE_BASE_1_MD5='fcad43c5606236f5fe5023a40f6b539e'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_SIZE='703925'
ARCHIVE_BASE_1_VERSION='2.0.0.2974-gog78415'
ARCHIVE_BASE_1_URL='https://www.gog.com/game/empire_earth_gold_edition'

ARCHIVE_BASE_0_NAME='setup_empire_earth_gold_2.0.0.2974_(25522).exe'
ARCHIVE_BASE_0_MD5='e8b22cdc9520aefab49e21349b20b731'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='710000'
ARCHIVE_BASE_0_VERSION='2.0.0.2974-gog25522'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_INNER_BASE='Empire Earth'
CONTENT_PATH_INNER_CONQUEST='Empire Earth - The Art of Conquest'
CONTENT_GAME_BIN_RELATIVE_PATH="$CONTENT_PATH_INNER_BASE"
CONTENT_GAME_BIN_FILES='
redist
SierraCom.cfg
WONLobby.cfg
zzWONVersion.cfg
Empire Earth.exe
sierra.inf'
CONTENT_GAME_BIN_CONQUEST_RELATIVE_PATH="$CONTENT_PATH_INNER_CONQUEST"
CONTENT_GAME_BIN_CONQUEST_FILES='
redist
WONLobby.cfg
zzWONVersion.cfg
EE-AOC.exe'
# TODO: Check if the following binaries are required.
CONTENT_GAME0_BIN_RELATIVE_PATH="$CONTENT_PATH_INNER_BASE"
CONTENT_GAME0_BIN_FILES='
dxcfg.exe
rmvparse.exe'
CONTENT_GAME0_BIN_CONQUEST_RELATIVE_PATH="$CONTENT_PATH_INNER_CONQUEST"
CONTENT_GAME0_BIN_CONQUEST_FILES='
dxcfg.exe'
# TODO: Include an explicit list of libraries.
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME1_BIN_RELATIVE_PATH="$CONTENT_PATH_INNER_BASE"
CONTENT_GAME1_BIN_FILES='
*.dll'
CONTENT_GAME1_BIN_CONQUEST_RELATIVE_PATH="$CONTENT_PATH_INNER_CONQUEST"
CONTENT_GAME1_BIN_CONQUEST_FILES='
*.dll'
CONTENT_GAME_DATA_RELATIVE_PATH="$CONTENT_PATH_INNER_BASE"
CONTENT_GAME_DATA_CONQUEST_RELATIVE_PATH="$CONTENT_PATH_INNER_CONQUEST"
CONTENT_GAME_DATA_FILES='
Data'
CONTENT_GAME_DATA_CONQUEST_FILES='
Data'
CONTENT_DOC_DATA_FILES='
Manual.pdf
scenario_editor_manual.pdf
technology_tree.pdf'
CONTENT_DOC_DATA_CONQUEST_FILES='
manual_aoc.pdf
technology_tree_aoc.pdf'

# Allow running the expansion without having to start the base game first.
# First "$GAME_ID" is the standard path component, second one is the inner path.
installed_from_directory="\\\\${GAME_ID}\\\\${GAME_ID}\\\\"
WINE_REGISTRY_INIT='
[HKEY_CURRENT_USER\Software\SSSI\Empire Earth]
"Installed From Directory"="'"${installed_from_directory}"'"
"Installed From Volume"="C:"'
# Ensure that background music is played during gameplay.
# Without these verbs, music is only played in the game menus.
# (WINE 10.0)
# TODO: Identify the role of each library.
WINE_WINETRICKS_VERBS='dmusic dmloader dmsynth dmime dmband dmcompos dmstyle'

USER_PERSISTENT_DIRECTORIES="
${GAME_ID}/Data/Scenarios
${GAME_ID}/Data/Saved Games
${GAME_ID}/Users
${GAME_ID_CONQUEST}/Data/Scenarios
${GAME_ID_CONQUEST}/Data/Saved Games
${GAME_ID_CONQUEST}/Users"

APP_MAIN_EXE='Empire Earth.exe'
APP_MAIN_ICON="${CONTENT_PATH_INNER_BASE}/Empire Earth.exe"

APP_CONQUEST_ID="${GAME_ID}-${GAME_ID_CONQUEST}"
APP_CONQUEST_NAME="$GAME_NAME - $GAME_NAME_CONQUEST"
APP_CONQUEST_EXE='EE-AOC.exe'
APP_CONQUEST_ICON="${CONTENT_PATH_INNER_CONQUEST}/EE-AOC.exe"

PACKAGES_LIST='
PKG_BIN
PKG_DATA
PKG_BIN_CONQUEST
PKG_DATA_CONQUEST'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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

PKG_BIN_CONQUEST_ID="${GAME_ID}-${GAME_ID_CONQUEST}"
PKG_BIN_CONQUEST_ARCH='32'
PKG_BIN_CONQUEST_DESCRIPTION="$GAME_NAME_CONQUEST"
PKG_BIN_CONQUEST_DEPENDENCIES_SIBLINGS='
PKG_BIN
PKG_DATA_CONQUEST'

PKG_DATA_CONQUEST_ID="${GAME_ID}-${GAME_ID_CONQUEST}-data"
PKG_DATA_CONQUEST_DESCRIPTION="$GAME_NAME_CONQUEST - data"
PKG_DATA_CONQUEST_DEPENDENCIES_SIBLINGS='
PKG_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_icons 'PKG_DATA_CONQUEST' 'APP_CONQUEST'
content_inclusion 'GAME_BIN'  'PKG_BIN'  "$(path_game_data)/$(game_id)"
content_inclusion 'GAME0_BIN' 'PKG_BIN'  "$(path_game_data)/$(game_id)"
content_inclusion 'GAME1_BIN' 'PKG_BIN'  "$(path_game_data)/$(game_id)"
content_inclusion 'GAME_DATA' 'PKG_DATA' "$(path_game_data)/$(game_id)"
content_inclusion 'DOC_DATA'  'PKG_DATA' "$(path_documentation)/$(game_id)"
content_inclusion 'GAME_BIN_CONQUEST'  'PKG_BIN_CONQUEST'  "$(path_game_data)/${GAME_ID_CONQUEST}"
content_inclusion 'GAME0_BIN_CONQUEST' 'PKG_BIN_CONQUEST'  "$(path_game_data)/${GAME_ID_CONQUEST}"
content_inclusion 'GAME1_BIN_CONQUEST' 'PKG_BIN_CONQUEST'  "$(path_game_data)/${GAME_ID_CONQUEST}"
content_inclusion 'GAME_DATA_CONQUEST' 'PKG_DATA_CONQUEST' "$(path_game_data)/${GAME_ID_CONQUEST}"
content_inclusion 'DOC_DATA_CONQUEST'  'PKG_DATA_CONQUEST' "$(path_documentation)/${GAME_ID_CONQUEST}"
rm --recursive "${PLAYIT_WORKDIR}/gamedata"

# Write launchers.

# 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
}

APP_MAIN_EXE="$(game_id)/$(application_exe 'APP_MAIN')"
APP_CONQUEST_EXE="${GAME_ID_CONQUEST}/$(application_exe 'APP_CONQUEST')"
launchers_generation 'PKG_BIN' 'APP_MAIN'
# Prevent a file path conflict if the registry script
# is created both for the base game and the expansion.
wine_registry_script_write() { return 0; }
launchers_generation 'PKG_BIN_CONQUEST' 'APP_CONQUEST'

# Build packages.

packages_generation
print_instructions 'PKG_BIN' 'PKG_DATA'
(
	GAME_NAME="$GAME_NAME - $GAME_NAME_CONQUEST"
	print_instructions 'PKG_BIN_CONQUEST' 'PKG_DATA_CONQUEST'
)

# Clean up.

working_directory_cleanup

exit 0
