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

###
# Battlefleet Gothic: Armada
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260402.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='battlefleet-gothic-armada-1'
GAME_NAME='Battlefleet Gothic: Armada'

ARCHIVE_BASE_0_NAME='setup_battlefleet_gothic_armada_1.0.12_(64bit)_(41732).exe'
ARCHIVE_BASE_0_MD5='74271753f6cba4385e50c3f8c05b32da'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_battlefleet_gothic_armada_1.0.12_(64bit)_(41732)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='bea83722e900a3253ca68600723e5853'
ARCHIVE_BASE_0_PART2_NAME='setup_battlefleet_gothic_armada_1.0.12_(64bit)_(41732)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='246e012f15d4bcbbac9a024f1b6816a6'
ARCHIVE_BASE_0_SIZE='14000000'
ARCHIVE_BASE_0_VERSION='2.0.26100-gog41732'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/battlefleet_gothic_armada_1'

UNREALENGINE4_NAME='BattleFleetGothic'

CONTENT_PATH_DEFAULT='.'
# TODO: Drop unused libraries.
CONTENT_LIBS_BIN_FILES='
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icudt53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icudtd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuin53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuind53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuio53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuiod53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icule53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/iculed53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/iculx53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/iculxd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icutu53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icutud53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuuc53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2012/icuucd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icudt53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icudtd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuin53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuind53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuio53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuiod53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icule53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/iculed53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/iculx53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/iculxd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icutu53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icutud53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuuc53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2013/icuucd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icudt53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icudtd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuin53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuind53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuio53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuiod53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icule53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/iculed53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/iculx53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/iculxd53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icutu53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icutud53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuuc53.dll
Engine/Binaries/ThirdParty/ICU/icu4c-53_1/Win64/VS2015/icuucd53.dll
Engine/Binaries/ThirdParty/Leap/Win64/Leap.dll
Engine/Binaries/ThirdParty/Ogg/Win64/VS2015/libogg_64.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2012/libeay32.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2012/ssleay32.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2013/libeay32.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2013/ssleay32.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2015/libeay32.dll
Engine/Binaries/ThirdParty/OpenSSL/Win64/VS2015/ssleay32.dll
Engine/Binaries/ThirdParty/OpenVR/OpenVRv000/Win64/openvr_api.dll
Engine/Binaries/ThirdParty/PhysX/PhysX-3.3/Win64/VS2015/glut32.dll
Engine/Binaries/ThirdParty/PhysX/PhysX-3.3/Win64/VS2015/nvToolsExt64_1.dll
Engine/Binaries/ThirdParty/PhysX/PhysX-3.3/Win64/VS2015/PhysX3Common_x64.dll
Engine/Binaries/ThirdParty/PhysX/PhysX-3.3/Win64/VS2015/PhysX3Cooking_x64.dll
Engine/Binaries/ThirdParty/PhysX/PhysX-3.3/Win64/VS2015/PhysX3_x64.dll
Engine/Binaries/ThirdParty/Vorbis/Win64/VS2015/libvorbis_64.dll
Engine/Binaries/ThirdParty/Vorbis/Win64/VS2015/libvorbisfile_64.dll'
# TODO: Check if the Galaxy library is required.
CONTENT_LIBS0_BIN_FILES='
Engine/Source/Runtime/Online/OnlineSubsystemGog/Source/ThirdParty/GalaxySDK/Libraries/Galaxy64.dll'
CONTENT_GAME0_BIN_FILES="
${UNREALENGINE4_NAME}/version.bfgver"

HUGE_FILES_DATA="
${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak"

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'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/x-ms-asf'

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 libraries.
content_inclusion_default_libraries 'PKG_BIN'
rm --recursive \
	"$(path_archive_contents)/Engine/Binaries/ThirdParty" \
	"$(path_archive_contents)/Engine/Source"
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 --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
