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

###
# Everspace
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260708.1

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=everspace
GAME_NAME='Everspace'

ARCHIVE_BASE_3_NAME='everspace_1_3_5_3655_32896.sh'
ARCHIVE_BASE_3_MD5=55c9230b3805c54d13371f940860d4a9
ARCHIVE_BASE_3_SIZE=10280299
ARCHIVE_BASE_3_VERSION=1.3.5-1

ARCHIVE_BASE_2_NAME='everspace_1_3_4_29339.sh'
ARCHIVE_BASE_2_MD5=2010b839534fb5a265eea6116b9193ae
ARCHIVE_BASE_2_SIZE=11000000
ARCHIVE_BASE_2_VERSION=1.3.4-1

ARCHIVE_BASE_1_NAME='everspace_1_3_3_25886.sh'
ARCHIVE_BASE_1_MD5=df8f210059a515ef738f247bfcd61bb2
ARCHIVE_BASE_1_SIZE=11000000
ARCHIVE_BASE_1_VERSION=1.3.3-1

ARCHIVE_BASE_0_NAME='everspace_en_1_3_2_3_22978.sh'
ARCHIVE_BASE_0_MD5=4290b47c1396f140198f45a74bf53abf
ARCHIVE_BASE_0_SIZE=11000000
ARCHIVE_BASE_0_VERSION=1.3.2.3-1

UNREALENGINE4_NAME='RSG'

CONTENT_PATH_DEFAULT='data/noarch/game'
# RSG-Linux-Shipping requirement
CONTENT_LIBS_BIN_RELATIVE_PATH='Engine/Binaries/ThirdParty/OpenVR/OpenVRv1_0_7/linux64'
CONTENT_LIBS_BIN_FILES='
libopenvr_api.so'

# TODO: Why not a regular symlinks prefix?
APPLICATIONS_PREFIX_TYPE=none

APP_MAIN_EXE="${UNREALENGINE4_NAME}/Binaries/Linux/${UNREALENGINE4_NAME}-Linux-Shipping"
APP_MAIN_ICON='../support/icon.png'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH=64
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libopenal.so.1
libpthread.so.0
librt.so.1'
# libopenvr_api.so dependencies
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libc.so.6
libdl.so.2
libm.so.6
libpthread.so.0
libstdc++.so.6"

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

# Drop shipped VLC libraries.

rm --recursive "$(path_archive_contents)/$(unrealengine4_name)/Plugins"

# Integrate with system-provided VLC libraries.

libraries_source=$(path_libraries_system)
libraries_destination=$(package_path PKG_BIN)$(path_game_data)/$(unrealengine4_name)/Plugins/VlcMedia/ThirdParty/vlc/Linux/x86_64-unknown-linux-gnu
mkdir --parents "$libraries_destination"
ln --symbolic "${libraries_source}/libvlc.so.5" "${libraries_destination}/libvlc.so"
ln --symbolic "${libraries_source}/libvlccore.so.9" "${libraries_destination}/libvlccore.so"
PKG_BIN_DEPENDENCIES_LIBRARIES="$(dependencies_libraries_list PKG_BIN)
libvlc.so.5
libvlccore.so.9"
# This Pulseaudio library is a shorthand for VLC base plugins.
PKG_BIN_DEPENDENCIES_LIBRARIES="$(dependencies_libraries_list PKG_BIN)
libvlc_pulse.so"

# Include game data.

content_inclusion_icons
# Prevent inclusion of unwanted libraries.
content_inclusion_default_libraries PKG_BIN
rm --recursive "$(path_archive_contents)/Engine"
content_inclusion_default

# Write launchers.

# Prevent a segmentation fault when using the wayland backend of SDL.
# Using the system-provided SDL library is not an option, as it leads to a segmentation fault too.
# TODO: Paste a copy of the segfault trace here.
APP_MAIN_PRERUN="$(application_prerun APP_MAIN)
$(launcher_tweak_sdl_force_x11)"

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
