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

###
# Unreal Tournament 2004
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260220.6

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='unreal-tournament-2004'
GAME_NAME='Unreal Tournament 2004'

# This CD-ROM image does not include a CD key, one must still be provided.
# Create a file ~/.local/share/games/unreal-tournament-2004/System/CDKey and write a valid CD key in it,
# or the game will fail to start with the following error:
# Your CD key appears to be invalid.  You will need to reinstall the game and enter your CD key to fix this problem.
ARCHIVE_BASE_OLDUNREAL_0_NAME='UT2004.ISO'
ARCHIVE_BASE_OLDUNREAL_0_MD5='4ad34b16d757e0752809eb9bf5fb1fba'
ARCHIVE_BASE_OLDUNREAL_0_TYPE='iso'
ARCHIVE_BASE_OLDUNREAL_0_SIZE='8993007'
ARCHIVE_BASE_OLDUNREAL_0_VERSION='1.0-oldunreal1'
ARCHIVE_BASE_OLDUNREAL_0_URL='https://files.oldunreal.net/UT2004.ISO'

# This game is no longer available for sale from GOG.com since 2022-12-23.
# This installer includes a valid CD key so there is no need to provide one manually.
# It can still be done following the instructions above to override the default GOG.com CD key with your own.
ARCHIVE_BASE_GOG_0_NAME='setup_unreal_tournament_2004_1.0_(18947).exe'
ARCHIVE_BASE_GOG_0_MD5='243376d34413b830324c5879ac2f9cfd'
# Do not convert file paths to lowercase.
ARCHIVE_BASE_GOG_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_GOG_0_EXTRACTOR_OPTIONS=' '
ARCHIVE_BASE_GOG_0_PART1_NAME='setup_unreal_tournament_2004_1.0_(18947)-1.bin'
ARCHIVE_BASE_GOG_0_PART1_MD5='a211e2a6feed7334bb3b7deef6b858b5'
ARCHIVE_BASE_GOG_0_SIZE='5961826'
ARCHIVE_BASE_GOG_0_VERSION='1.0-gog18947'

ARCHIVE_REQUIRED_ENGINE_0_NAME='ut2004-lnxpatch3369-2.tar.bz2'
ARCHIVE_REQUIRED_ENGINE_0_MD5='0fa447e05fe5a38e0e32adf171be405e'
ARCHIVE_REQUIRED_ENGINE_0_SIZE='76580'
ARCHIVE_REQUIRED_ENGINE_0_VERSION='3369.2-1'
ARCHIVE_REQUIRED_ENGINE_0_URL='http://downloads.dotslashplay.it/games/unreal-tournament-2004/'

ARCHIVE_REQUIRED_CPP5_NAME='libstdc++5.tar.xz'
ARCHIVE_REQUIRED_CPP5_MD5='5c0ed9260d1b75778ef5f0785e4be223'
ARCHIVE_REQUIRED_CPP5_SIZE='829'
ARCHIVE_REQUIRED_CPP5_URL='http://downloads.dotslashplay.it/resources/gcc/'

