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

###
# Mordheim: City of the Damned
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260629.1

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID='mordheim-city-of-the-damned'
GAME_NAME='Mordheim: City of the Damned'

ARCHIVE_BASE_0_NAME='setup_mordheim_city_of_the_damned_1.4.4.4_(64bit)_(36463).exe'
ARCHIVE_BASE_0_MD5='940d8348b78e00c931757ab87616fe70'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_mordheim_city_of_the_damned_1.4.4.4_(64bit)_(36463)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='d4946258c60ad850b36721d038d016c8'
ARCHIVE_BASE_0_PART2_NAME='setup_mordheim_city_of_the_damned_1.4.4.4_(64bit)_(36463)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='fafb1db20fcd987078811b2a6800bd45'
ARCHIVE_BASE_0_SIZE=5436429
ARCHIVE_BASE_0_VERSION='1.4.4.4-1'

UNITY3D_NAME='mordheim'
# TODO: Drop unused libraries.
UNITY3D_PLUGINS='
sqlite3.dll'
# TODO: Are the Steam libraries required?
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
steam_api64.dll
CSteamworks.dll"
# TODO: Is the GOG Galaxy library required?
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll"

CONTENT_PATH_DEFAULT='.'
CONTENT_DOC_DATA_FILES='
release-note.txt'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/Mordheim'

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 'libplayit.sh.2 not found.\n'
	exit 1
fi
. "$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" "$(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" -type f | 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
