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

###
# Anno 1404
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260406.9

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='anno-1404'
GAME_NAME='Anno 1404'

GAME_ID_VENICE="${GAME_ID}-venice"
GAME_NAME_VENICE="$GAME_NAME - Venice"

ARCHIVE_BASE_FR_2_NAME='setup_anno_1404_2.01_gog_v3_(french)_(82734).exe'
ARCHIVE_BASE_FR_2_MD5='962fc9e000c96b61c10b86fd689f0965'
ARCHIVE_BASE_FR_2_TYPE='innosetup'
ARCHIVE_BASE_FR_2_PART1_NAME='setup_anno_1404_2.01_gog_v3_(french)_(82734)-1.bin'
ARCHIVE_BASE_FR_2_PART1_MD5='a317f491845498b3fcd99ef0da5a3201'
ARCHIVE_BASE_FR_2_SIZE='3987108'
ARCHIVE_BASE_FR_2_VERSION='2.01.5010-gog82734'
ARCHIVE_BASE_FR_2_URL='https://www.gog.com/game/anno_1404_gold_edition'

ARCHIVE_BASE_DE_2_NAME='setup_anno_1404_2.01_gog_v3_(german)_(82734).exe'
ARCHIVE_BASE_DE_2_MD5='8429276be0c88e10391d678fc0084042'
ARCHIVE_BASE_DE_2_TYPE='innosetup'
ARCHIVE_BASE_DE_2_PART1_NAME='setup_anno_1404_2.01_gog_v3_(german)_(82734)-1.bin'
ARCHIVE_BASE_DE_2_PART1_MD5='0949763a297acf8f90208b2ebc0b4ed6'
ARCHIVE_BASE_DE_2_SIZE='4065604'
ARCHIVE_BASE_DE_2_VERSION='2.01.5010-gog82734'
ARCHIVE_BASE_DE_2_URL='https://www.gog.com/game/anno_1404_gold_edition'

ARCHIVE_BASE_EN_2_NAME='setup_anno_1404_2.01_gog_v3_(82734).exe'
ARCHIVE_BASE_EN_2_MD5='3bfccb25804b0f40d6c77540e3d98fee'
ARCHIVE_BASE_EN_2_TYPE='innosetup'
ARCHIVE_BASE_EN_2_PART1_NAME='setup_anno_1404_2.01_gog_v3_(82734)-1.bin'
ARCHIVE_BASE_EN_2_PART1_MD5='bc079fa06f11197aeac9d5a1ad8369a0'
ARCHIVE_BASE_EN_2_SIZE='4015442'
ARCHIVE_BASE_EN_2_VERSION='2.01.5010-gog82734'
ARCHIVE_BASE_EN_2_URL='https://www.gog.com/game/anno_1404_gold_edition'

ARCHIVE_BASE_FR_1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(french)_(76234).exe'
ARCHIVE_BASE_FR_1_MD5='45fa9dd36cfb3fc4fc90de9758c16022'
ARCHIVE_BASE_FR_1_TYPE='innosetup'
ARCHIVE_BASE_FR_1_PART1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(french)_(76234)-1.bin'
ARCHIVE_BASE_FR_1_PART1_MD5='d632639d4847f91a37f5d37627c23d3e'
ARCHIVE_BASE_FR_1_SIZE='3987056'
ARCHIVE_BASE_FR_1_VERSION='2.01.5010-gog76234'

ARCHIVE_BASE_DE_1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(german)_(76234).exe'
ARCHIVE_BASE_DE_1_MD5='2fb420a6ff653cfa5c99e69f4b18c917'
ARCHIVE_BASE_DE_1_TYPE='innosetup'
ARCHIVE_BASE_DE_1_PART1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(german)_(76234)-1.bin'
ARCHIVE_BASE_DE_1_PART1_MD5='edaced2fd4e8e61891afe297585c8c79'
ARCHIVE_BASE_DE_1_SIZE='4065552'
ARCHIVE_BASE_DE_1_VERSION='2.01.5010-gog76234'

ARCHIVE_BASE_EN_1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(76234).exe'
ARCHIVE_BASE_EN_1_MD5='d307aae42bcc14f3358732e374f37b85'
ARCHIVE_BASE_EN_1_TYPE='innosetup'
ARCHIVE_BASE_EN_1_PART1_NAME='setup_anno_1404_2.01_v2_gog_0.1_(76234)-1.bin'
ARCHIVE_BASE_EN_1_PART1_MD5='7867cd6e09aec40e5f36637884249b0b'
ARCHIVE_BASE_EN_1_SIZE='4015390'
ARCHIVE_BASE_EN_1_VERSION='2.01.5010-gog76234'

