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

###
# Songs of Conquest
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260321.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='songs-of-conquest'
GAME_NAME='Songs of Conquest'

ARCHIVE_BASE_14_NAME='setup_songs_of_conquest_1.9.3_344cbd1379_4100_(89512).exe'
ARCHIVE_BASE_14_MD5='cf501d0737830ecdd4f3e3ae54c46306'
ARCHIVE_BASE_14_TYPE='innosetup'
ARCHIVE_BASE_14_PART1_NAME='setup_songs_of_conquest_1.9.3_344cbd1379_4100_(89512)-1.bin'
ARCHIVE_BASE_14_PART1_MD5='a9171a0b2fe4310dc6d2baa663f6c47d'
ARCHIVE_BASE_14_SIZE='2389292'
ARCHIVE_BASE_14_VERSION='1.9.3-gog89512'
ARCHIVE_BASE_14_URL='https://www.gog.com/game/songs_of_conquest'

ARCHIVE_BASE_13_NAME='setup_songs_of_conquest_1.9.2_2bac3c6479_4075_(89372).exe'
ARCHIVE_BASE_13_MD5='2a3900cb2449fea7a6df31e54690d4d6'
ARCHIVE_BASE_13_TYPE='innosetup'
ARCHIVE_BASE_13_PART1_NAME='setup_songs_of_conquest_1.9.2_2bac3c6479_4075_(89372)-1.bin'
ARCHIVE_BASE_13_PART1_MD5='7f0719e3aab8e1acc6b7cb61ce744335'
ARCHIVE_BASE_13_SIZE='2386370'
ARCHIVE_BASE_13_VERSION='1.9.2-gog89372'

ARCHIVE_BASE_12_NAME='setup_songs_of_conquest_1.9.1_a10783a599_4055_(89220).exe'
ARCHIVE_BASE_12_MD5='741a40f26ee08117cde0205585d4b2d1'
ARCHIVE_BASE_12_TYPE='innosetup'
ARCHIVE_BASE_12_PART1_NAME='setup_songs_of_conquest_1.9.1_a10783a599_4055_(89220)-1.bin'
ARCHIVE_BASE_12_PART1_MD5='bb9a833d3affe94b6dea0b0eb30acb0d'
ARCHIVE_BASE_12_SIZE='2386258'
ARCHIVE_BASE_12_VERSION='1.9.1-gog89220'

ARCHIVE_BASE_11_NAME='setup_songs_of_conquest_1.8.2_0fc08df290_3930_(88293).exe'
ARCHIVE_BASE_11_MD5='4752a11bf14af3f9f18eecf205ca14f3'
ARCHIVE_BASE_11_TYPE='innosetup'
ARCHIVE_BASE_11_PART1_NAME='setup_songs_of_conquest_1.8.2_0fc08df290_3930_(88293)-1.bin'
ARCHIVE_BASE_11_PART1_MD5='f406f3da9879f43d084a3c4f5d4c86d1'
ARCHIVE_BASE_11_SIZE='2373102'
ARCHIVE_BASE_11_VERSION='1.8.2-gog88293'

ARCHIVE_BASE_10_NAME='setup_songs_of_conquest_1.8.1_cc22406b0b_3811_(87114).exe'
ARCHIVE_BASE_10_MD5='e6b61caf8d71e88d86b8133d05cce58d'
ARCHIVE_BASE_10_TYPE='innosetup'
ARCHIVE_BASE_10_PART1_NAME='setup_songs_of_conquest_1.8.1_cc22406b0b_3811_(87114)-1.bin'
ARCHIVE_BASE_10_PART1_MD5='5fd5d38f78421801b1d01477e9b12d3a'
ARCHIVE_BASE_10_SIZE='2372835'
ARCHIVE_BASE_10_VERSION='1.8.1-gog87114'

ARCHIVE_BASE_9_NAME='setup_songs_of_conquest_1.7.3_d817094042_3758_(86541).exe'
ARCHIVE_BASE_9_MD5='dc5e2e5605a2cf2fa9b7357196be0dab'
ARCHIVE_BASE_9_TYPE='innosetup'
ARCHIVE_BASE_9_PART1_NAME='setup_songs_of_conquest_1.7.3_d817094042_3758_(86541)-1.bin'
ARCHIVE_BASE_9_PART1_MD5='2d4ed6f931e478c6590ac28e09211e05'
ARCHIVE_BASE_9_SIZE='2357821'
ARCHIVE_BASE_9_VERSION='1.7.3-gog86541'

ARCHIVE_BASE_8_NAME='setup_songs_of_conquest_1.6.1_1c165726cf_3547_(83102).exe'
ARCHIVE_BASE_8_MD5='1d02d310853568e03a6e052960844841'
ARCHIVE_BASE_8_TYPE='innosetup'
ARCHIVE_BASE_8_PART1_NAME='setup_songs_of_conquest_1.6.1_1c165726cf_3547_(83102)-1.bin'
ARCHIVE_BASE_8_PART1_MD5='5788080ea7520a9ede5a89ecd7a0dceb'
ARCHIVE_BASE_8_SIZE='2355359'
ARCHIVE_BASE_8_VERSION='1.6.1-gog83102'
ARCHIVE_BASE_8_URL='https://www.gog.com/game/songs_of_conquest'

