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

###
# Jazz Jackrabbit 2
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260224.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='jazz-jackrabbit-2'
GAME_NAME='Jazz Jackrabbit 2'

ARCHIVE_BASE_2_NAME='setup_jazz_jackrabbit_2_1.24hf_(16886).exe'
ARCHIVE_BASE_2_MD5='
25a730c0813eb006555e6bbaf9613487
45be80bad040ea821bc6096abe6f3196
48a48258ed60b24068cbbb2f110b049b'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_SIZE='66354'
ARCHIVE_BASE_2_VERSION='1.24-gog16886'
ARCHIVE_BASE_2_URL='https://www.gog.com/game/jazz_jackrabbit_2_collection'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='app'
CONTENT_SOURCE_FILES='
html
jcshelp
tiles
userlevels
*.asc
*.it
*.j2?
*.j2as
*.mo3
*.mod
*.mut
*.pal
*.s3m
*.wav'
CONTENT_DOC_MAIN_FILES='
license.txt
readme.txt'

APPLICATIONS_PREFIX_TYPE='none'

APP_MAIN_TYPE='custom'
APP_MAIN_ICON='jazz2.exe'

PKG_MAIN_DEPENDENCIES_COMMANDS='
jazz2'
# Easier upgrades from pre-20250812.1 packages
PKG_MAIN_PROVIDES="${PKG_MAIN_PROVIDES:-}
jazz-jackrabbit-2-bin
jazz-jackrabbit-2-data"

# 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

# Prevent inclusion of a file that would break The Christmas Chronicles expansion.
rm "${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/xmas1.j2l"

# Include game data.

content_inclusion_icons
option_package=$(option_value 'package')
case "$option_package" in
	('deb')
		PATH_JAZZ2='/usr/share/games/jazz2-native'
	;;
	('gentoo')
		PATH_JAZZ2='/usr/share/jazz2'
	;;
	(*)
		PATH_JAZZ2='/usr/share/Jazz² Resurrection'
	;;
esac
content_inclusion 'SOURCE' 'PKG_MAIN' "${PATH_JAZZ2}/Source"
content_inclusion_default

# Write launchers.

custom_launcher() {
	launcher_headers
	cat <<- 'EOF'
	jazz2 "$@"
	EOF
	launcher_exit
}
launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
