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

###
# Frostpunk
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260323.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='frostpunk'
GAME_NAME='Frostpunk'

ARCHIVE_BASE_2_NAME='setup_frostpunk_1.6.1_51852_59618_(51757).exe'
ARCHIVE_BASE_2_MD5='6d1f0d7c81821c949c5a7eb9c7f26133'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_frostpunk_1.6.1_51852_59618_(51757)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='43f6adbfdc53299d4591a2abb1c84457'
ARCHIVE_BASE_2_PART2_NAME='setup_frostpunk_1.6.1_51852_59618_(51757)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='6689f6a289b899ddcd69af920f4ecdc4'
ARCHIVE_BASE_2_PART3_NAME='setup_frostpunk_1.6.1_51852_59618_(51757)-3.bin'
ARCHIVE_BASE_2_PART3_MD5='a72c123ddeef82cdebeed57792b02e4c'
ARCHIVE_BASE_2_SIZE='9593582'
ARCHIVE_BASE_2_VERSION='1.6.1-gog51757'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/frostpunk'

ARCHIVE_BASE_1_NAME='setup_frostpunk_1.5.0.51146.56648_(2020-02-14_17-22)_(36204).exe'
ARCHIVE_BASE_1_MD5='cc9bd3aba061dacd4f83e79e6a13d4e8'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_PART1_NAME='setup_frostpunk_1.5.0.51146.56648_(2020-02-14_17-22)_(36204)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='ba31654b9f3b1d24b22df3999e5ff78e'
ARCHIVE_BASE_1_PART2_NAME='setup_frostpunk_1.5.0.51146.56648_(2020-02-14_17-22)_(36204)-2.bin'
ARCHIVE_BASE_1_PART2_MD5='fc560cb127691e6fdcd9e1a82f0e7b26'
ARCHIVE_BASE_1_PART3_NAME='setup_frostpunk_1.5.0.51146.56648_(2020-02-14_17-22)_(36204)-3.bin'
ARCHIVE_BASE_1_PART3_MD5='b82e3dc7bd981c8a30b8da51813acbf6'
ARCHIVE_BASE_1_SIZE='8700000'
ARCHIVE_BASE_1_VERSION='1.5.0-gog36204'

ARCHIVE_BASE_0_NAME='setup_frostpunk_1.4.1.50110.53938_(2019-11-05_1825)_(33713).exe'
ARCHIVE_BASE_0_MD5='99b71af138d5fdcb67418392b0a14d62'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_frostpunk_1.4.1.50110.53938_(2019-11-05_1825)_(33713)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='3fbe880a3c3acaf00ede07c998b13c92'
ARCHIVE_BASE_0_PART2_NAME='setup_frostpunk_1.4.1.50110.53938_(2019-11-05_1825)_(33713)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='86267bb5dc870db828c50ccaa45d6091'
ARCHIVE_BASE_0_SIZE='6500000'
ARCHIVE_BASE_0_VERSION='1.4.1-gog33713'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
Frostpunk.exe
GfxConfig.ini'
# Frostpunk.exe requirements
CONTENT_LIBS_BIN_FILES='
AnselSDK64.dll
Galaxy64.dll
libcurl.dll'
# libcurl.dll requirements
CONTENT_LIBS0_BIN_FILES='
LIBEAY32.dll
SSLEAY32.dll
libssh2.dll'
CONTENT_GAME_DATA_FILES='
animations.dat
animations.idx
archives.dat
archives.idx
common.dat
common.idx
localizations.dat
localizations.idx
scenes.dat
scenes.idx
sequences.dat
sequences.idx
sounds.dat
sounds.idx
templates.dat
templates.idx
videos.dat
videos.idx'
# The game crashes on launch if these .idx files are missing.
CONTENT_GAME0_DATA_FILES='
custom_localizations.idx
voices.idx'
CONTENT_GAME_DATA_TEXTURES_FILES='
textures-s3.dat
textures-s3.idx'
CONTENT_DOC_DATA_FILES='
openfontlicense.txt'

# With wined3d the game menu is rendered with glitches, and the actual gameplay is too dark.
# - https://bugs.winehq.org/show_bug.cgi?id=45098
# - https://bugs.winehq.org/show_bug.cgi?id=45364
# (WINE 11.5)
WINE_DIRECT3D_RENDERER='dxvk'
WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Roaming/11bitstudios/Frostpunk'

USER_PERSISTENT_FILES='
GfxConfig.ini'

APP_MAIN_EXE='Frostpunk.exe'
# The game crashes on launch with some SDL settings.
APP_MAIN_PRERUN='
# The game crashes on launch with some SDL settings
if [ "${SDL_VIDEODRIVER:-}" = "wayland" ]; then
	unset SDL_VIDEODRIVER
fi
if [ "${SDL_AUDIODRIVER:-}" = "alsa" ]; then
	unset SDL_AUDIODRIVER
fi
'

PACKAGES_LIST='
PKG_BIN
PKG_DATA
PKG_DATA_TEXTURES'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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

PKG_DATA_TEXTURES_ID="${PKG_DATA_ID}-textures"
PKG_DATA_TEXTURES_DESCRIPTION="$PKG_DATA_DESCRIPTION - textures"
# Easier upgrades from pre-20251111.2 packages
PKG_DATA_TEXTURES_PROVIDES='
frostpunk-textures'

# 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

# Create some empty files, the game crashes on launch if they are missing.

touch \
	"$(package_path 'PKG_DATA')$(path_game_data)/custom_localizations.dat" \
	"$(package_path 'PKG_DATA')$(path_game_data)/voices.dat"

# Write launchers.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	ln --force --symbolic $(path_libraries)/* "\${PATH_PREFIX}"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
