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

###
# Werewolf: The Apocalypse - Forest Stories series:
# - Werewolf: The Apocalypse - Heart of the Forest
# - Werewolf: The Apocalypse - Purgatory
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260303.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID_HEART='werewolf-the-apocalypse-heart-of-the-forest'
GAME_NAME_HEART='Werewolf: The Apocalypse - Heart of the Forest'

GAME_ID_PURGATORY='werewolf-the-apocalypse-purgatory'
GAME_NAME_PURGATORY='Werewolf: The Apocalypse - Purgatory'

ARCHIVE_BASE_HEART_0_NAME='werewolf_the_apocalypse_heart_of_the_forest_1_0_13_2010191537_42086.sh'
ARCHIVE_BASE_HEART_0_MD5='8f8655786c9eed74746c9cc151b5543b'
ARCHIVE_BASE_HEART_0_SIZE='902811'
ARCHIVE_BASE_HEART_0_VERSION='1.0.13-gog42086'
ARCHIVE_BASE_HEART_0_URL='https://www.gog.com/game/werewolf_the_apocalypse_heart_of_the_forest'

ARCHIVE_BASE_PURGATORY_1_NAME='purgatory_1_1_13_2504011817_81091.sh'
ARCHIVE_BASE_PURGATORY_1_MD5='012f8c571e7649b50b56e2a59542d1d1'
ARCHIVE_BASE_PURGATORY_1_SIZE='1524084'
ARCHIVE_BASE_PURGATORY_1_VERSION='1.1.13-gog81091'
ARCHIVE_BASE_PURGATORY_1_URL='https://www.gog.com/game/werewolf_the_apocalypse_purgatory'

ARCHIVE_BASE_PURGATORY_0_NAME='purgatory_1_0_13_2406131256_74723.sh'
ARCHIVE_BASE_PURGATORY_0_MD5='eac63ba42adc2614e66e0cd733c019fb'
ARCHIVE_BASE_PURGATORY_0_SIZE='1069791'
ARCHIVE_BASE_PURGATORY_0_VERSION='1.0.13-gog74723'

UNITY3D_NAME_HEART='Heart of the Forest'
UNITY3D_NAME_PURGATORY='Purgatory'
UNITY3D_PLUGINS_HEART='
libfmodL.so
libfmod.so
libfmodstudioL.so
libfmodstudio.so
libgvraudio.so
libresonanceaudio.so'
UNITY3D_PLUGINS_PURGATORY='
libfmodstudio.so
libresonanceaudio.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_HEART='
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libpthread.so.0
libresolv.so.2
librt.so.1
libstdc++.so.6
libz.so.1'
PKG_BIN_DEPENDENCIES_LIBRARIES_PURGATORY='
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'

PKG_DATA_ID_HEART="${GAME_ID_HEART}-data"
PKG_DATA_ID_PURGATORY="${GAME_ID_PURGATORY}-data"
PKG_DATA_DESCRIPTION='data'

# Load common functions.

# Scan library paths used by ./play.it ≥ 2.35.
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
# Scan library paths used by ./play.it < 2.35.
# This can go away when PLAYIT_COMPATIBILITY_LEVEL is bumped to 2.35 or higher.
if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_dir in \
		"$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'
	do
		playit_lib_path="${playit_lib_dir}/libplayit2.sh"
		if [ -e "$playit_lib_path" ]; then
			PLAYIT_LIB2="$playit_lib_path"
			break
		fi
	done
fi
# Error out if the library can not be found.
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"

# 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.

# Heart of the Forest - Shipped libraries expect an executable stack.
case "$(current_archive)" in
	('ARCHIVE_BASE_HEART_'*)
		game_exec_line() {
			cat <<- 'EOF'
			GLIBC_TUNABLES=glibc.rtld.execstack=2 \
			    "./Heart of the Forest" -logFile "./logs/$(date +%F-%R).log" "$@"
			EOF
		}
	;;
esac

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
