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

###
# Endzone: A World Apart
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260331.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='endzone-1'
GAME_NAME='Endzone: A World Apart'

ARCHIVE_BASE_FR_0_NAME='setup_endzone_-_a_world_apart_1.2.8630.30586_(french_64bit)_(66949).exe'
ARCHIVE_BASE_FR_0_MD5='003ad80a6a2ce4123ffe2d98c5fe0de5'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_PART1_NAME='setup_endzone_-_a_world_apart_1.2.8630.30586_(french_64bit)_(66949)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5='5f68efd024f84f71edad63e2122f7bf3'
ARCHIVE_BASE_FR_0_SIZE='4244580'
ARCHIVE_BASE_FR_0_VERSION='1.2.8630.30586-gog66949'
ARCHIVE_BASE_FR_0_URL='https://www.gog.com/game/endzone_a_world_apart'

ARCHIVE_BASE_EN_0_NAME='setup_endzone_-_a_world_apart_1.2.8630.30586_(64bit)_(66949).exe'
ARCHIVE_BASE_EN_0_MD5='01a625f5c0e07d324fb07d8ac43fdd14'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_PART1_NAME='setup_endzone_-_a_world_apart_1.2.8630.30586_(64bit)_(66949)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5='de1aba989b4798154fce6e4fa2ffbf46'
ARCHIVE_BASE_EN_0_SIZE='4242544'
ARCHIVE_BASE_EN_0_VERSION='1.2.8630.30586-gog66949'
ARCHIVE_BASE_EN_0_URL='https://www.gog.com/game/endzone_a_world_apart'

UNITY3D_NAME='Endzone'
# Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Check if the Galaxy libraries are required.
UNITY3D_PLUGINS='
Galaxy64.dll
GalaxyCSharpGlue.dll'
# TODO: Check if the Steam library is required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
steam_api64.dll"

CONTENT_PATH_DEFAULT='.'
# TODO: Move voice-overs, always in English, to a common package.
CONTENT_GAME_L10N_FILES='
Localizations'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Gentlymad Studios/Endzone'

PACKAGES_LIST='
PKG_BIN
PKG_L10N
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'

PKG_L10N_BASE_ID="${GAME_ID}-l10n"
PKG_L10N_ID_FR="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_ID_EN="${PKG_L10N_BASE_ID}-en"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_DESCRIPTION_FR='French localisation'
PKG_L10N_DESCRIPTION_EN='English localisation'

PKG_DATA_ID="${GAME_ID}-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 '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

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Include Unity3D plugins.
unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins/x86_64" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
content_inclusion_default

# Write launchers.

# Install Unity3D plugins.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
