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

###
# Grim Dawn
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260321.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='grim-dawn'
GAME_NAME='Grim Dawn'

ARCHIVE_BASE_2_NAME='setup_grim_dawn_1.2.1.6_(64bit)_(83443).exe'
ARCHIVE_BASE_2_MD5='21763dfc1938de2acac61415dd701e3d'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_grim_dawn_1.2.1.6_(64bit)_(83443)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='494304bc1238d6825124ec84dd5b36d1'
ARCHIVE_BASE_2_PART2_NAME='setup_grim_dawn_1.2.1.6_(64bit)_(83443)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='089131ce2805e00a1059d28fb1e81b2a'
ARCHIVE_BASE_2_SIZE='4829597'
ARCHIVE_BASE_2_VERSION='1.2.1.6-gog83443'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/grim_dawn'

ARCHIVE_BASE_1_NAME='setup_grim_dawn_1.2.0.5a_(64bit)_(71558).exe'
ARCHIVE_BASE_1_MD5='f232c25a9c69a5776dd4ce65b40df6df'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_grim_dawn_1.2.0.5a_(64bit)_(71558)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='c78a59aa61b124030e6d96cabaaabd2d'
ARCHIVE_BASE_1_SIZE='4768460'
ARCHIVE_BASE_1_VERSION='1.2.0.5a-gog71558'

ARCHIVE_BASE_0_NAME='setup_grim_dawn_1.1.9.8_(64bit)_(65199).exe'
ARCHIVE_BASE_0_MD5='c0fb173132b21833052d1496f6ab832e'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_grim_dawn_1.1.9.8_(64bit)_(65199)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='d97de2b8cd7ae7aa723e720ec2fa21f5'
ARCHIVE_BASE_0_SIZE='4332472'
ARCHIVE_BASE_0_VERSION='1.1.9.8-gog65199'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
x64/Grim Dawn.exe'
# Grim Dawn.exe requirements
CONTENT_LIBS_BIN_FILES='
x64/StatReport.dll
x64/CrashReport.dll
x64/lua51.dll
x64/Galaxy64.dll
x64/Engine.dll
x64/Game.dll'
# Engine.dll requirements
CONTENT_LIBS0_BIN_FILES='
x64/FreeImage.dll
x64/zlibwapi.dll'
# The game is stuck to a black screen on launch if any of the following libraries is missing.
CONTENT_LIBS1_BIN_FILES='
x64/Direct3D.dll
x64/DirectInput.dll'
# The game is stuck to a black screen right after showing the game cursor if the following library is missing.
CONTENT_LIBS2_BIN_FILES='
x64/DS8Sound.dll'
# ds8sound.dll requirements
CONTENT_LIBS3_BIN_FILES='
x64/libvorbisfile.dll'
# libvorbisfile.dll requirements
CONTENT_LIBS4_BIN_FILES='
x64/libogg.dll
x64/libvorbis.dll'
# TODO: Check if this binary is required.
CONTENT_GAME0_BIN_FILES='
x64/crashreporter.exe'
CONTENT_GAME_DATA_FILES='
database
resources
video
minimap.atn
GDMapStitch_x64_v2.zip'
# TODO: Modding stuff should be moved to a dedicated package.
CONTENT_GAME0_DATA_FILES='
ModdingTutorial.zip'
CONTENT_DOC_DATA_FILES='
Grim Dawn Modding Guide.pdf'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/Grim Dawn'

APP_MAIN_EXE='x64/Grim Dawn.exe'

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.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
