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

###
# Monkey Island Special Edition:
# - The Secret of Monkey Island
# - Monkey Island 2: LeChuck's Revenge
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260330.3

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_MONKEY1='monkey-island-1-special-edition'
GAME_NAME_MONKEY1='The Secret of Monkey Island'

GAME_ID_MONKEY2='monkey-island-2-special-edition'
GAME_NAME_MONKEY2='Monkey Island 2: LeChuck’s Revenge'

# The Secret of Monkey Island

ARCHIVE_BASE_MONKEY1_1_NAME='setup_the_secret_of_monkey_islandtm_special_edition_1.0_(18587).exe'
ARCHIVE_BASE_MONKEY1_1_MD5='dc463e4d640e417df53684afb9921dd1'
ARCHIVE_BASE_MONKEY1_1_TYPE='innosetup'
ARCHIVE_BASE_MONKEY1_1_SIZE='2594611'
ARCHIVE_BASE_MONKEY1_1_VERSION='1.0-gog18587'
ARCHIVE_BASE_MONKEY1_1_URL='https://www.gog.com/game/the_secret_of_monkey_island_special_edition'

ARCHIVE_BASE_MONKEY1_0_NAME='setup_monkey_island_1_se_1.0_(18587).exe'
ARCHIVE_BASE_MONKEY1_0_MD5='ff2eaa21af8f59371583b007b439b873'
ARCHIVE_BASE_MONKEY1_0_TYPE='innosetup'
ARCHIVE_BASE_MONKEY1_0_PART1_NAME='setup_monkey_island_1_se_1.0_(18587)-1.bin'
ARCHIVE_BASE_MONKEY1_0_PART1_MD5='6a3ca78328b99ae0d9d0a3d7a4fb3cd9'
ARCHIVE_BASE_MONKEY1_0_SIZE='2600000'
ARCHIVE_BASE_MONKEY1_0_VERSION='1.0-gog18587'

# Monkey Island 2: LeChuck's Revenge

ARCHIVE_BASE_MONKEY2_0_NAME='setup_monkey_island2_se_2.0.0.10.exe'
ARCHIVE_BASE_MONKEY2_0_MD5='20a0bc39dcf543856f0d463649c482c4'
ARCHIVE_BASE_MONKEY2_0_TYPE='innosetup'
ARCHIVE_BASE_MONKEY2_0_SIZE='2300000'
ARCHIVE_BASE_MONKEY2_0_VERSION='1.0-gog2.0.0.10'
ARCHIVE_BASE_MONKEY2_0_URL='https://www.gog.com/game/monkey_island_2_special_edition_lechucks_revenge'

CONTENT_PATH_DEFAULT_MONKEY1='.'
CONTENT_PATH_DEFAULT_MONKEY1_0='app'
CONTENT_PATH_DEFAULT_MONKEY2='app'
CONTENT_GAME_BIN_FILES_MONKEY1='
MISE.exe'
CONTENT_GAME_BIN_FILES_MONKEY2='
Monkey2.exe'
CONTENT_GAME_DATA_FILES='
audio'
# No need for dedicated localisation packages,
# language selection is done in-game.
CONTENT_GAME0_DATA_FILES_MONKEY1='
localization
Monkey1.pak'
CONTENT_GAME0_DATA_FILES_MONKEY2='
monkey2.pak'
# TODO: Build dedicated packages for localisations.
CONTENT_GAME1_DATA_FILES_MONKEY2='
lang.ini'
CONTENT_DOC_DATA_FILES='
*.pdf'

# The following file includes the saved games,
# the AppData path set below only includes settings.
USER_PERSISTENT_FILES_MONKEY2='
monkey2.bin'

WINE_PERSISTENT_DIRECTORIES_MONKEY1='
users/${USER}/AppData/Roaming/LucasArts/The Secret of Monkey Island Special Edition'
WINE_PERSISTENT_DIRECTORIES_MONKEY2='
users/${USER}/AppData/Roaming/LucasArts/Monkey Island 2 Special Edition'
# Native D3DCompiler is required to prevent a crash on launch.
# - https://bugs.winehq.org/show_bug.cgi?id=37676
# - https://bugs.winehq.org/show_bug.cgi?id=42026
# (WINE 11.5)
# TODO: Check if d3dcompiler_42 could work for both games.
WINE_WINETRICKS_VERBS_MONKEY1='d3dcompiler_47'
WINE_WINETRICKS_VERBS_MONKEY2='d3dcompiler_42'

APP_MAIN_EXE_MONKEY1='MISE.exe'
APP_MAIN_EXE_MONKEY2='Monkey2.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
audioconvert
audio/x-wma, wmaversion=(int)1'

PKG_DATA_ID_MONKEY1="${GAME_ID_MONKEY1}-data"
PKG_DATA_ID_MONKEY2="${GAME_ID_MONKEY2}-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 '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

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
