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

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

script_version=20260326.6

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='myst-1-realmyst'
GAME_NAME='realMyst'

# This game is no longer available for sale from gog.com.
ARCHIVE_BASE_0_NAME='setup_realmyst_1.11_(21511).exe'
ARCHIVE_BASE_0_MD5='30bf7206c397ea3a3c3e8b5e73817dcc'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='774727'
ARCHIVE_BASE_0_VERSION='1.1.1-gog21511'

# Native Indeo video decoder is required to play linking books videos.
ARCHIVE_REQUIRED_INDEO_NAME='iv5setup.exe'
ARCHIVE_REQUIRED_INDEO_MD5='359afe192360a11e6bd94611a473687c'
ARCHIVE_REQUIRED_INDEO_EXTRACTOR='unar'
ARCHIVE_REQUIRED_INDEO_SIZE='6315'
ARCHIVE_REQUIRED_INDEO_URL='https://download.civforum.de/civ2/iv5setup.exe'
# This archive is a self-extracting InstallShield installer.
REQUIREMENTS_LIST='
unshield'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
RealMYST.exe
realMYSTSetup.exe'
# RealMYST.exe triggers an explicit error if binkw32.dll is missing.
CONTENT_LIBS_BIN_FILES='
binkw32.dll'
# Some puzzles are broken if the following libraries are missing.
CONTENT_LIBS0_BIN_FILES='
XCmd/M3D_LibraryXCmd.dll
XCmd/M3D_MazeXcmd.dll
XCmd/M3D_Xcmd.dll'
# Native Indeo video decoder is required to play linking books videos.
CONTENT_LIBS1_BIN_PATH='indeo_5_codec'
CONTENT_LIBS1_BIN_FILES='
ir50_32.dll'
CONTENT_GAME_DATA_FILES='
FNT
INIT
Scn
mara.dni
pera.dni
sho.dni'
CONTENT_DOC_DATA_FILES='
readme.txt'

USER_PERSISTENT_DIRECTORIES='
INIT
sav'
# Crashes happen on some age transitions if the following directory is missing.
USER_PERSISTENT_DIRECTORIES="$USER_PERSISTENT_DIRECTORIES
swp"

# Native Indeo video decoder is required to play linking books videos.
# (WINE 11.5)
WINE_DLLOVERRIDES_DEFAULT='winemenubuilder.exe,mscoree,mshtml='
WINE_DLLOVERRIDES_DEFAULT="${WINE_DLLOVERRIDES_DEFAULT};ir50_32.dll=n"

APP_MAIN_EXE='RealMYST.exe'
APP_MAIN_ICON='app/goggame-1207658824.ico'

APP_SETUP_ID="${GAME_ID}-setup"
APP_SETUP_NAME="${GAME_NAME} - Setup"
APP_SETUP_CAT='Settings'
APP_SETUP_EXE='realMYSTSetup.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

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

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

# Check for the presence of the Indeo video decoder installer.

archive_initialize_required \
	'ARCHIVE_INDEO' \
	'ARCHIVE_REQUIRED_INDEO'

# Extract game data.

archive_extraction_default

# Extract the required video decoding library.

archive_extraction 'ARCHIVE_INDEO'
ARCHIVE_INDEO_INNER_PATH="${PLAYIT_WORKDIR}/gamedata/data1.cab"
ARCHIVE_INDEO_INNER_TYPE='installshield'
archive_extraction 'ARCHIVE_INDEO_INNER'
rm "$ARCHIVE_INDEO_INNER_PATH"

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