ARCHIVE_BASE_FR_0_NAME='setup_anno_1404_2.01_v2_(french)_(30326).exe'
ARCHIVE_BASE_FR_0_MD5='24be40c2f1ce714dcc9c505ed62fcdb2'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_PART1_NAME='setup_anno_1404_2.01_v2_(french)_(30326)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5='d2bbff77601562218b62b53901edd5e7'
ARCHIVE_BASE_FR_0_SIZE='4000000'
ARCHIVE_BASE_FR_0_VERSION='2.01.5010-gog30326'

ARCHIVE_BASE_EN_0_NAME='setup_anno_1404_2.01_v2_(30326).exe'
ARCHIVE_BASE_EN_0_MD5='5b92b95ddd3a60bff25afaca6531dab3'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_PART1_NAME='setup_anno_1404_2.01_v2_(30326)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5='3bf8dd4469d43392617df7737cebad04'
ARCHIVE_BASE_EN_0_SIZE='4100000'
ARCHIVE_BASE_EN_0_VERSION='2.01.5010-gog30326'

ARCHIVE_BASE_MULTILANG_0_NAME='setup_anno_1404_gold_edition_2.01.5010_(13111).exe'
ARCHIVE_BASE_MULTILANG_0_MD5='b19333f57c1c15b788e29ff6751dac20'
ARCHIVE_BASE_MULTILANG_0_TYPE='innosetup'
ARCHIVE_BASE_MULTILANG_0_PART1_NAME='setup_anno_1404_gold_edition_2.01.5010_(13111)-1.bin'
ARCHIVE_BASE_MULTILANG_0_PART1_MD5='17933b44bdb2a26d8d82ffbfdc494210'
ARCHIVE_BASE_MULTILANG_0_PART2_NAME='setup_anno_1404_gold_edition_2.01.5010_(13111)-2.bin'
ARCHIVE_BASE_MULTILANG_0_PART2_MD5='2f71f5378b5f27a84a41cc481a482bd6'
ARCHIVE_BASE_MULTILANG_0_SIZE='6200000'
ARCHIVE_BASE_MULTILANG_0_VERSION='2.01.5010-gog13111'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_MULTILANG='app'
CONTENT_GAME_BIN_FILES='
bin/miles
Anno4.exe'
CONTENT_GAME_BIN_VENICE_FILES='
Addon.exe'
# TODO: Install libraries in a standard path.
# TODO: Drop unused libraries.
CONTENT_GAME0_BIN_FILES='
bin/Anno1404GDF.dll
bin/binkw32.dll
bin/granny2.dll
bin/mss32.dll
bin/NetComEngine3.dll
tools/binkw32.dll'
# TODO: Drop unused binaries.
CONTENT_GAME1_BIN_FILES='
tools/anno1404_lang_switch.exe
tools/Anno4Web.exe
tools/Benchmark.exe'
CONTENT_GAME0_BIN_VENICE_FILES='
bin/Anno1404_Addon_GDF.dll'
CONTENT_GAME1_BIN_VENICE_FILES='
tools/AddonWeb.exe'
CONTENT_GAME_L10N_FR_FILES='
maindata/fra0.rda'
CONTENT_GAME_L10N_DE_FILES='
maindata/ger0.rda'
CONTENT_GAME_L10N_EN_FILES='
maindata/eng0.rda'
CONTENT_GAME_L10N_FR_VENICE_FILES='
addon/fra0.rda'
CONTENT_GAME_L10N_DE_VENICE_FILES='
addon/ger0.rda'
CONTENT_GAME_L10N_EN_VENICE_FILES='
addon/eng0.rda'
CONTENT_GAME_DATA_FILES='
data
maindata
Resources'
CONTENT_GAME_DATA_VENICE_FILES='
addon'
CONTENT_DOC_DATA_FILES='
Manual.pdf'
CONTENT_DOC_DATA_VENICE_FILES='
Manual addon.pdf'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Roaming/Ubisoft/Anno1404
users/${USER}/AppData/Roaming/Ubisoft/Anno1404Addon
users/${USER}/Documents/Anno 1404'
# Avoid rendering failures leading to a black screen.
# cf. https://bugs.winehq.org/show_bug.cgi?id=45624
# (WINE 11.6)
WINE_WINETRICKS_VERBS='d3dcompiler_47'

APP_MAIN_EXE='Anno4.exe'

APP_VENICE_ID="$GAME_ID_VENICE"
APP_VENICE_NAME="$GAME_NAME_VENICE"
APP_VENICE_EXE='Addon.exe'

