#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2016 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2018 Mopi
# SPDX-FileCopyrightText: © 2020 Fabien Givors <captnfab@debian-facile.org>
set -o errexit

###
# Riven: The Sequel to Myst
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260224.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='riven'
GAME_NAME='Riven: The Sequel to Myst'

ARCHIVE_BASE_5_NAME='setup_riven_(1997)_2.0.5_(64bit)_(83079).exe'
ARCHIVE_BASE_5_MD5='0c348854e43bdf6636521c1531bb8864'
ARCHIVE_BASE_5_TYPE='innosetup'
ARCHIVE_BASE_5_PART1_NAME='setup_riven_(1997)_2.0.5_(64bit)_(83079)-1.bin'
ARCHIVE_BASE_5_PART1_MD5='1462cd4eaaccc833f640c10ddc3fc31e'
ARCHIVE_BASE_5_PART2_NAME='setup_riven_(1997)_2.0.5_(64bit)_(83079)-2.bin'
ARCHIVE_BASE_5_PART2_MD5='3fb3e2125a77c78d3bf76bd3b28f8684'
ARCHIVE_BASE_5_SIZE='7402416'
ARCHIVE_BASE_5_VERSION='2.0.5-gog83079'
ARCHIVE_BASE_5_URL='https://www.gog.com/game/riven_the_sequel_to_myst'

ARCHIVE_BASE_4_NAME='setup_riven_(1997)_2.0.3_(64bit)_(82361).exe'
ARCHIVE_BASE_4_MD5='4cc4222f47dd95879c022a00b88a3265'
ARCHIVE_BASE_4_TYPE='innosetup'
ARCHIVE_BASE_4_PART1_NAME='setup_riven_(1997)_2.0.3_(64bit)_(82361)-1.bin'
ARCHIVE_BASE_4_PART1_MD5='6d2287790cbbf147a37501be07f9d503'
ARCHIVE_BASE_4_PART2_NAME='setup_riven_(1997)_2.0.3_(64bit)_(82361)-2.bin'
ARCHIVE_BASE_4_PART2_MD5='0fb47e21468e9664a7d47b3b81200342'
ARCHIVE_BASE_4_SIZE='7401427'
ARCHIVE_BASE_4_VERSION='2.0.3-gog82361'

ARCHIVE_BASE_3_NAME='setup_riven_(1997)_2.0.2_(64bit)_(82312).exe'
ARCHIVE_BASE_3_MD5='22a6bb23b3ca1ae25fabc0e85c293519'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_PART1_NAME='setup_riven_(1997)_2.0.2_(64bit)_(82312)-1.bin'
ARCHIVE_BASE_3_PART1_MD5='f04c236767db0f5fe4166afe01a7e811'
ARCHIVE_BASE_3_PART2_NAME='setup_riven_(1997)_2.0.2_(64bit)_(82312)-2.bin'
ARCHIVE_BASE_3_PART2_MD5='22ec68ce3002b0aaf7f637846aad975f'
ARCHIVE_BASE_3_SIZE='7399203'
ARCHIVE_BASE_3_VERSION='2.0.2-gog82312'

ARCHIVE_BASE_2_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_no_launcher_(55114).exe'
ARCHIVE_BASE_2_MD5='
e3bb9372a059a7284b0dc02f39aba929
879b8aaa724f4f422661634186ee2534'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_PART1_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_no_launcher_(55114)-1.bin'
ARCHIVE_BASE_2_PART1_MD5='
99921a49f1834af00e5f6dda7227d456
dd91f1256d819d6f26d9d0bcaa81548f'
ARCHIVE_BASE_2_PART2_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_no_launcher_(55114)-2.bin'
ARCHIVE_BASE_2_PART2_MD5='
2db5912fc46865a8992cb32417674fca
2d03d82b784666472e1a3f436ed20924'
ARCHIVE_BASE_2_SIZE='6946550'
ARCHIVE_BASE_2_VERSION='1.2-gog55114'

ARCHIVE_BASE_0_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_update3_(22594).exe'
ARCHIVE_BASE_0_MD5='0520fac1f2ae966aa5c490850b51930f'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_update3_(22594)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='4ee339ad50203020e717a78ff95bb590'
ARCHIVE_BASE_0_PART2_NAME='setup_riven_-_the_sequel_to_myst_1.2_svm_update3_(22594)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='2ff2d7d25f2e8c2995c17d8b122d672c'
ARCHIVE_BASE_0_SIZE='6954413'
ARCHIVE_BASE_0_VERSION='1.2-gog22594'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_L10N_FR_FILES='
b_data_french.mhk
j_data_french.mhk
o_data_french.mhk
p_data_french.mhk
r_data_french.mhk
t_data_french.mhk'
CONTENT_FONTS_MAIN_FILES='
freesans.ttf
mplus-2c-regular.ttf'
CONTENT_GAME_MAIN_FILES='
riven.exe
a_data.mhk
b_data.mhk
b2_data.mhk
g_data.mhk
j_data1.mhk
j_data2.mhk
o_data.mhk
p_data.mhk
r_data.mhk
t_data1.mhk
t_data2.mhk
a_sounds.mhk
b_sounds.mhk
g_sounds.mhk
j_sounds.mhk
o_sounds.mhk
p_sounds.mhk
r_sounds.mhk
t_sounds.mhk
extras.mhk'
## The following localisation files must be included,
## or ScummVM fails to detect the correct game version.
CONTENT_GAME0_MAIN_FILES='
a_data_french.mhk
a_data_german.mhk
a_data_italian.mhk
a_data_japanese.mhk
a_data_polish.mhk
a_data_russian.mhk
a_data_spanish.mhk'
CONTENT_DOC_MAIN_FILES='
manual.pdf
readme.txt'

APP_MAIN_SCUMMID='mohawk:riven'
APP_MAIN_ICON='riven.exe'
APP_MAIN_ICON_WRESTOOL_OPTIONS='--type=14 --name=APPICON'

PACKAGES_LIST='
PKG_L10N_FR
PKG_L10N_EN
PKG_MAIN'

PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N'
## Easier upgrades from pre-20260401.2 packages
PKG_MAIN_PROVIDES="${PKG_MAIN_PROVIDES:-}
myst-2-riven-data
myst-2-riven-common"

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'
## Easier upgrades from pre-20260401.2 packages
PKG_L10N_PROVIDES="${PKG_L10N_PROVIDES:-}
myst-2-riven-fr
myst-2-riven-en"

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

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons
content_inclusion_default

# Link the fonts in the hardcoded path the game engine expects.

fonts_source=$(path_fonts_ttf)
fonts_destination="$(package_path 'PKG_MAIN')$(path_game_data)"
mkdir --parents "$fonts_destination"
ln --symbolic --target-directory="$fonts_destination" \
	"${fonts_source}/freesans.ttf" \
	"${fonts_source}/mplus-2c-regular.ttf"

# Write launchers.

## Set game language.
game_exec_line() {
	cat <<- 'EOF'
	# Set game language
	if [ -e "${PATH_GAME_DATA}/b_data_french.mhk" ]; then
	    language=fr
	else
	    language=en
	fi
	scummvm --language="$language" --path="$PATH_GAME_DATA" "$@" mohawk:riven
	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_MAIN' 'PKG_L10N_FR'
printf "$lang_string" "$lang_en"
print_instructions 'PKG_MAIN' 'PKG_L10N_EN'

# Clean up.

working_directory_cleanup

exit 0
