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

###
# Rebel Galaxy
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260707.1

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=rebel-galaxy
GAME_NAME='Rebel Galaxy'

ARCHIVE_BASE_0_NAME='setup_rebel_galaxy_1.08(hotifx2)_(23097).exe'
ARCHIVE_BASE_0_MD5=9746494be23b83bc2a44d8da6cb6e311
ARCHIVE_BASE_0_TYPE=innosetup
ARCHIVE_BASE_0_PART1_NAME='setup_rebel_galaxy_1.08(hotifx2)_(23097)-1.bin'
ARCHIVE_BASE_0_PART1_MD5=e8e5d4450b5ad8f9cc757cc4153ba13c
ARCHIVE_BASE_0_SIZE=2465913
ARCHIVE_BASE_0_VERSION=1.08-1

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_MAIN_FILES='
MEDIA
music
PAKS
video
OGRE.CFG
plugins.cfg
plugins_dx11.cfg
plugins_gl.cfg
GoGLauncher.exe
RebelGalaxyGOG.exe'
# RebelGalaxyGoG.exe requirements
CONTENT_LIBS_MAIN_FILES='
Galaxy.dll
OgreOverlay.dll
fmodex.dll
OgreMain.dll'
# These 3 renderers are available through the settings application.
CONTENT_LIBS0_MAIN_FILES='
RenderSystem_Direct3D9.dll
RenderSystem_Direct3D11.dll
RenderSystem_GL.dll'
# The following Ogre plugins are required by all 3 renderers.
CONTENT_LIBS1_MAIN_FILES='
Plugin_OctreeSceneManager.dll
Plugin_OgreBink.dll'
# Plugin_OgreBink.dll requirements
CONTENT_LIBS2_MAIN_FILES='
binkw32.dll'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/Double Damage Games/RebelGalaxy'
# Microsoft Visual C++ 2012 Runtime is required by the settings window.
# TODO: Check if it still required with current WINE.
WINE_WINETRICKS_VERBS='vcrun2012'

APP_MAIN_EXE='RebelGalaxyGOG.exe'
# Set the OpenGL renderer by default.
APP_MAIN_PRERUN='
# Set the OpenGL renderer by default.
settings_file="${WINEPREFIX}/drive_c/users/${USER}/Documents/My Games/Double Damage Games/RebelGalaxy/local_settings.txt"
if [ ! -e "$settings_file" ]; then
	install -D --mode=644 "${PATH_GAME_DATA}/local_settings.txt" "$settings_file"
fi
'

APP_SETTINGS_ID=${GAME_ID}-settings
APP_SETTINGS_NAME="${GAME_NAME} - Settings"
APP_SETTINGS_CAT='Settings'
APP_SETTINGS_EXE='GoGLauncher.exe'
# Set the OpenGL renderer by default.
APP_SETTINGS_PRERUN=$APP_MAIN_PRERUN

PKG_MAIN_ARCH=32
# Easier upgrades from pre-20250530.2 packages
PKG_MAIN_PROVIDES='
rebel-galaxy-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
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons
content_inclusion_default

# Set the OpenGL renderer by default.

cat > "$(package_path PKG_MAIN)$(path_game_data)/local_settings.txt" << EOF
[SETTINGS]
	<INTEGER>OPENGL:1
	<INTEGER>DX9:0
	<INTEGER>DX11:0
[/SETTINGS]
EOF

# 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" -type f | while read -r dll_source; do
		dll_destination=${PATH_PREFIX}${dll_source#${PATH_LIBRARIES_SYSTEM}}
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
