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

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

script_version=20260508.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='ikenfell'
GAME_NAME='Ikenfell'

ARCHIVE_BASE_0_NAME='setup_ikenfell_1.0.3_(64bit)_(52030).exe'
ARCHIVE_BASE_0_MD5='edf00ffea67cb1c5007cf0dbaba618c4'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='439559'
ARCHIVE_BASE_0_VERSION='1.0.3-gog52030'

CONTENT_PATH_DEFAULT='.'
# The shipped Windows binary must be used through WINE, as calling it with native Mono triggers the following crash:
#
# System.DllNotFoundException: SDL2.dll assembly:<unknown assembly> type:<unknown type> member:(null)
#   at (wrapper managed-to-native) SDL2.SDL.Quit()
#   at GameEngine.Engine.Dispose (System.Boolean disposing) [0x00007] in <57ba3b07a71049a79fc9069a6c41013f>:0
#   at GameEngine.Engine.Finalize () [0x00002] in <57ba3b07a71049a79fc9069a6c41013f>:0
#
# The shipped MacOS binary (Contents/Resources/Ikenfell.exe) does not help, as it triggers a similar error:
#
# System.DllNotFoundException: SDL2.dll assembly:<unknown assembly> type:<unknown type> member:(null)
#   at (wrapper managed-to-native) SDL2.SDL.Init(SDL2.SubSystem)
#   at GameEngine.Engine..ctor (System.String title, System.Int32 width, System.Int32 height, System.Int32 windowScale, System.Single frameRate) [0x000e1] in <6ba33594429645fdacac96f34b3bb342>:0
#   at LittleWitch.Game..ctor () [0x00019] in <4c786faff1da4a04a5c2a4665d1d19d0>:0
#   at IkenfellMac.Program.Main (System.String[] args) [0x00007] in <79ffe5bf77cf4578a7600534a6d3593d>:0
#
# (Mono 6.14.1)
CONTENT_GAME_BIN_FILES='
IkenfellWin.exe'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME0_BIN_FILES='
fmod.dll
fmodstudio.dll
SDL2.dll'
CONTENT_GAME_DATA_FILES='
Atlas
Audio
Data
Scripts'
# The following .dll files are Mono libraries.
# TODO: Most of the following libraries could be replaced by system-provided ones.
CONTENT_GAME0_DATA_RELATIVE_PATH='Contents/Resources'
CONTENT_GAME0_DATA_FILES='
GameEngine.dll
LittleWitch.dll
Mono.Posix.dll
Mono.Security.dll
MoonSharp.Interpreter.dll
mscorlib.dll
System.Configuration.dll
System.Core.dll
System.Data.dll
System.dll
System.Drawing.dll
System.Numerics.dll
System.Runtime.Serialization.dll
System.Security.dll
System.Xml.dll
System.Xml.Linq.dll'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Roaming/ikenfell'
WINE_WINEPREFIX_TWEAKS='mono'

APP_MAIN_EXE='IkenfellWin.exe'
APP_MAIN_TYPE='wine'
# Prevent errors with SDL backends unsupported on Windows.
APP_MAIN_PRERUN='
# Prevent errors with SDL backends unsupported on Windows.
case "${SDL_AUDIODRIVER:-}" in
	("alsa")
		unset SDL_AUDIODRIVER
	;;
esac
case "${SDL_VIDEODRIVER:-}" in
	("wayland"|"x11")
		unset SDL_VIDEODRIVER
	;;
esac
'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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'
content_inclusion_default

# Write launchers.

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
