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

###
# Chaos Reborn
# send your bug reports to bugs@dotslashplay.it
###

script_version=20250129.1

PLAYIT_COMPATIBILITY_LEVEL='2.32'

GAME_ID='chaos-reborn'
GAME_NAME='Chaos Reborn'

ARCHIVE_BASE_4_NAME='chaos_reborn_en_1_13_3_23004.sh'
ARCHIVE_BASE_4_MD5='283d60b5adf91640679d2fe1c7962300'
ARCHIVE_BASE_4_SIZE='1700000'
ARCHIVE_BASE_4_VERSION='1.13.3-gog23004'
ARCHIVE_BASE_4_URL='https://www.gog.com/game/chaos_reborn'

ARCHIVE_BASE_3_NAME='chaos_reborn_en_1_13_2_17223.sh'
ARCHIVE_BASE_3_MD5='edb60d98710c87c0adea06f55be99567'
ARCHIVE_BASE_3_SIZE='1700000'
ARCHIVE_BASE_3_VERSION='1.13.2-gog17223'

ARCHIVE_BASE_2_NAME='chaos_reborn_en_1_131_14290.sh'
ARCHIVE_BASE_2_MD5='fcfea11ad6a6cbdda2290c4f29bbeb2b'
ARCHIVE_BASE_2_SIZE='1700000'
ARCHIVE_BASE_2_VERSION='1.13.1-gog14290'

ARCHIVE_BASE_1_NAME='gog_chaos_reborn_2.14.0.16.sh'
ARCHIVE_BASE_1_MD5='97dbfc0a679a7fd104c744b6aa46db36'
ARCHIVE_BASE_1_SIZE='1700000'
ARCHIVE_BASE_1_VERSION='1.13-gog2.14.0.16'

ARCHIVE_BASE_0_NAME='gog_chaos_reborn_2.13.0.15.sh'
ARCHIVE_BASE_0_MD5='a2abf12572eea8b43059a9bb8d5d3171'
ARCHIVE_BASE_0_SIZE='1700000'
ARCHIVE_BASE_0_VERSION='1.12.2-gog2.13.0.15'

UNITY3D_NAME='ChaosRebornLinux'
UNITY3D_PLUGINS='
ScreenSelector.so'

CONTENT_PATH_DEFAULT='data/noarch/game'

## Force windowed mode on first launch
APP_MAIN_PRERUN='
# Force windowed mode on first launch
config_file="$HOME/.config/unity3d/Snapshot Games Inc_/Chaos Reborn/prefs"
if [ ! -e "$config_file" ]; then
	mkdir --parents "$(dirname "$config_file")"
	cat > "$config_file" <<- EOF
	<unity_prefs version_major="1" version_minor="1">
	<pref name="Screenmanager Is Fullscreen mode" type="int">0</pref>
	</unity_prefs>
	EOF
fi
'

PACKAGES_LIST='
PKG_BIN64
PKG_BIN32
PKG_DATA'

PKG_BIN64_ARCH='64'
PKG_BIN32_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN64_DEPENDENCIES_SIBLINGS="$PKG_BIN_DEPENDENCIES_SIBLINGS"
PKG_BIN32_DEPENDENCIES_SIBLINGS="$PKG_BIN_DEPENDENCIES_SIBLINGS"
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libgdk_pixbuf-2.0.so.0
libglib-2.0.so.0
libgobject-2.0.so.0
libgtk-x11-2.0.so.0
libm.so.6
libpthread.so.0
librt.so.1
libstdc++.so.6
libz.so.1'
PKG_BIN64_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES"
PKG_BIN32_DEPENDENCIES_LIBRARIES="$PKG_BIN_DEPENDENCIES_LIBRARIES"

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

# Load common functions

PLAYIT_LIB_PATHS="
$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"

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_path in $PLAYIT_LIB_PATHS; do
		if [ -e "${playit_lib_path}/libplayit2.sh" ]; then
			PLAYIT_LIB2="${playit_lib_path}/libplayit2.sh"
			break
		fi
	done
fi
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 the default initialization actions

initialization_default "$@"

# Extract game data

archive_extraction_default

# Include game data

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers

launchers_generation 'PKG_BIN64'
launchers_generation 'PKG_BIN32'

# Build packages

packages_generation
print_instructions

# Clean up

working_directory_cleanup

exit 0