PACKAGES_LIST_FR='
PKG_BIN
PKG_BIN_VENICE
PKG_L10N_FR
PKG_L10N_FR_VENICE
PKG_DATA
PKG_DATA_VENICE'
PACKAGES_LIST_DE='
PKG_BIN
PKG_BIN_VENICE
PKG_L10N_DE
PKG_L10N_DE_VENICE
PKG_DATA
PKG_DATA_VENICE'
PACKAGES_LIST_EN='
PKG_BIN
PKG_BIN_VENICE
PKG_L10N_EN
PKG_L10N_EN_VENICE
PKG_DATA
PKG_DATA_VENICE'
PACKAGES_LIST_MULTILANG='
PKG_BIN
PKG_BIN_VENICE
PKG_L10N_FR
PKG_L10N_FR_VENICE
PKG_L10N_DE
PKG_L10N_DE_VENICE
PKG_L10N_EN
PKG_L10N_EN_VENICE
PKG_DATA
PKG_DATA_VENICE'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'

PKG_BIN_VENICE_ID="$GAME_ID_VENICE"
PKG_BIN_VENICE_ARCH='32'
PKG_BIN_VENICE_DESCRIPTION='Venice expansion'
PKG_BIN_VENICE_DEPENDENCIES_SIBLINGS='
PKG_BIN
PKG_L10N_VENICE_BASE
PKG_DATA_VENICE'

PKG_L10N_BASE_ID="${GAME_ID}-l10n"
PKG_L10N_FR_ID="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_DE_ID="${PKG_L10N_BASE_ID}-de"
PKG_L10N_EN_ID="${PKG_L10N_BASE_ID}-en"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_DE_DESCRIPTION='German localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'

PKG_L10N_VENICE_BASE_ID="${GAME_ID_VENICE}-l10n"
PKG_L10N_FR_VENICE_ID="${PKG_L10N_VENICE_BASE_ID}-fr"
PKG_L10N_DE_VENICE_ID="${PKG_L10N_VENICE_BASE_ID}-de"
PKG_L10N_EN_VENICE_ID="${PKG_L10N_VENICE_BASE_ID}-en"
PKG_L10N_VENICE_PROVIDES="
$PKG_L10N_VENICE_BASE_ID"
PKG_L10N_FR_VENICE_PROVIDES="$PKG_L10N_VENICE_PROVIDES"
PKG_L10N_DE_VENICE_PROVIDES="$PKG_L10N_VENICE_PROVIDES"
PKG_L10N_EN_VENICE_PROVIDES="$PKG_L10N_VENICE_PROVIDES"
PKG_L10N_FR_VENICE_DESCRIPTION='Venice expansion - French localisation'
PKG_L10N_DE_VENICE_DESCRIPTION='Venice expansion - German localisation'
PKG_L10N_EN_VENICE_DESCRIPTION='Venice expansion - English localisation'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'

PKG_DATA_VENICE_ID="${GAME_ID_VENICE}-data"
PKG_DATA_VENICE_DESCRIPTION='Venice expansion - 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 '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'        'APP_MAIN'
content_inclusion_icons 'PKG_DATA_VENICE' 'APP_VENICE'
content_inclusion_default

# Write launchers.

launchers_generation 'PKG_BIN'        'APP_MAIN'
launchers_generation 'PKG_BIN_VENICE' 'APP_VENICE'

# Build packages.

packages_generation
case "$(current_archive)" in
	('ARCHIVE_BASE_FR_'*|'ARCHIVE_BASE_DE_'*|'ARCHIVE_BASE_EN_'*)
		print_instructions
	;;
	('ARCHIVE_BASE_MULTILANG_'*)
		case "$(messages_language)" in
			('fr')
				lang_string='version %s :'
				lang_fr='française'
				lang_de='allemande'
				lang_en='anglaise'
			;;
			('en'|*)
				lang_string='%s version:'
				lang_fr='French'
				lang_de='German'
				lang_en='English'
			;;
		esac
		printf '\n'
		printf "$lang_string" "$lang_fr"
		print_instructions \
			'PKG_BIN' 'PKG_DATA' 'PKG_L10N_FR' \
			'PKG_BIN_VENICE' 'PKG_DATA_VENICE' 'PKG_L10N_FR_VENICE'
		printf "$lang_string" "$lang_de"
		print_instructions \
			'PKG_BIN' 'PKG_DATA' 'PKG_L10N_DE' \
			'PKG_BIN_VENICE' 'PKG_DATA_VENICE' 'PKG_L10N_DE_VENICE'
		printf "$lang_string" "$lang_en"
		print_instructions \
			'PKG_BIN' 'PKG_DATA' 'PKG_L10N_EN' \
			'PKG_BIN_VENICE' 'PKG_DATA_VENICE' 'PKG_L10N_EN_VENICE'
	;;
esac

# Clean up.

working_directory_cleanup

exit 0
