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

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

script_version=20260321.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='scrapland'
GAME_NAME='Scrapland'

ARCHIVE_BASE_0_NAME='setup_scrapland_remastered_1.6.1_(52159).exe'
ARCHIVE_BASE_0_MD5='5f1a6494309632a2536c46e2bf3226bc'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_scrapland_remastered_1.6.1_(52159)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='93c07bedabd25a0d63c7ae36cf5abd47'
ARCHIVE_BASE_0_PART2_NAME='setup_scrapland_remastered_1.6.1_(52159)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='8543d603b9c8eb6d666aec35387c311c'
ARCHIVE_BASE_0_SIZE='8943631'
ARCHIVE_BASE_0_VERSION='1.6.1-gog52159'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/scrapland_remastered'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
Bin/Scrap.exe
Scrap.cfg'
# TODO: Move server to a dedicated package.
CONTENT_GAME0_BIN_FILES='
Bin/Dedicated.exe
Dedicated'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME1_BIN_FILES='
Bin/fmod.dll
Bin/Galaxy.dll
Bin/steam_api.dll'
CONTENT_GAME_L10N_FR_FILES='
Language/French.txt
French.packed'
CONTENT_GAME_L10N_EN_FILES='
Language/English.txt
English.packed'
CONTENT_VIDEO_L10N_FR_PATH='Video/French'
CONTENT_VIDEO_L10N_EN_PATH='Video'
CONTENT_VIDEO_L10N_FILES='
Bishop.vds
Intro.vds
Mayor.vds
Police.vds
Viscous.vds'
CONTENT_VIDEO_L10N_FR_FILES="$CONTENT_VIDEO_L10N_FILES"
CONTENT_VIDEO_L10N_EN_FILES="$CONTENT_VIDEO_L10N_FILES"
CONTENT_GAME_DATA_FILES='
Video/MSE.vds
Video/MSE-Short.vds
Data.packed
data??.packed'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/MercurySteam/Scrapland'

APP_MAIN_EXE='Bin/Scrap.exe'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_FR
PKG_L10N_EN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/x-msvideo'

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

PKG_L10N_ID="${GAME_ID}-l10n"
PKG_L10N_FR_ID="${PKG_L10N_ID}-fr"
PKG_L10N_EN_ID="${PKG_L10N_ID}-en"
PKG_L10N_PROVIDES="
$PKG_L10N_ID"
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'

# 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
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include language setting file.

language_file_name='goggame-1688988400.info'
language_file_origin="$(path_archive_contents)/${language_file_name}"
language_file_l10n_fr_destination="$(package_path 'PKG_L10N_FR')$(path_game_data)/${language_file_name}"
language_file_l10n_en_destination="$(package_path 'PKG_L10N_EN')$(path_game_data)/${language_file_name}"
mkdir --parents \
	"$(dirname "$language_file_l10n_fr_destination")" \
	"$(dirname "$language_file_l10n_en_destination")"
sed --expression='s/"language": ".*",/"language": "French",/' \
	"$language_file_origin" > "$language_file_l10n_fr_destination"
sed --expression='s/"language": ".*",/"language": "English",/' \
	"$language_file_origin" > "$language_file_l10n_en_destination"

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Include localised videos.
content_inclusion 'VIDEO_L10N_FR' 'PKG_L10N_FR' "$(path_game_data)/Video"
content_inclusion 'VIDEO_L10N_EN' 'PKG_L10N_EN' "$(path_game_data)/Video"
content_inclusion_default

# Write launchers.

# Run the game binary from its parent directory.
game_exec_line() {
	cat <<- 'EOF'
	cd Bin
	$(wine_command) Scrap.exe "$@"
	EOF
}

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		lang_string='version %s :'
		lang_fr='française'
		lang_en='anglaise'
	;;
	('en'|*)
		lang_string='%s version:'
		lang_fr='French'
		lang_en='English'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_L10N_FR'
printf "$lang_string" "$lang_en"
print_instructions 'PKG_BIN' 'PKG_DATA' 'PKG_L10N_EN'

# Clean up.

working_directory_cleanup

exit 0
