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

###
# Reus 2
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260314.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='reus-2'
GAME_NAME='Reus 2'

ARCHIVE_BASE_6_NAME='setup_reus_2_1.11.4_(89195).exe'
ARCHIVE_BASE_6_MD5='1219d6f44042e7cd2cc810ed21dc4c46'
ARCHIVE_BASE_6_TYPE='innosetup'
ARCHIVE_BASE_6_SIZE='4396034'
ARCHIVE_BASE_6_VERSION='1.11.4-gog89195'
ARCHIVE_BASE_6_URL='https://www.gog.com/game/reus_2'

ARCHIVE_BASE_5_NAME='setup_reus_2_1.11.2_(88356).exe'
ARCHIVE_BASE_5_MD5='dd2f1d6b7c814928d16d7236723716bb'
ARCHIVE_BASE_5_TYPE='innosetup'
ARCHIVE_BASE_5_SIZE='4396802'
ARCHIVE_BASE_5_VERSION='1.11.2-gog88356'

ARCHIVE_BASE_4_NAME='setup_reus_2_1.10.3_(86943).exe'
ARCHIVE_BASE_4_MD5='eb2a7f2cf69f4679a1bc1a628907c6a7'
ARCHIVE_BASE_4_TYPE='innosetup'
ARCHIVE_BASE_4_SIZE='3966786'
ARCHIVE_BASE_4_VERSION='1.10.3-gog86943'

ARCHIVE_BASE_3_NAME='setup_reus_2_1.10.0_(86675).exe'
ARCHIVE_BASE_3_MD5='f36e26a1c47a8a6c6ff7b7423c772bf0'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_SIZE='4064054'
ARCHIVE_BASE_3_VERSION='1.10.0-gog86675'

ARCHIVE_BASE_2_NAME='setup_reus_2_1.8.3_(83845).exe'
ARCHIVE_BASE_2_MD5='5384f9e5fdf5375219adf7f8b8643567'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_SIZE='3999785'
ARCHIVE_BASE_2_VERSION='1.8.3-gog83845'

ARCHIVE_BASE_1_NAME='setup_reus_2_1.8.0_(83653).exe'
ARCHIVE_BASE_1_MD5='2ced0663287c4540efaffac336d42b88'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_SIZE='3858778'
ARCHIVE_BASE_1_VERSION='1.8.0-gog83653'

ARCHIVE_BASE_0_NAME='setup_reus_2_1.6.3_(80797).exe'
ARCHIVE_BASE_0_MD5='7d8a3aa3d7f7457cf8ef4fba979e3a7e'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='3234044'
ARCHIVE_BASE_0_VERSION='1.6.3-gog80797'

UNITY3D_NAME='Reus 2'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# Required to prevent a freeze on launch
UNITY3D_PLUGINS='
GalaxyCSharpGlue.dll
fmodstudio.dll'
# GalaxyCSharpGlue.dll requirement
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll"

CONTENT_PATH_DEFAULT='.'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Abbey Games/Reus 2'

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"
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 "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
