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

###
# Pillars of Eternity 2
###

script_version=20260710.1

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=pillars-of-eternity-2
GAME_NAME='Pillars of Eternity II: Deadfire'

ARCHIVE_BASE_0_NAME='pillars_of_eternity_ii_deadfire_v5_0_0_0040_29222.sh'
ARCHIVE_BASE_0_MD5=61987ed80717b2a96b296dc8af6a9f50
ARCHIVE_BASE_0_SIZE=43213628
ARCHIVE_BASE_0_VERSION=5.0.0.0040-1

UNITY3D_NAME='PillarsOfEternityII'
UNITY3D_PLUGINS='
libAkFlanger.so
libAkHarmonizer.so
libAkPitchShifter.so
libAkSoundEngine.so
libAkStereoDelay.so
libAkTremolo.so
libMcDSP.so
ScreenSelector.so'
# The game menu fails to load if the Discord library is missing:
# DllNotFoundException: discord-rpc
#   at (wrapper managed-to-native) DiscordRpc:UpdatePresence (DiscordRpc/RichPresence&)
#   at Game.RichPresenceManager.RefreshPresence () [0x00000] in <filename unknown>:0
#   at Game.RichPresenceManager.StaticRefreshPresence () [0x00000] in <filename unknown>:0
#   at Game.UI.UIMainMenuManager.OnyxAwake () [0x00000] in <filename unknown>:0
#   at Onyx.OnyxBehavior.Awake () [0x00000] in <filename unknown>:0
UNITY3D_PLUGINS="$UNITY3D_PLUGINS
libdiscord-rpc.so"

CONTENT_PATH_DEFAULT='data/noarch/game'

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
libpthread.so.0
librt.so.1
libstdc++.so.6
libz.so.1'
# libAk*.so dependencies
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libc.so.6
libgcc_s.so.1
libm.so.6
libSDL2-2.0.so.0
libstdc++.so.6"
# libMcDSP.so dependencies
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libc.so.6
libm.so.6
libstdc++.so.6"
# ScreenSelector.so dependencies
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libc.so.6
libdl.so.2
libgdk_pixbuf-2.0.so.0
libglib-2.0.so.0
libgobject-2.0.so.0
libgtk-x11-2.0.so.0
libstdc++.so.6"
# libdiscord-rpc.so dependencies
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libc.so.6
libgcc_s.so.1
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

# Include game data.

content_inclusion_icons PKG_DATA
content_inclusion_default

# Write launchers.

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