CONTENT_PATH_DEFAULT='.'
CONTENT_LIBS_BIN_FILES='
libstdc++.so.5
libstdc++.so.5.0.7'
CONTENT_GAME_BIN_FILES='
System/*.u
System/*.upl
System/*.int
System/*.det
System/*.est
System/*.frt
System/*.itt
System/*.kot'
CONTENT_GAME0_BIN_PATH='UT2004-Patch'
CONTENT_GAME0_BIN_FILES='
System/ut2004-bin-linux-amd64
System/Packages.md5
System/*.u
System/*.ucl
System/*.int
System/*.det
System/*.est
System/*.frt
System/*.itt
System/*.kot'
CONTENT_GAME1_BIN_PATH_GOG='__support/app'
CONTENT_GAME1_BIN_FILES='
System/Default.ini
System/DefUser.ini'
CONTENT_GAME_DATA_FILES='
Animations
ForceFeedback
KarmaData
Maps
Music
Sounds
Speech
StaticMeshes
Textures
Web'
CONTENT_GAME0_DATA_PATH='UT2004-Patch'
CONTENT_GAME0_DATA_FILES='
Animations
Speech
Textures
Web'
CONTENT_DOC0_DATA_RELATIVE_PATH='Help'
CONTENT_DOC0_DATA_FILES='
*.txt'
CONTENT_DOC1_DATA_RELATIVE_PATH='Manual'
CONTENT_DOC1_DATA_FILES='
*.pdf'
CONTENT_DOC2_DATA_PATH='UT2004-Patch/Help'
CONTENT_DOC2_DATA_FILES='
*.txt'

APP_MAIN_EXE='System/ut2004-bin-linux-amd64'
APP_MAIN_ICON='Help/Unreal.ico'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libm.so.6
libopenal.so.1
libpthread.so.0
libSDL-1.2.so.0'

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

REQUIREMENTS_LIST_OLDUNREAL='
unshield'

# 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 "$@"

# Chek for the presence of the required game engine.

archive_initialize_required \
	'ARCHIVE_ENGINE' \
	'ARCHIVE_REQUIRED_ENGINE_0'
archive_initialize_required \
	'ARCHIVE_CPP5' \
	'ARCHIVE_REQUIRED_CPP5'
archive_version=$(archive_version 'ARCHIVE_ENGINE' | cut --delimiter='-' --fields=1)
archive_release=$(archive_version "$(current_archive)" | cut --delimiter='-' --fields=2)
export "$(current_archive)_VERSION=${archive_version}-${archive_release}"

# Extract game data.

archive_extraction_default
# OldUnreal ISO - Extract files from inner archives.
case "$(current_archive)" in
	('ARCHIVE_BASE_OLDUNREAL_'*)
		path_archive_contents="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)"
		ln --symbolic --target-directory="$path_archive_contents" \
			"${path_archive_contents}/Disk1/data1.hdr" \
			"${path_archive_contents}/Disk1/data1.cab" \
			"${path_archive_contents}/Disk1/data2.cab" \
			"${path_archive_contents}/Disk2/data3.cab" \
			"${path_archive_contents}/Disk3/data4.cab" \
			"${path_archive_contents}/Disk4/data5.cab" \
			"${path_archive_contents}/Disk5/data6.cab"
		ARCHIVE_INSTALLSHIELD_PATH="${path_archive_contents}/data1.hdr"
		ARCHIVE_INSTALLSHIELD_EXTRACTOR='unshield'
		ARCHIVE_INSTALLSHIELD_EXTRACTOR_OPTIONS=' '
		archive_extraction 'ARCHIVE_INSTALLSHIELD'
		rm \
			"${path_archive_contents}/Disk1/data1.hdr" \
			"${path_archive_contents}/Disk1/data1.cab" \
			"${path_archive_contents}/Disk1/data2.cab" \
			"${path_archive_contents}/Disk2/data3.cab" \
			"${path_archive_contents}/Disk3/data4.cab" \
			"${path_archive_contents}/Disk4/data5.cab" \
			"${path_archive_contents}/Disk5/data6.cab"
		(
			cd "$path_archive_contents"
			mv All_Animations                             Animations
			mv All_ForceFeedback                          ForceFeedback
			mv All_Help                                   Help
			mv English_Sounds_Speech_System_Help/Help/*   Help/
			mv All_KarmaData                              KarmaData
			mv English_Manual                             Manual
			mv All_Maps                                   Maps
			mv All_Music                                  Music
			mv English_Sounds_Speech_System_Help/Sounds   Sounds
			mv English_Sounds_Speech_System_Help/Speech   Speech
			mv All_StaticMeshes                           StaticMeshes
			mv All_UT2004.EXE                             System
			mv English_Sounds_Speech_System_Help/System/* System/
			mv All_Textures                               Textures
			mv All_Web                                    Web
		)
	;;
esac
archive_extraction 'ARCHIVE_ENGINE'
archive_extraction 'ARCHIVE_CPP5'

# Include game data.

# GOG.com installer - Include required CD key.
case "$(current_archive)" in
	('ARCHIVE_BASE_GOG_'*)
		source_file="${PLAYIT_WORKDIR}/gamedata/$(content_path_default)/goggame-1207658691.script"
		destination_directory="$(package_path 'PKG_BIN')$(path_game_data)/System"
		mkdir --parents "$destination_directory"
		grep --before=1 --fixed-strings 'CDKey' "$source_file" |
			sed --silent 's/\s*"valueData": "\(.*\)",/\1/p' > "${destination_directory}/CDKey"
	;;
esac

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Create required links for some libraries.

path_libraries_source=$(path_libraries_system)
path_libraries_destination="$(package_path 'PKG_BIN')$(path_game_data)/System"
ln --symbolic "${path_libraries_source}/libSDL-1.2.so.0" "${path_libraries_destination}/libSDL-1.2.so.0"
ln --symbolic "${path_libraries_source}/libopenal.so.1" "${path_libraries_destination}/openal.so"

# Write launchers.

# Run the game binary from its parent directory.
game_exec_line() {
	cat <<- 'EOF'
	# Run the game binary from its parent directory.
	cd System
	./ut2004-bin-linux-amd64 "$@"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
