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

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

script_version=20260523.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='noita'
GAME_NAME='Noita'

ARCHIVE_BASE_0_NAME='setup_noita_20231221-_050_(69862).exe'
ARCHIVE_BASE_0_MD5='106a6244f5d69eccfb690e2a5d5e3100'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='1446784'
ARCHIVE_BASE_0_VERSION='2023.12.21-gog69862'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/noita'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
noita.exe
config.xml'
# TODO: Check if this extra binary is required.
CONTENT_GAME0_BIN_FILES='
noita_dev.exe'
# noita.exe requirements
# TODO: Install libraries in a standard path.
CONTENT_GAME1_BIN_FILES='
lua51.dll
SDL2.dll
fmod.dll
fmodstudio.dll
Galaxy.dll'
# TODO: Move the modding tools to a dedicated package (or drop them).
CONTENT_GAME2_BIN_FILES='
tools_modding'
CONTENT_GAME_DATA_FILES='
data'
# TODO: Move the mods to a dedicated package.
CONTENT_GAME0_DATA_FILES='
mods'
CONTENT_DOC_DATA_FILES='
licenses
_branch.txt
_release_notes.txt
_version_hash.txt
readme.txt
screenshot_paths.txt'

USER_PERSISTENT_DIRECTORIES='
mods'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/LocalLow/Nolla_Games_Noita'
# The game fails to start unless Microsoft Visual C++ 2013 libraries are available.
# TODO: Check if this is still a problem with current WINE.
WINE_WINETRICKS_VERBS='vcrun2013'

APP_MAIN_EXE='noita.exe'
APP_MAIN_ICON='data/icon.bmp'
# Prevent errors with SDL backends unsupported on Windows.
APP_MAIN_PRERUN='
# Prevent errors with SDL backends unsupported on Windows.
case "${SDL_VIDEODRIVER:-}" in
	("wayland"|"x11")
		unset SDL_VIDEODRIVER
	;;
esac
'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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

# Convert icon to standard resolutions.

REQUIREMENTS_LIST='
magick'

# 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

# Convert icon to standard resolutions.

APP_MAIN_ICON_64='icon_64.png'
APP_MAIN_ICON_96='icon_96.png'
icon_source="$(path_archive_contents)/$(icon_path 'APP_MAIN_ICON')"
magick convert "$icon_source" -resize 64x64 "$(path_archive_contents)/$(icon_path 'APP_MAIN_ICON_64')"
magick convert "$icon_source" -resize 96x96 "$(path_archive_contents)/$(icon_path 'APP_MAIN_ICON_96')"
APP_MAIN_ICONS_LIST="$(application_icons_list 'APP_MAIN')
APP_MAIN_ICON_64
APP_MAIN_ICON_96"

# 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
