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

###
# Infinity Engine Enhanced Edition expansions:
# - Baldur's Gate Enhanced Edition - Siege of Dragonspear
# - Baldur's Gate Enhanced Edition - Faces of Good and Evil
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260104.1

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID_DRAGONSPEAR='baldurs-gate-1-enhanced-edition'
GAME_NAME_DRAGONSPEAR='Baldurʼs Gate Enhanced Edition'

GAME_ID_FACES='baldurs-gate-1-enhanced-edition'
GAME_NAME_FACES='Baldurʼs Gate Enhanced Edition'

EXPANSION_ID_DRAGONSPEAR='siege-of-dragonspear'
EXPANSION_NAME_DRAGONSPEAR='Siege of Dragonspear'

EXPANSION_ID_FACES='faces-of-good-and-evil'
EXPANSION_NAME_FACES='Faces of Good and Evil'

## Siege of Dragonspear

ARCHIVE_BASE_DRAGONSPEAR_2_NAME='baldur_s_gate_siege_of_dragonspear_2_6_6_0_p_81762.sh'
ARCHIVE_BASE_DRAGONSPEAR_2_MD5='2bb1b6eaefb2744f9bbb32f82a30e60c'
ARCHIVE_BASE_DRAGONSPEAR_2_SIZE='1887043'
ARCHIVE_BASE_DRAGONSPEAR_2_VERSION='2.6.6.0-gog81762'
ARCHIVE_BASE_DRAGONSPEAR_2_URL='https://www.gog.com/game/baldurs_gate_siege_of_dragonspear'

ARCHIVE_BASE_DRAGONSPEAR_1_NAME='baldur_s_gate_siege_of_dragonspear_2_6_6_0_47291.sh'
ARCHIVE_BASE_DRAGONSPEAR_1_MD5='36d275f6822b3cd2946ca606c0ebdb67'
ARCHIVE_BASE_DRAGONSPEAR_1_SIZE='1900000'
ARCHIVE_BASE_DRAGONSPEAR_1_VERSION='2.6.6.0-gog47291'

ARCHIVE_BASE_DRAGONSPEAR_0_NAME='baldur_s_gate_siege_of_dragonspear_2_6_5_0_46477.sh'
ARCHIVE_BASE_DRAGONSPEAR_0_MD5='27970876d9252fcb3174df8201db3ca3'
ARCHIVE_BASE_DRAGONSPEAR_0_SIZE='1900000'
ARCHIVE_BASE_DRAGONSPEAR_0_VERSION='2.6.5.0-gog46477'

## Faces of Good and Evil

ARCHIVE_BASE_FACES_2_NAME='baldur_s_gate_faces_of_good_and_evil_2_6_6_0_p_81762.sh'
ARCHIVE_BASE_FACES_2_MD5='6289884d62f5b9726c52050787be8099'
ARCHIVE_BASE_FACES_2_SIZE='2119'
ARCHIVE_BASE_FACES_2_VERSION='2.6.6.0-gog81762'
ARCHIVE_BASE_FACES_2_URL='https://www.gog.com/game/baldurs_gate_faces_of_good_and_evil'

ARCHIVE_BASE_FACES_1_NAME='baldur_s_gate_faces_of_good_and_evil_2_6_6_0_47291.sh'
ARCHIVE_BASE_FACES_1_MD5='5bcc622e44bdf9b03af64f5fe0a83d38'
ARCHIVE_BASE_FACES_1_SIZE='2500'
ARCHIVE_BASE_FACES_1_VERSION='2.6.6.0-gog47291'

CONTENT_PATH_DEFAULT='data/noarch/game'
CONTENT_GAME_MAIN_FILES_DRAGONSPEAR='
sod-dlc.zip'
CONTENT_GAME_MAIN_FILES_FACES='
override/yanner2?.bmp
override/M_YANN2.lua'

PKG_PARENT_ID_DRAGONSPEAR="$GAME_ID_DRAGONSPEAR"
PKG_PARENT_ID_FACES="$GAME_ID_FACES"

PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_PARENT'

# 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.

content_inclusion_default

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
