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

###
# Synergy
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260315.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='synergy'
GAME_NAME='Synergy'

ARCHIVE_BASE_1_NAME='setup_synergy_2510031634_(64bit)_(85527).exe'
ARCHIVE_BASE_1_MD5='1efae7076534977aac8721a93e462cba'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_SIZE='4109111'
ARCHIVE_BASE_1_VERSION='1.2.2510031634-gog85527'
ARCHIVE_BASE_1_URL='https://www.gog.com/game/synergy'

ARCHIVE_BASE_0_NAME='setup_synergy_2508200455_(64bit)_(84460).exe'
ARCHIVE_BASE_0_MD5='2ec9168f2b0dfe15d020892c43368a03'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='4106911'
ARCHIVE_BASE_0_VERSION='1.2.2508200455-gog84460'

UNITY3D_NAME='Synergy'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# Prevent a loading freeze after the introduction logos.
UNITY3D_PLUGINS='
fmodstudio.dll'

CONTENT_PATH_DEFAULT='.'
# synergy.exe requirements
CONTENT_LIBS_BIN_FILES='
UnityPlayer.dll'
# Prevent a fatal error on launch.
CONTENT_LIBS0_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/mono-2.0-bdwgc.dll'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Leikir Studio/Synergy'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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 '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'
# Include Unity3D plugins.
unity3d_plugins_inclusion 'PKG_BIN' "$(unity3d_name)_Data/Plugins/x86_64" "$(path_libraries)/$(unity3d_name)_Data/Plugins"
# Prevent inclusion of unwanted shipped libraries.
content_inclusion_default_libraries 'PKG_BIN'
rm --recursive "$(path_archive_contents)/MonoBleedingEdge/EmbedRuntime"
content_inclusion_default

# Write launchers.

# Install shipped libraries.
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 "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