ARCHIVE_BASE_7_NAME='setup_songs_of_conquest_1.6.0_52a28cd7d5_3538_(82930).exe'
ARCHIVE_BASE_7_MD5='b54f2148db8e175789d69c890b4e4b02'
ARCHIVE_BASE_7_TYPE='innosetup'
ARCHIVE_BASE_7_PART1_NAME='setup_songs_of_conquest_1.6.0_52a28cd7d5_3538_(82930)-1.bin'
ARCHIVE_BASE_7_PART1_MD5='7a14a09e120d260142e7a519c88bff62'
ARCHIVE_BASE_7_SIZE='2356914'
ARCHIVE_BASE_7_VERSION='1.6.0-gog82930'

ARCHIVE_BASE_6_NAME='setup_songs_of_conquest_1.5.5_4e813b3773_3204_(80582).exe'
ARCHIVE_BASE_6_MD5='d88ee6892bd1a93a275596e8ed5fb028'
ARCHIVE_BASE_6_TYPE='innosetup'
ARCHIVE_BASE_6_PART1_NAME='setup_songs_of_conquest_1.5.5_4e813b3773_3204_(80582)-1.bin'
ARCHIVE_BASE_6_PART1_MD5='9c7470e8bce4972cd4eebcf71da9964a'
ARCHIVE_BASE_6_SIZE='2246884'
ARCHIVE_BASE_6_VERSION='1.5.5-gog80582'

ARCHIVE_BASE_5_NAME='setup_songs_of_conquest_1.4.13_2d9731f566_3063_(79474).exe'
ARCHIVE_BASE_5_MD5='ff47f9178c22a9804a4e513185ce6122'
ARCHIVE_BASE_5_TYPE='innosetup'
ARCHIVE_BASE_5_PART1_NAME='setup_songs_of_conquest_1.4.13_2d9731f566_3063_(79474)-1.bin'
ARCHIVE_BASE_5_PART1_MD5='d080b22508bcafe99f81b63dca1471db'
ARCHIVE_BASE_5_SIZE='2238908'
ARCHIVE_BASE_5_VERSION='1.4.13-gog79474'

ARCHIVE_BASE_4_NAME='setup_songs_of_conquest_1.4.12_a5624b455d_3033_(79217).exe'
ARCHIVE_BASE_4_MD5='d5e6bbfb4a7a6e27696f22f2915b1269'
ARCHIVE_BASE_4_TYPE='innosetup'
ARCHIVE_BASE_4_PART1_NAME='setup_songs_of_conquest_1.4.12_a5624b455d_3033_(79217)-1.bin'
ARCHIVE_BASE_4_PART1_MD5='8d25598cf45e67b54faaa76708e1a642'
ARCHIVE_BASE_4_SIZE='2240483'
ARCHIVE_BASE_4_VERSION='1.4.12-gog79217'

ARCHIVE_BASE_3_NAME='setup_songs_of_conquest_1.4.11_a56e2e9ed1_3013_(78972).exe'
ARCHIVE_BASE_3_MD5='af79080c297f067b66a45625abd50104'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_songs_of_conquest_1.4.11_a56e2e9ed1_3013_(78972)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='7d35955cb78e863fa90db91fb690859b'
ARCHIVE_BASE_3_SIZE='2238284'
ARCHIVE_BASE_3_VERSION='1.4.11-gog78972'

ARCHIVE_BASE_2_NAME='setup_songs_of_conquest_1.4.10_967940f7ee_3002_(78614).exe'
ARCHIVE_BASE_2_MD5='3f7c811f0a316df65446cbce9f294ba6'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_songs_of_conquest_1.4.10_967940f7ee_3002_(78614)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='87bffffbb31fec05c4deba26a40ce42a'
ARCHIVE_BASE_2_SIZE='2239689'
ARCHIVE_BASE_2_VERSION='1.4.10-gog78614'

ARCHIVE_BASE_1_NAME='setup_songs_of_conquest_1.4.8_b1dfeaadfa_2989_(78547).exe'
ARCHIVE_BASE_1_MD5='f3d225f934b63608e707bbd9b9821c78'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_songs_of_conquest_1.4.8_b1dfeaadfa_2989_(78547)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='ecb65717e71b6a8a2f28911512a1f0eb'
ARCHIVE_BASE_1_SIZE='2237801'
ARCHIVE_BASE_1_VERSION='1.4.8-gog78547'

ARCHIVE_BASE_0_NAME='setup_songs_of_conquest_1.3.2_ab33a56fa6_2566_(76712).exe'
ARCHIVE_BASE_0_MD5='ea01d32299258ee757fc349e5d4f9b1e'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_songs_of_conquest_1.3.2_ab33a56fa6_2566_(76712)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='e1a0a815f1f9500d7b5cfdfdf65223f1'
ARCHIVE_BASE_0_SIZE='2544156'
ARCHIVE_BASE_0_VERSION='1.3.2-gog76712'

UNITY3D_NAME='SongsOfConquest'
# TODO: Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# The initial loading fails to complete witout these libraries.
UNITY3D_PLUGINS='
AkUnitySoundEngine.dll
GalaxyCSharpGlue.dll'
# GalaxyCSharpGlue.dll requirement
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll"

CONTENT_PATH_DEFAULT='.'
# songsofconquest.exe requirements
CONTENT_LIBS_BIN_FILES='
UnityPlayer.dll'
# Prevent the following error:
# Failed to load mono
CONTENT_LIBS0_BIN_FILES='
MonoBleedingEdge/EmbedRuntime/mono-2.0-bdwgc.dll'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Lavapotion/SongsOfConquest'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/quicktime, variant=(string)iso'

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'
# Prevent inclusion of unwanted shipped libraries.
content_inclusion_default_libraries 'PKG_BIN'
rm --recursive "$(path_archive_contents)/MonoBleedingEdge/EmbedRuntime"
# 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 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
