#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: © 2023 Christian Stalp
set -o errexit

###
# Pillars of Eternity 2
###

script_version=20260120.1

## FIXME: PLAYIT_COMPATIBILITY_LEVEL actually requires a compatibility level ≥ 2.26.
PLAYIT_COMPATIBILITY_LEVEL='2.22'

APP_MAIN_TYPE='native'

GAME_ID='pillars-of-eternity-2' 
GAME_NAME='Pillars of Eternity 2' 

## FIXME: This should be replaced with ARCHIVE_xxx_NAME,
##        requiring a compatibility level ≥ 2.26.
ARCHIVE_BASE_0='pillars_of_eternity_ii_deadfire_v5_0_0_0040_29222.sh'
ARCHIVE_BASE_0_MD5='61987ed80717b2a96b296dc8af6a9f50'
ARCHIVE_BASE_0_EXTRACTOR='unzip'
ARCHIVE_BASE_0_SIZE='45000000'
ARCHIVE_BASE_0_VERSION='5.0.0.0040-gog29222'
ARCHIVE_BASE_0_URL='https://www.gog.com/de/game/pillars_of_eternity_2_game'

UNITY3D_NAME='PillarsOfEternityII'

CONTENT_PATH_DEFAULT='data/noarch/game'

CONTENT_GAME_BIN_FILES="
${UNITY3D_NAME}
#${UNITY3D_NAME}.x86_64
${UNITY3D_NAME}_Data/Mono/x86_64
${UNITY3D_NAME}_Data/Plugins/x86_64"

CONTENT_GAME_STREAMTILE_FILES="
${UNITY3D_NAME}_Data/assetbundles/streamtile"

CONTENT_GAME_ASSETBUNDLES_FILES="
${UNITY3D_NAME}_Data/assetbundles"

CONTENT_GAME_STREAMING_ASSETS_FILES="
${UNITY3D_NAME}_Data/StreamingAssets"

CONTENT_GAME_DATA_FILES="
${UNITY3D_NAME}_Data"

APP_MAIN_EXE="$UNITY3D_NAME"

PACKAGES_LIST='PKG_BIN PKG_STREAMTILE PKG_ASSETBUNDLES PKG_STREAMING_ASSETS PKG_DATA'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'

PKG_STREAMTILE_ID="${PKG_DATA_ID}-streamtile"
PKG_STREAMTILE_DESCRIPTION='some data files'

PKG_ASSETBUNDLES_ID="${PKG_DATA_ID}-assetbundles"
PKG_ASSETBUNDLES_DESCRIPTION='some more data file'

PKG_STREAMING_ASSETS_ID="${PKG_DATA_ID}-streaming-assets"
PKG_STREAMING_ASSETS_DESCRIPTION='even more data files'



PKG_BIN_ARCH='64'
PKG_BIN_DEPS="$PKG_DATA_ID"
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'


# Load common functions

PLAYIT_LIB_PATHS="
$PWD
${XDG_DATA_HOME:="${HOME}/.local/share"}/play.it
/usr/local/share/games/play.it
/usr/local/share/play.it
/usr/share/games/play.it
/usr/share/play.it"

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_path in $PLAYIT_LIB_PATHS; do
		if [ -e "${playit_lib_path}/libplayit2.sh" ]; then
			PLAYIT_LIB2="${playit_lib_path}/libplayit2.sh"
			break
		fi
	done
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit2.sh not found.\n'
	exit 1
fi
# shellcheck source=libplayit2.sh
. "$PLAYIT_LIB2"

# Extract game data

## Data extraction using unzip will end in a failure state even if nothing went wrong.
set +o errexit
## FIXME: The archive identifier "SOURCE_ARCHIVE" should no longer be used from game scripts.
##        The following can be replaced with a call to archive_extraction_default,
##        requiring a compatibility level ≥ 2.26.
archive_extraction 'SOURCE_ARCHIVE' 2>/dev/null
set -o errexit
(
	cd "${PLAYIT_WORKDIR}/gamedata/$(content_path_default)"

	# Set minimal permissions on all game files
	set_standard_permissions .
)

# Include game icons

## FIXME: This should be replaced with a call to content_inclusion_icons,
##        requiring a compatibility level ≥ 2.28.
PKG='PKG_DATA'
# shellcheck disable=SC2119
icons_inclusion

# Include game data

content_inclusion_default

# Delete temporary files

rm --recursive "${PLAYIT_WORKDIR}/gamedata"

# Write launchers

## FIXME: This should be replaced with a call to launchers_generation,
##        requiring a compatibility level ≥ 2.28.
PKG='PKG_BIN'
# shellcheck disable=SC2119
launchers_write

# Build packages

## FIXME: Both following functions calls should be replaced with a single call to packages_generation,
##        requiring a compatibility level ≥ 2.24.
write_metadata
build_pkg

# Print instructions

print_instructions

# Clean up

rm --recursive "$PLAYIT_WORKDIR"

exit 0
