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

###
# Factory Town
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260405.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='factory-town'
GAME_NAME='Factory Town'

ARCHIVE_BASE_2_NAME='setup_factory_town_2.2.10a_(64bit)_(85555).exe'
ARCHIVE_BASE_2_MD5='8d20e04c2a5484aa609c053fb7abc656'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_SIZE='599323'
ARCHIVE_BASE_2_VERSION='2.2.10-gog85555'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/factory_town'

ARCHIVE_BASE_1_NAME='setup_factory_town_2.2.7_(64bit)_(77941).exe'
ARCHIVE_BASE_1_MD5='79cfa439d7468916310f9293dbfc6c6c'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_SIZE='598856'
ARCHIVE_BASE_1_VERSION='2.2.7-gog77941'

ARCHIVE_BASE_0_NAME='setup_factory_town_1.13.3_(64bit)_(53563).exe'
ARCHIVE_BASE_0_MD5='4b9199687bd79a1d8f7641bf4577a2dd'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='630000'
ARCHIVE_BASE_0_VERSION='1.13.3-gog53563'

UNITY3D_NAME='Factory Town'
# Experimental support for Windows Unity3D plugins,
# no support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Drop unused libraries.
UNITY3D_PLUGINS='
InControlNative.dll
XInputInterface64.dll'
# TODO: Check if the Galaxy libraries are required.
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
Galaxy64.dll
GalaxyCSharpGlue.dll"

CONTENT_PATH_DEFAULT='.'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/82 Apps/Factory Town'
# Work around missing text.
# (WINE 11.6)
WINE_WINETRICKS_VERBS='arial'

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

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
