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

###
# Aven Colony
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260404.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='aven-colony'
GAME_NAME='Aven Colony'

ARCHIVE_BASE_0_NAME='setup_aven_colony_1.0.1_(64bit)_(25959).exe'
ARCHIVE_BASE_0_MD5='959a44c1bb18e8c3342b10d396ae8a53'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_aven_colony_1.0.1_(64bit)_(25959)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='4f24fb8c38d99916a623c46aafc4eeeb'
ARCHIVE_BASE_0_PART2_NAME='setup_aven_colony_1.0.1_(64bit)_(25959)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='db96cef3e5ce8e2f160b703d6d81777b'
ARCHIVE_BASE_0_PART3_NAME='setup_aven_colony_1.0.1_(64bit)_(25959)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='0ab31c5b95332e6ab160f55c5fbe0db3'
ARCHIVE_BASE_0_PART4_NAME='setup_aven_colony_1.0.1_(64bit)_(25959)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='2537b2db5c65a3b1ded96e938a864c52'
ARCHIVE_BASE_0_SIZE='15000000'
ARCHIVE_BASE_0_VERSION='1.0.25697-gog25959'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/aven_colony'

UNREALENGINE4_NAME='AvenColony'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_DATA_TILESETS_1_FILES="
${UNREALENGINE4_NAME}/Content/GraniteSDK/ImportedTileSets/city-*"
CONTENT_GAME_DATA_TILESETS_2_FILES="
${UNREALENGINE4_NAME}/Content/GraniteSDK/ImportedTileSets"
# TODO: Drop unused libraries.
CONTENT_LIBS_BIN_FILES='
Engine/Binaries/ThirdParty/GraniteSDK/Win64/release/grCore.dll
Engine/Binaries/ThirdParty/GraniteSDK/Win64/release/grGranite.dll
Engine/Binaries/ThirdParty/GraniteSDK/Win64/release/grGraniteDX11.dll
Engine/Binaries/ThirdParty/GraniteSDK/Win64/release/grGraniteDX12.dll
Engine/Binaries/ThirdParty/GraniteSDK/Win64/release/grGraniteQuartzFast.dll
Engine/Binaries/ThirdParty/NVIDIA/NVaftermath/Win64/GFSDK_Aftermath_Lib.dll
Engine/Binaries/ThirdParty/Ogg/Win64/VS2015/libogg_64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/ApexFramework_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/APEX_Clothing_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/APEX_Destructible_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/APEX_Legacy_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/NvCloth_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/PhysX3Common_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/PhysX3Cooking_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/PhysX3_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/PxFoundation_x64.dll
Engine/Binaries/ThirdParty/PhysX/Win64/VS2015/PxPvdSDK_x64.dll
Engine/Binaries/ThirdParty/Vorbis/Win64/VS2015/libvorbisfile_64.dll
Engine/Binaries/ThirdParty/Vorbis/Win64/VS2015/libvorbis_64.dll
Engine/Binaries/ThirdParty/Windows/DirectX/x64/WinPixEventRuntime.dll'
CONTENT_LIBS0_BIN_FILES='
Engine/Binaries/Win64/AWSSDK.Core.dll
Engine/Binaries/Win64/AWSSDK.S3.dll
Engine/Binaries/Win64/BugSplatRc.dll'
CONTENT_LIBS1_BIN_FILES='
Engine/Plugins/Runtime/Nvidia/Ansel/Binaries/ThirdParty/AnselSDK64.dll'

APP_MAIN_EXE="${UNREALENGINE4_NAME}/Binaries/Win64/${UNREALENGINE4_NAME}-Win64-Shipping.exe"
APP_MAIN_ICON_WRESTOOL_OPTIONS='--type=14 --name=123'

PACKAGES_LIST='
PKG_BIN
PKG_DATA_TILESETS_1
PKG_DATA_TILESETS_2
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_TILESETS_1
PKG_DATA_TILESETS_2'

PKG_DATA_TILESETS_ID="${PKG_DATA_ID}-tilesets"
PKG_DATA_TILESETS_1_ID="${PKG_DATA_TILESETS_ID}-1"
PKG_DATA_TILESETS_2_ID="${PKG_DATA_TILESETS_ID}-2"
PKG_DATA_TILESETS_DESCRIPTION="$PKG_DATA_DESCRIPTION - tilesets"
PKG_DATA_TILESETS_1_DESCRIPTION="$PKG_TILESETS_DESCRIPTION - 1"
PKG_DATA_TILESETS_2_DESCRIPTION="$PKG_TILESETS_DESCRIPTION - 2"

# 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 libraries.
content_inclusion_default_libraries 'PKG_BIN'
rm --recursive \
	"$(path_archive_contents)/Engine/Binaries" \
	"$(path_archive_contents)/Engine/Plugins"
content_inclusion_default

# Write launchers.

# Installing Visual C++ 2019 libraries can cause problems with this game.
unrealengine4_wine_winetricks_verbs_default() { return 0; }

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

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
