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

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

script_version=20260116.4

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='stellaris'
GAME_NAME='Stellaris'

ARCHIVE_BASE_4_NAME='stellaris_4_2_4_87179.sh'
ARCHIVE_BASE_4_MD5='d92a7d056bd05fb8312a5dbe7e7aa117'
ARCHIVE_BASE_4_SIZE='26630528'
ARCHIVE_BASE_4_VERSION='4.2.4-gog87179'
ARCHIVE_BASE_4_URL='https://www.gog.com/game/stellaris'

ARCHIVE_BASE_3_NAME='stellaris_4_1_7_86266.sh'
ARCHIVE_BASE_3_MD5='173d5ab87955eaac9d504a95ecfa3966'
ARCHIVE_BASE_3_SIZE='25274344'
ARCHIVE_BASE_3_VERSION='4.1.7-gog86266'

ARCHIVE_BASE_2_NAME='stellaris_4_0_23_84679.sh'
ARCHIVE_BASE_2_MD5='72012fb6ce1ecd9c729e3ceb04fbba2a'
ARCHIVE_BASE_2_SIZE='22711133'
ARCHIVE_BASE_2_VERSION='4.0.23-gog84679'

ARCHIVE_BASE_1_NAME='stellaris_3_14_1592653_81105.sh'
ARCHIVE_BASE_1_MD5='1cc50dc7470ed4ddd58fc755e9cf3024'
ARCHIVE_BASE_1_SIZE='20346386'
ARCHIVE_BASE_1_VERSION='3.14.1592653-gog81105'

ARCHIVE_BASE_0_NAME='stellaris_2_8_1_2_42827.sh'
ARCHIVE_BASE_0_MD5='8278463a7b3a9b6b7f9c5ede4b51b222'
ARCHIVE_BASE_0_SIZE='11000000'
ARCHIVE_BASE_0_VERSION='2.8.1.2-gog42827'

CONTENT_PATH_DEFAULT='data/noarch/game'
CONTENT_LIBS_BIN_FILES='
libnakama-cpp.so
libpops_api.so'
CONTENT_FONTS_DATA_RELATIVE_PATH='gfx/fonts'
CONTENT_FONTS_DATA_FILES='
Jura-VariableFont_wght.ttf
malgun.ttf
NotoSans-Regular.ttf
ONE_Mobile_Title.ttf
Orbitron-Regular.ttf
rounded-mgenplus-1c-medium.ttf'
CONTENT_FONTS0_DATA_RELATIVE_PATH='gfx/fonts/arimo'
CONTENT_FONTS0_DATA_FILES='
Arimo-BoldItalic.ttf
Arimo-Bold.ttf
Arimo-Italic.ttf
Arimo-Regular.ttf'
CONTENT_FONTS1_DATA_RELATIVE_PATH='previewer_assets/gfx/pdx_gui'
CONTENT_FONTS1_DATA_FILES='
previewer_arial.ttf'
CONTENT_GAME_BIN_FILES='
crash_reporter
tools
stellaris'
CONTENT_GAME_DATA_FILES='
common
curated_save_games
dlc
dlc_metadata
events
flags
fonts
gfx
interface
licenses
locales
localisation
localisation_synced
map
music
prescripted_countries
previewer_assets
tweakergui_assets
unchecked_defines
ChangeLog.txt
ChangeLogBlank.txt
checksum_manifest.txt
ThirdPartyLicenses.txt'
CONTENT_GAME_DATA_SOUND_FILES='
sound'
CONTENT_GAME_DATA_MODELS_FILES='
gfx/models'
CONTENT_GAME_DATA_MODELS_SHIPS_FILES='
gfx/models/ships'
CONTENT_GAME_DATA_MODELS_SHIPS_MEGASTRUCTURES_FILES='
gfx/models/ships/megastructures'
CONTENT_GAME_MULTIPLAYER_FILES='
launcher-assets
livepp
pdx_launcher
pdx_online_assets
dowser
launcher-installer-linux_2024.14
pdx_core_test
launcher-settings.json'

## Symbolic links are explicitly unsupported by the game engine.
## Trying to run the game from a symlinks farm prefix leads to the following errors:
##
## [virtualfilesystem_physfs.cpp:797] File 'gfx/exe_icon.bmp' does not exist : symlinks are forbidden
## [virtualfilesystem_physfs.cpp:797] File 'gfx/loadingscreens/init.bmp' does not exist : symlinks are forbidden
APP_MAIN_PREFIX_TYPE='none'
APP_MAIN_EXE='stellaris'
APP_MAIN_ICON='gfx/exe_icon.bmp'

PACKAGES_LIST='
PKG_BIN
PKG_DATA_SOUND
PKG_DATA_MODELS_SHIPS_MEGASTRUCTURES
PKG_DATA_MODELS_SHIPS
PKG_DATA_MODELS
PKG_DATA
PKG_MULTIPLAYER'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libGL.so.1
libm.so.6
libpthread.so.0
libresolv.so.2
libstdc++.so.6
libuuid.so.1
libX11.so.6
libz.so.1'

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

PKG_DATA_SOUND_ID="${PKG_DATA_ID}-sound"
PKG_DATA_SOUND_DESCRIPTION="${PKG_DATA_DESCRIPTION} - sound"

PKG_DATA_MODELS_ID="${PKG_DATA_ID}-models"
PKG_DATA_MODELS_DESCRIPTION="${PKG_DATA_DESCRIPTION} - models"
PKG_DATA_MODELS_DEPENDENCIES_SIBLINGS='
PKG_DATA_MODELS_SHIPS'

PKG_DATA_MODELS_SHIPS_ID="${PKG_DATA_MODELS_ID}-ships"
PKG_DATA_MODELS_SHIPS_DESCRIPTION="${PKG_DATA_MODELS_DESCRIPTION} - ships"
PKG_DATA_MODELS_SHIPS_DEPENDENCIES_SIBLINGS='
PKG_DATA_MODELS_SHIPS_MEGASTRUCTURES'

PKG_DATA_MODELS_SHIPS_MEGASTRUCTURES_ID="${PKG_DATA_MODELS_SHIPS_ID}-megastructures"
PKG_DATA_MODELS_SHIPS_MEGASTRUCTURES_DESCRIPTION="${PKG_DATA_MODELS_SHIPS_DESCRIPTION} - megastructures"

PKG_MULTIPLAYER_ID="${GAME_ID}-multiplayer"
PKG_MULTIPLAYER_DESCRIPTION='Paradox launcher'
PKG_MULTIPLAYER_ARCH='64'
PKG_MULTIPLAYER_DEPENDENCIES_SIBLINGS='
PKG_BIN'
PKG_MULTIPLAYER_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libpthread.so.0
libstdc++.so.6
libX11.so.6'

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

## Force the use of system-provided SDL.
APP_MAIN_PRERUN="$(application_prerun 'APP_MAIN')
$(launcher_tweak_sdl_override)"
PKG_BIN_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES
libSDL2-2.0.so.0"
## Prevent a crash with SDL3 when music should be played.
## cf. https://github.com/libsdl-org/sdl2-compat/issues/548
APP_MAIN_PRERUN="$(application_prerun 'APP_MAIN')
# Prevent a crash with SDL3 when music should be played.
# cf. https://github.com/libsdl-org/sdl2-compat/issues/548
export SDL2COMPAT_BROKEN_LOADWAV_SIZE=1
"

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
