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

###
# Goblins original series:
# - Gobliiins
# - Gobliins 2
# - Goblins 3
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260226.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID_GOB1='goblins-1'
GAME_ID_GOB1_FLOPPY="${GAME_ID_GOB1}-floppy"
GAME_ID_GOB1_CDROM="${GAME_ID_GOB1}-cdrom"
GAME_NAME_GOB1='Gobliiins'
GAME_NAME_GOB1_FLOPPY="${GAME_NAME_GOB1} (floppy)"
GAME_NAME_GOB1_CDROM="${GAME_NAME_GOB1} (CD-ROM)"

GAME_ID_GOB2='goblins-2'
GAME_ID_GOB2_FLOPPY="${GAME_ID_GOB2}-floppy"
GAME_ID_GOB2_CDROM="${GAME_ID_GOB2}-cdrom"
GAME_NAME_GOB2='Gobliins 2: Le Prince Bouffon'
GAME_NAME_GOB2_FLOPPY="${GAME_NAME_GOB2} (floppy)"
GAME_NAME_GOB2_CDROM="${GAME_NAME_GOB2} (CD-ROM)"

GAME_ID_GOB3='goblins-3'
GAME_ID_GOB3_FLOPPY="${GAME_ID_GOB3}-floppy"
GAME_ID_GOB3_CDROM="${GAME_ID_GOB3}-cdrom"
GAME_NAME_GOB3='Goblins 3'
GAME_NAME_GOB3_FLOPPY="${GAME_NAME_GOB3} (floppy)"
GAME_NAME_GOB3_CDROM="${GAME_NAME_GOB3} (CD-ROM)"

# Gobliiins (GOG.com)

ARCHIVE_BASE_GOG_GOB1_0_NAME='setup_gobliiins_1.02_(20270).exe'
ARCHIVE_BASE_GOG_GOB1_0_MD5='dd4fa52b7ed43b964d82f2056c18b681'
ARCHIVE_BASE_GOG_GOB1_0_TYPE='innosetup'
ARCHIVE_BASE_GOG_GOB1_0_SIZE='99000'
ARCHIVE_BASE_GOG_GOB1_0_VERSION='1.02-gog20270'
ARCHIVE_BASE_GOG_GOB1_0_URL='https://www.gog.com/game/gobliiins_pack'

# Gobliins 2 (GOG.com)

ARCHIVE_BASE_GOG_GOB2_0_NAME='setup_gobliins_2_-_the_prince_buffoon_1.02_(20270).exe'
ARCHIVE_BASE_GOG_GOB2_0_MD5='3607f4ab042fea51e3b6544775955701'
ARCHIVE_BASE_GOG_GOB2_0_TYPE='innosetup'
ARCHIVE_BASE_GOG_GOB2_0_SIZE='110000'
ARCHIVE_BASE_GOG_GOB2_0_VERSION='1.02-gog20270'
ARCHIVE_BASE_GOG_GOB2_0_URL='https://www.gog.com/game/gobliiins_pack'

# Goblins 3 (GOG.com)

ARCHIVE_BASE_GOG_GOB3_FR_0_NAME='setup_goblins_quest_3_1.02_(french)_(20270).exe'
ARCHIVE_BASE_GOG_GOB3_FR_0_MD5='52649e08b57d8edfdbb4b72bc032e625'
ARCHIVE_BASE_GOG_GOB3_FR_0_TYPE='innosetup'
ARCHIVE_BASE_GOG_GOB3_FR_0_SIZE='200000'
ARCHIVE_BASE_GOG_GOB3_FR_0_VERSION='1.02-gog20270'
ARCHIVE_BASE_GOG_GOB3_FR_0_URL='https://www.gog.com/game/gobliiins_pack'

ARCHIVE_BASE_GOG_GOB3_EN_0_NAME='setup_goblins_quest_3_1.02_(20270).exe'
ARCHIVE_BASE_GOG_GOB3_EN_0_MD5='9d98b9f643dad9c793416d50bcbd9f17'
ARCHIVE_BASE_GOG_GOB3_EN_0_TYPE='innosetup'
ARCHIVE_BASE_GOG_GOB3_EN_0_SIZE='210000'
ARCHIVE_BASE_GOG_GOB3_EN_0_VERSION='1.02-gog20270'
ARCHIVE_BASE_GOG_GOB3_EN_0_URL='https://www.gog.com/game/gobliiins_pack'

# Adi 4 Collège CD-ROM

