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

###
# Warcraft 2
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260322.1

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='warcraft-2'
GAME_NAME='Warcraft II: Tides of Darkness'

# The following installers are no longer available for sale from GOG,
# they have been removed when Blizzard released a remaster tied to their DRM system.

ARCHIVE_BASE_FR_0_NAME='setup_warcraft_ii_2.02_v5_(french)_(78104).exe'
ARCHIVE_BASE_FR_0_MD5='346bb75021090767a68902947b4a19ed'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_PART1_NAME='setup_warcraft_ii_2.02_v5_(french)_(78104)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5='bffa0de5bf00595a24c1d468c896d393'
ARCHIVE_BASE_FR_0_VERSION='2.02-gog78104'
ARCHIVE_BASE_FR_0_SIZE='711005'

ARCHIVE_BASE_EN_0_NAME='setup_warcraft_ii_2.02_v5_(78104).exe'
ARCHIVE_BASE_EN_0_MD5='6e5f20698076b193f3e94a4f762b361f'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_PART1_NAME='setup_warcraft_ii_2.02_v5_(78104)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5='3ac578bac3d490f3994f88311936a544'
ARCHIVE_BASE_EN_0_VERSION='2.02-gog78104'
ARCHIVE_BASE_EN_0_SIZE='732702'

CONTENT_PATH_DEFAULT='.'
CONTENT_GAME_BIN_FILES='
W2SOUNDS.EXE
Warcraft II BNE.exe
battle.snp'
# warcraft ii bne.exe requirements
CONTENT_LIBS_BIN_FILES='
Storm.dll'
# The game triggers an error if the following library is missing, then only renders a black screen (with sound).
CONTENT_LIBS0_BIN_FILES='
W2Local.dll'
# Videos are not played if the following library is missing.
CONTENT_LIBS1_BIN_FILES='
smackw32.dll'
# TODO: Move the map editor to a dedicated package.
CONTENT_GAME0_BIN_FILES='
Warcraft II Map Editor.exe'
CONTENT_GAME_DATA_FILES='
GameSFX
Maps
Support
bncache.dat
install.mpq
War2Dat.mpq
War2Patch.mpq'
CONTENT_DOC_DATA_FILES='
License.txt
patch.txt'
# TODO: Move the map editor to a dedicated package.
CONTENT_DOC0_DATA_FILES='
Warcraft II Map Editor.chm'

USER_PERSISTENT_DIRECTORIES='
Maps
Save'

APP_MAIN_EXE='Warcraft II BNE.exe'

# TODO: Move the map editor to a dedicated package.
APP_EDITOR_ID="${GAME_ID}-editor"
APP_EDITOR_NAME='Warcraft II Map Editor'
APP_EDITOR_EXE='Warcraft II Map Editor.exe'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_ID="$GAME_ID"
PKG_BIN_ID_FR="${PKG_BIN_ID}-fr"
PKG_BIN_ID_EN="${PKG_BIN_ID}-en"
PKG_BIN_PROVIDES="
$GAME_ID"
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'
PKG_BIN_DESCRIPTION_FR='Binaries (French localisation)'
PKG_BIN_DESCRIPTION_EN='Binaries (English localisation)'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_ID_FR="${PKG_DATA_ID}-fr"
PKG_DATA_ID_EN="${PKG_DATA_ID}-en"
PKG_DATA_PROVIDES="
$PKG_DATA_ID"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DESCRIPTION_FR='Data (French localisation)'
PKG_DATA_DESCRIPTION_EN='Data (English localisation)'

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

# Extract game data.

archive_extraction_default

# 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
