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

###
# Europa Universalis 2
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260405.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='europa-universalis-2'
GAME_NAME='Europa Universalis II'

ARCHIVE_BASE_0_NAME='setup_europa_universalis2_2.0.0.1.exe'
ARCHIVE_BASE_0_MD5='eb19eab80b45105d571bf684e18118a4'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_VERSION='1.09-gog2.0.0.1'
ARCHIVE_BASE_0_SIZE='430000'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/europa_universalis_ii'

CONTENT_PATH_DEFAULT='app'
CONTENT_GAME_BIN_FILES='
Config
net.cfg
settings.cfg
eu2.exe
EU2_Settings.exe
music.ini
STDOLE2.TLB'
# TODO: Move the scenario editor to a dedicated package.
CONTENT_GAME0_BIN_FILES='
Scenario Editor.exe'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME1_BIN_FILES='
ASYCFILT.DLL
comcat.dll
libbz2.dll
Msvbvm60.dll
OLEAUT32.DLL
OLEPRO32.DLL'
CONTENT_GAME_DATA_FILES='
AI
avi
DB
GFX
Map
music
Scenarios
sfx
Tutorial'
CONTENT_DOC_DATA_FILES='
EU2_manual.pdf
Readme.txt'
# TODO: Move the scenario editor to a dedicated package.
CONTENT_DOC0_DATA_FILES='
eu 2 event scripting.doc
ScenarioEditor.txt'

USER_PERSISTENT_DIRECTORIES='
Config
Scenarios/save games'
USER_PERSISTENT_FILES='
config.eu
history.txt
*.cfg
*.ini'

APP_MAIN_EXE='eu2.exe'

APP_SETTINGS_ID="${GAME_ID}-settings"
APP_SETTINGS_NAME="$GAME_NAME - Settings"
APP_SETTINGS_CAT='Settings'
APP_SETTINGS_EXE='EU2_Settings.exe'

APP_EDITOR_ID="${GAME_ID}-editor"
APP_EDITOR_NAME="$GAME_NAME - Scenario editor"
APP_EDITOR_EXE='Scenario Editor.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
application/x-id3'

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

# Fix music playback.

REQUIREMENTS_LIST='
dos2unix
unix2dos'

# 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 'libplayit.sh.2 not found.\n'
	exit 1
fi
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Fix music playback.

config_file="$(path_archive_contents)/music.ini"
config_field='music'
config_value="C:\\\\$(game_id)\\\\music"
sed_pattern="s/^${config_field}=.*$/${config_field}=${config_value}/"
dos2unix --quiet "$config_file"
sed --in-place "$sed_pattern" "$config_file"
unix2dos --quiet "$config_file"

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