ARCHIVE_BASE_ADI4COLLEGE_0_NAME='ADI4_COLLEGE_2.iso'
ARCHIVE_BASE_ADI4COLLEGE_0_MD5='21c03f5cb4d7debfd50de31ee4fdf07a'
ARCHIVE_BASE_ADI4COLLEGE_0_SIZE='631210'
# TODO: Support for contextual version would allow setting a distinct version for each package/game.
ARCHIVE_BASE_ADI4COLLEGE_0_VERSION='1.0-adi4.21'

CONTENT_PATH_DEFAULT_GOG='.'
CONTENT_PATH_DEFAULT_ADI4COLLEGE='Games'
CONTENT_GAME_GOB1_FLOPPY_RELATIVE_PATH_GOG='fdd'
CONTENT_GAME_GOB1_FLOPPY_RELATIVE_PATH_ADI4COLLEGE='Gob1'
CONTENT_GAME_GOB1_FLOPPY_FILES='
*.FNT
*.MID
*.STK'
CONTENT_GAME_GOB2_FLOPPY_RELATIVE_PATH_GOG='fdd'
CONTENT_GAME_GOB2_FLOPPY_RELATIVE_PATH_ADI4COLLEGE='Gob2'
CONTENT_GAME_GOB2_FLOPPY_FILES='
*.MID
*.STK'
CONTENT_GAME_GOB3_FLOPPY_RELATIVE_PATH_GOG='fdd'
CONTENT_GAME_GOB3_FLOPPY_RELATIVE_PATH_ADI4COLLEGE='Gob3'
CONTENT_GAME_GOB3_FLOPPY_FILES='
IMD.ITK
*.MID
*.STK'
CONTENT_GAME_GOB1_CDROM_FILES='
GOB.LIC
Track1.mp3
*.STK'
CONTENT_GAME_GOB2_CDROM_FILES='
GOBNEW.LIC
Track1.mp3
*.STK'
CONTENT_GAME_GOB3_CDROM_FILES='
MDOS.IMD
IMD.ITK
MUS_GOB3.LIC
*.STK'
CONTENT_GAME_GOB3_L10N_FILES='
??GOB3.ITK'
CONTENT_GOB1_DOC_DOC_FILES='
*.pdf'
CONTENT_GOB2_DOC_DOC_FILES='
*.pdf'
CONTENT_GOB3_DOC_DOC_FILES='
*.pdf'

APP_GOB1_SCUMMID='gob:gob1'
APP_GOB1_ICON='goggame-1207662273.ico'

APP_GOB2_SCUMMID='gob:gob2'
APP_GOB2_ICON='goggame-1207662293.ico'

APP_GOB3_SCUMMID='gob:gob3'
APP_GOB3_ICON='goggame-1207662313.ico'

PACKAGES_LIST_GOG_GOB1='
PKG_GOB1_DOC
PKG_GOB1_FLOPPY
PKG_GOB1_CDROM'
PACKAGES_LIST_GOG_GOB2='
PKG_GOB2_DOC
PKG_GOB2_FLOPPY
PKG_GOB2_CDROM'
PACKAGES_LIST_GOG_GOB3='
PKG_GOB3_DOC
PKG_GOB3_FLOPPY
PKG_GOB3_CDROM
PKG_GOB3_L10N'
PACKAGES_LIST_ADI4COLLEGE='
PKG_GOB1_FLOPPY
PKG_GOB2_FLOPPY
PKG_GOB3_FLOPPY'

PKG_GOB1_DOC_ID="${GAME_ID_GOB1}-doc"
PKG_GOB2_DOC_ID="${GAME_ID_GOB2}-doc"
PKG_GOB3_DOC_ID="${GAME_ID_GOB3}-doc"
# Easier upgrades from pre-20260226.1 packages
PKG_GOB1_DOC_PROVIDES='
goblins-1-common'
PKG_GOB2_DOC_PROVIDES='
goblins-2-common'
PKG_GOB3_DOC_PROVIDES='
goblins-3-common'

PKG_GOB3_CDROM_DEPENDENCIES_SIBLINGS='
PKG_GOB3_L10N_BASE'

PKG_GOB3_L10N_BASE_ID="${GAME_ID_GOB3}-l10n"
PKG_GOB3_L10N_ID_GOG_GOB3_FR="${PKG_GOB3_L10N_BASE_ID}-fr"
PKG_GOB3_L10N_ID_GOG_GOB3_EN="${PKG_GOB3_L10N_BASE_ID}-en"
PKG_GOB3_L10N_PROVIDES_GOB3="
$PKG_GOB3_L10N_BASE_ID"
PKG_GOB3_L10N_DESCRIPTION_GOG_GOB3_FR='French localisation'
PKG_GOB3_L10N_DESCRIPTION_GOG_GOB3_EN='English localisation'

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

# Extract game data.

