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

###
# The Guild 2 series:
# - The Guild 2
# - The Guild 2 Renaissance
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260319.7

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_ORIGINAL='the-guild-2'
GAME_NAME_ORIGINAL='The Guild 2'

GAME_ID_RENAISSANCE='the-guild-2-renaissance'
GAME_NAME_RENAISSANCE='The Guild 2 Renaissance'

ARCHIVE_BASE_ORIGINAL_0_NAME='setup_the_guild2_2.0.0.4.exe'
ARCHIVE_BASE_ORIGINAL_0_MD5='2b14191f4831b3486e95d52245d6cc64'
ARCHIVE_BASE_ORIGINAL_0_TYPE='innosetup'
ARCHIVE_BASE_ORIGINAL_0_SIZE='2400000'
ARCHIVE_BASE_ORIGINAL_0_VERSION='1.4-gog2.0.0.4'
ARCHIVE_BASE_ORIGINAL_0_URL='https://www.gog.com/game/the_guild_2'

# TODO: Add support for the French build.
ARCHIVE_BASE_RENAISSANCE_0_NAME='setup_the_guild2_renaissance_2.2.0.5.exe'
ARCHIVE_BASE_RENAISSANCE_0_MD5='86389c3154c2ea6ef3b072278f1e9c6c'
ARCHIVE_BASE_RENAISSANCE_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_RENAISSANCE_0_EXTRACTOR_OPTIONS='--gog'
ARCHIVE_BASE_RENAISSANCE_0_PART1_NAME='setup_the_guild2_renaissance_2.2.0.5-1.bin'
ARCHIVE_BASE_RENAISSANCE_0_PART1_MD5='ae4c17c8e3793befeec8b9a16e4f2b0c'
ARCHIVE_BASE_RENAISSANCE_0_PART1_EXTRACTOR='unar'
ARCHIVE_BASE_RENAISSANCE_0_SIZE='3800000'
ARCHIVE_BASE_RENAISSANCE_0_VERSION='4.21-gog2.2.0.5'
ARCHIVE_BASE_RENAISSANCE_0_URL='https://www.gog.com/game/the_guild_2_renaissance'

CONTENT_PATH_DEFAULT_ORIGINAL='app'
CONTENT_PATH_DEFAULT_RENAISSANCE='game'
CONTENT_GAME_BIN_FILES='
GuildII.exe'
CONTENT_GAME0_BIN_FILES_ORIGINAL='
Config.ini
input.ini
userconfig.ini'
CONTENT_GAME0_BIN_PATH_RENAISSANCE='support/app'
CONTENT_GAME0_BIN_FILES_RENAISSANCE='
config.ini
input.ini'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME1_BIN_FILES='
dbghelp.dll
fmod.dll
MFC71.dll
msvcp71.dll
msvcr71.dll
stlport.5.0.dll
stlportd.5.0.dll'
# TODO: Drop unused binaries.
CONTENT_GAME2_BIN_FILES_RENAISSANCE='
ModLauncher.exe
WMEncoderEN.exe'
CONTENT_GAME_DATA_FILES='
CameraPaths
DB
GUI
Movie
msx
Objects
Particles
Resource
Savegames
Scenes
Scripts
sfx
Shader
shots
Textures
Worlds
Sim_Commands.dat'
CONTENT_GAME0_DATA_FILES_ORIGINAL='
Editor'
CONTENT_GAME0_DATA_FILES_RENAISSANCE='
mods
*.raw'
CONTENT_DOC_DATA_FILES_ORIGINAL='
Manual.pdf
ReadMe.txt'
CONTENT_DOC0_DATA_FILES_ORIGINAL='
Readme_Patch_1xto12_International.txt
Readme_Patch_10to13_International.txt
ReadMe_Patch_13_to_14_en.txt'
CONTENT_DOC0_DATA_FILES_RENAISSANCE='
ReadMe_Patch_Ren_40_to_415_en.txt
ReadMe_Patch_Ren_40_to_421_en.txt'

USER_PERSISTENT_DIRECTORIES='
Savegames'
USER_PERSISTENT_FILES='
*.ini'

APP_MAIN_EXE='GuildII.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS_ORIGINAL='
video/mpeg, systemstream=(boolean)true, mpegversion=(int)1'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS_RENAISSANCE='
video/x-ms-asf'

PKG_DATA_ID_ORIGINAL="${GAME_ID_ORIGINAL}-data"
PKG_DATA_ID_RENAISSANCE="${GAME_ID_RENAISSANCE}-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

# Enforce paths case.

path_archive_contents=$(path_archive_contents)
path_reference="${path_archive_contents}/Savegames"
path_alternative="${path_archive_contents}/savegames"
if [ -e "$path_alternative" ]; then
	mv "$path_alternative" "$path_reference"
fi

# The Guild 2 Renaissance - Enable movies playback.

case "$(current_archive)" in
	('ARCHIVE_BASE_RENAISSANCE_'*)
		sed --in-place \
			--expression='s/\(^SplashScreen\s*\)= 0/\1= 1/' \
			"$(path_archive_contents 'GAME0_BIN')/config.ini"
	;;
esac

# 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
