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

###
# Wingspan
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260407.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='wingspan'
GAME_NAME='Wingspan'

ARCHIVE_BASE_4_NAME='setup_wingspan_295_(88309).exe'
ARCHIVE_BASE_4_MD5='d6bea4bc4a4a1e70c690674e49df7785'
ARCHIVE_BASE_4_TYPE='innosetup'
ARCHIVE_BASE_4_PART1_NAME='setup_wingspan_295_(88309)-1.bin'
ARCHIVE_BASE_4_PART1_MD5='3ecda902f350f65bf5f0c154c1d5127e'
ARCHIVE_BASE_4_SIZE='2209005'
ARCHIVE_BASE_4_VERSION='295-gog88309'
ARCHIVE_BASE_4_URL='https://www.gog.com/game/wingspan'

ARCHIVE_BASE_3_NAME='setup_wingspan_245_(79908).exe'
ARCHIVE_BASE_3_MD5='7d25219f5ea206a5b07075da912f4eeb'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_wingspan_245_(79908)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='77a3548ce92b64f2528cb04707ce0002'
ARCHIVE_BASE_3_SIZE='1819577'
ARCHIVE_BASE_3_VERSION='245-gog79908'

ARCHIVE_BASE_2_NAME='setup_wingspan_205_(73380).exe'
ARCHIVE_BASE_2_MD5='11f9487bfac4640f9c6b60ba571b9d3f'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_wingspan_205_(73380)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='d45eed5ada273495ec6686283cf2cb33'
ARCHIVE_BASE_2_SIZE='1713414'
ARCHIVE_BASE_2_VERSION='205-gog73380'

ARCHIVE_BASE_1_NAME='setup_wingspan_102_(53871).exe'
ARCHIVE_BASE_1_MD5='1628a30ffccd86d875895e2d166c1cde'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_wingspan_102_(53871)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='7a2baede2444e183941698a73e7a3044'
ARCHIVE_BASE_1_SIZE='1100000'
ARCHIVE_BASE_1_VERSION='102-gog53871'

ARCHIVE_BASE_0_NAME='setup_wingspan_83_(50869).exe'
ARCHIVE_BASE_0_MD5='5d68e6a8027e34504e86075a96e8bc15'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_wingspan_83_(50869)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='110175d9b0bca8b33b13e826da0a7b99'
ARCHIVE_BASE_0_SIZE='1100000'
ARCHIVE_BASE_0_VERSION='83-gog50869'

UNITY3D_NAME='Wingspan'
# Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Drop unused libraries.
UNITY3D_PLUGINS='
Rewired_DirectInput.dll
Rewired_WindowsGamingInput.dll
sqlite3.dll'
# TODO: Check if the Galaxy libraries are required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy.dll
GalaxyCSharpGlue.dll"
# TODO: Check if the Steam library is required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
steam_api.dll"

CONTENT_PATH_DEFAULT='.'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Monster Couch/Wingspan'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
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 '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" "$(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
