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

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

script_version=20260101.2

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='diablo-1'
GAME_NAME='Diablo'

ARCHIVE_BASE_8_NAME='setup_diablo_1.09_hellfire_v4_(78466).exe'
ARCHIVE_BASE_8_MD5='c4d36192c35eb5f34faa1d97aae34455'
ARCHIVE_BASE_8_TYPE='innosetup'
ARCHIVE_BASE_8_SIZE='845401'
ARCHIVE_BASE_8_VERSION='1.09-gog78466'
ARCHIVE_BASE_8_URL='https://www.gog.com/game/diablo'

ARCHIVE_BASE_7_NAME='setup_diablo_1.09_hellfire_v3_(78106).exe'
ARCHIVE_BASE_7_MD5='b8d5a38fd4b21311853f19559c2ebfee'
ARCHIVE_BASE_7_TYPE='innosetup'
ARCHIVE_BASE_7_SIZE='845020'
ARCHIVE_BASE_7_VERSION='1.09-gog78106'

ARCHIVE_BASE_6_NAME='setup_diablo_1.09_hellfire_v2_(30037).exe'
ARCHIVE_BASE_6_MD5='2b8f0eafc528a56452b0008dde4151a8'
ARCHIVE_BASE_6_TYPE='innosetup'
ARCHIVE_BASE_6_SIZE='843414'
ARCHIVE_BASE_6_VERSION='1.09-gog30037'

ARCHIVE_BASE_5_NAME='setup_diablo_1.09_hellfire_v2_(30038).exe'
ARCHIVE_BASE_5_MD5='e70187d92fa120771db99dfa81679cfc'
ARCHIVE_BASE_5_TYPE='innosetup'
ARCHIVE_BASE_5_SIZE='850000'
ARCHIVE_BASE_5_VERSION='1.09-gog30038'

ARCHIVE_BASE_4_NAME='setup_diablo_1.09_v6_(28378).exe'
ARCHIVE_BASE_4_MD5='588ab50c1ef25abb682b86ea4306ea50'
ARCHIVE_BASE_4_TYPE='innosetup'
ARCHIVE_BASE_4_SIZE='670000'
ARCHIVE_BASE_4_VERSION='1.09-gog28378'

ARCHIVE_BASE_3_NAME='setup_diablo_1.09_v4_(27989).exe'
ARCHIVE_BASE_3_MD5='8dac74a616646fa41d5d73f4765cef40'
ARCHIVE_BASE_3_TYPE='innosetup'
ARCHIVE_BASE_3_SIZE='670000'
ARCHIVE_BASE_3_VERSION='1.09-gog27989'

ARCHIVE_BASE_2_NAME='setup_diablo_1.09_v3_(27965).exe'
ARCHIVE_BASE_2_MD5='38d654af858d7a2591711f0e6324fcd0'
ARCHIVE_BASE_2_TYPE='innosetup'
ARCHIVE_BASE_2_SIZE='670000'
ARCHIVE_BASE_2_VERSION='1.09-gog27695'

ARCHIVE_BASE_1_NAME='setup_diablo_1.09_v2_(27882).exe'
ARCHIVE_BASE_1_MD5='83b2d6b8551a9825a426dac7b9302654'
ARCHIVE_BASE_1_TYPE='innosetup'
ARCHIVE_BASE_1_SIZE='670000'
ARCHIVE_BASE_1_VERSION='1.09-gog27882'

ARCHIVE_BASE_0_NAME='setup_diablo_1.09_(27873).exe'
ARCHIVE_BASE_0_MD5='bf57594f5218a794a284b5e2a0f5ba14'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='680000'
ARCHIVE_BASE_0_VERSION='1.09-gog27873'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_MAIN_FILES='
diabdat.mpq
drtl_ix86_108_109.mpq
patch_rt.mpq'
CONTENT_DOC_MAIN_FILES='
manual.pdf
manual_de.pdf
manual_fr.pdf
manual_it.pdf
license.txt
patch.txt
readme.txt
update.txt'

APP_MAIN_TYPE='custom'
APP_MAIN_ICON='diablo.exe'

PKG_MAIN_DEPENDENCIES_COMMANDS='
devilutionx'
## Easier upgrades from pre-20260101.2 packages
PKG_MAIN_PROVIDES='
diablo-1-data'

# 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_icons
content_inclusion_default

# Write launchers.

custom_launcher() {
	launcher_headers
	cat <<- EOF
	devilutionx --data-dir "$(path_game_data)"
	EOF
	launcher_exit
}
launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