archive_extraction_default

# Include game data.

case "$(current_archive)" in
	('ARCHIVE_BASE_GOG_GOB1_'*)
		content_inclusion_icons 'PKG_GOB1_FLOPPY' 'APP_GOB1'
		content_inclusion_icons 'PKG_GOB1_CDROM'  'APP_GOB1'
	;;
	('ARCHIVE_BASE_GOG_GOB2_'*)
		content_inclusion_icons 'PKG_GOB2_FLOPPY' 'APP_GOB2'
		content_inclusion_icons 'PKG_GOB2_CDROM'  'APP_GOB2'
	;;
	('ARCHIVE_BASE_GOG_GOB3_'*)
		content_inclusion_icons 'PKG_GOB3_FLOPPY' 'APP_GOB3'
		content_inclusion_icons 'PKG_GOB3_CDROM'  'APP_GOB3'
	;;
	('ARCHIVE_BASE_ADI4COLLEGE_'*)
		# No icon is provided with the Adi 4 CD-ROM.
		# TODO: Provide icons through an optional archive.
	;;
esac
content_inclusion_default

# Write launchers.

case "$(current_archive)" in
	('ARCHIVE_BASE_GOG_GOB1_'*)
		launchers_generation 'PKG_GOB1_FLOPPY' 'APP_GOB1'
		launchers_generation 'PKG_GOB1_CDROM'  'APP_GOB1'
	;;
	('ARCHIVE_BASE_GOG_GOB2_'*)
		launchers_generation 'PKG_GOB2_FLOPPY' 'APP_GOB2'
		launchers_generation 'PKG_GOB2_CDROM'  'APP_GOB2'
	;;
	('ARCHIVE_BASE_GOG_GOB3_'*)
		launchers_generation 'PKG_GOB3_FLOPPY' 'APP_GOB3'
		APP_GOB3_PRERUN='
		# Set game language.
		SCUMMVM_LANGUAGE=$(
		    basename "${PATH_GAME_DATA}"/??gob3.itk |
		    grep --only-matching --regexp="^.."
		)
		'
		game_exec_line() {
			cat <<- 'EOF'
			scummvm --language="$SCUMMVM_LANGUAGE" --path="$PATH_GAME_DATA" "$@" gob:gob3
			EOF
		}
		launchers_generation 'PKG_GOB3_CDROM'  'APP_GOB3'
	;;
	('ARCHIVE_BASE_ADI4COLLEGE_'*)
		launchers_generation 'PKG_GOB1_FLOPPY' 'APP_GOB1'
		launchers_generation 'PKG_GOB2_FLOPPY' 'APP_GOB2'
		launchers_generation 'PKG_GOB3_FLOPPY' 'APP_GOB3'
	;;
esac

# Build packages.

packages_generation
case "$(current_archive)" in
	('ARCHIVE_BASE_GOG_GOB1_'*)
		(
			set_current_package 'PKG_GOB1_FLOPPY'
			print_instructions 'PKG_GOB1_DOC' 'PKG_GOB1_FLOPPY'
			set_current_package 'PKG_GOB1_CDROM'
			print_instructions 'PKG_GOB1_DOC' 'PKG_GOB1_CDROM'
		)
	;;
	('ARCHIVE_BASE_GOG_GOB2_'*)
		(
			set_current_package 'PKG_GOB2_FLOPPY'
			print_instructions 'PKG_GOB2_DOC' 'PKG_GOB2_FLOPPY'
			set_current_package 'PKG_GOB2_CDROM'
			print_instructions 'PKG_GOB2_DOC' 'PKG_GOB2_CDROM'
		)
	;;
	('ARCHIVE_BASE_GOG_GOB3_'*)
		(
			set_current_package 'PKG_GOB3_FLOPPY'
			print_instructions 'PKG_GOB3_DOC' 'PKG_GOB3_FLOPPY'
			set_current_package 'PKG_GOB3_CDROM'
			print_instructions 'PKG_GOB3_DOC' 'PKG_GOB3_CDROM' 'PKG_GOB3_L10N'
		)
	;;
	('ARCHIVE_BASE_ADI4COLLEGE_'*)
		(
			# TODO: print_instructions should set the context to the first package in its arguments list.
			# (that would be the default package with the implicit list, keeping the current behaviour)
			for PLAYIT_CONTEXT_PACKAGE in \
				'PKG_GOB1_FLOPPY' \
				'PKG_GOB2_FLOPPY' \
				'PKG_GOB3_FLOPPY'
			do
				print_instructions "$PLAYIT_CONTEXT_PACKAGE"
			done
		)
	;;
esac

# Clean up.

working_directory_cleanup

exit 0
