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

###
# Tropico games:
# - Tropico
# - Tropico 2
# send your bug reports to vv221@dotslashplay.it
###

script_version=20260323.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID_TROPICO1='tropico-1'
GAME_NAME_TROPICO1='Tropico'

GAME_ID_TROPICO2='tropico-2'
GAME_NAME_TROPICO2='Tropico 2: Pirate Cove'

ARCHIVE_BASE_TROPICO1_FR_0_NAME='setup_tropico_french_2.1.0.14.exe'
ARCHIVE_BASE_TROPICO1_FR_0_MD5='aad4ea5a6fe2b2c2f347cfa7aae058b3'
ARCHIVE_BASE_TROPICO1_FR_0_TYPE='innosetup'
ARCHIVE_BASE_TROPICO1_FR_0_SIZE='1400000'
ARCHIVE_BASE_TROPICO1_FR_0_VERSION='1.5.3-gog2.1.0.14'
ARCHIVE_BASE_TROPICO1_FR_0_URL='https://www.gog.com/game/tropico_reloaded'

ARCHIVE_BASE_TROPICO1_EN_0_NAME='setup_tropico_2.1.0.14.exe'
ARCHIVE_BASE_TROPICO1_EN_0_MD5='1bd761bc4a40a42a9caeb41c70d46465'
ARCHIVE_BASE_TROPICO1_EN_0_TYPE='innosetup'
ARCHIVE_BASE_TROPICO1_EN_0_SIZE='1400000'
ARCHIVE_BASE_TROPICO1_EN_0_VERSION='1.5.3-gog2.1.0.14'
ARCHIVE_BASE_TROPICO1_EN_0_URL='https://www.gog.com/game/tropico_reloaded'

ARCHIVE_BASE_TROPICO2_FR_0_NAME='setup_tropico2_french_2.1.0.14.exe'
ARCHIVE_BASE_TROPICO2_FR_0_MD5='e9cb36d88a03fd65b7152c815f05a7cc'
ARCHIVE_BASE_TROPICO2_FR_0_TYPE='innosetup'
ARCHIVE_BASE_TROPICO2_FR_0_SIZE='1900000'
ARCHIVE_BASE_TROPICO2_FR_0_VERSION='1.20-gog2.1.0.14'
ARCHIVE_BASE_TROPICO2_FR_0_URL='https://www.gog.com/game/tropico_reloaded'

ARCHIVE_BASE_TROPICO2_EN_0_NAME='setup_tropico2_2.1.0.14.exe'
ARCHIVE_BASE_TROPICO2_EN_0_MD5='59a41778988f4b0a45d144f29187ffd8'
ARCHIVE_BASE_TROPICO2_EN_0_TYPE='innosetup'
ARCHIVE_BASE_TROPICO2_EN_0_SIZE='1900000'
ARCHIVE_BASE_TROPICO2_EN_0_VERSION='1.20-gog2.1.0.14'
ARCHIVE_BASE_TROPICO2_EN_0_URL='https://www.gog.com/game/tropico_reloaded'

CONTENT_PATH_DEFAULT='app'
# TODO: Check if/why these libraries are required.
CONTENT_LIBS_BIN_FILES='
binkw32.dll
mss32.dll'
CONTENT_GAME_BIN_FILES_TROPICO1='
Tropico.EXE'
CONTENT_GAME_BIN_FILES_TROPICO2='
Tropico2.exe
Tropico2.ini
Tropico2Safe.ini'
CONTENT_GAME0_BIN_FILES='
*.asi'
CONTENT_GAME_L10N_FILES='
Maps'
CONTENT_GAME0_L10N_FILES_TROPICO1='
data2/TROPICO.CFG
data2/px_cred.lng
data2/Tropico.lng
data2/x1.dap
data2/x2.dap
movies/s_f2o.bik
movies/s_m2o.bik
movies/s_s2o.bik
voice/Narrator'
CONTENT_GAME0_L10N_FILES_TROPICO2='
Data/soun.{}
Data/text.{}
Movies/CampaignToExit.bik
Movies/DeskToOutside.bik'
CONTENT_GAME_DATA_FILES='
Data
Movies'
CONTENT_GAME0_DATA_FILES_TROPICO1='
data2
voice/People'
CONTENT_DOC_L10N_FILES='
Manual.pdf
ReadMe.txt'
CONTENT_DOC0_L10N_FILES_TROPICO1='
HISTORY.TXT'
CONTENT_DOC0_L10N_FILES_TROPICO2='
PatchReadMe.doc
ReadMe.rtf
LastMinute.rtf'

USER_PERSISTENT_DIRECTORIES='
Campaign
Games
Maps
Save'
# TODO: Check if write access to all these files is required.
USER_PERSISTENT_FILES_TROPICO1='
data2/TROPICO.CFG
data2/HISCORE.DAT
data2/bldgtype.dat
data2/unittype.dat'
USER_PERSISTENT_FILES_TROPICO2='
Tropico2.ini
Tropico2Safe.ini'

# Tropico 2 - Loss of focus leads to graphical corruption.
# (WINE 10.0)
WINE_VIRTUAL_DESKTOP_TROPICO2='1600x1200'

APP_MAIN_EXE_TROPICO1='Tropico.EXE'
APP_MAIN_EXE_TROPICO2='Tropico2.exe'

PACKAGES_LIST='
PKG_BIN
PKG_L10N
PKG_DATA'

PKG_BIN_ARCH='32'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE
PKG_DATA'

PKG_L10N_BASE_ID_TROPICO1="${GAME_ID_TROPICO1}-l10n"
PKG_L10N_BASE_ID_TROPICO2="${GAME_ID_TROPICO2}-l10n"
PKG_L10N_ID_TROPICO1_FR="${PKG_L10N_BASE_ID_TROPICO1}-fr"
PKG_L10N_ID_TROPICO1_EN="${PKG_L10N_BASE_ID_TROPICO1}-en"
PKG_L10N_ID_TROPICO2_FR="${PKG_L10N_BASE_ID_TROPICO2}-fr"
PKG_L10N_ID_TROPICO2_EN="${PKG_L10N_BASE_ID_TROPICO2}-en"
PKG_L10N_PROVIDES_TROPICO1="
$PKG_L10N_BASE_ID_TROPICO1"
PKG_L10N_PROVIDES_TROPICO2="
$PKG_L10N_BASE_ID_TROPICO2"
PKG_L10N_DESCRIPTION_FR='French localisation'
PKG_L10N_DESCRIPTION_EN='English localisation'
PKG_L10N_DESCRIPTION_TROPICO1_FR="$PKG_L10N_DESCRIPTION_FR"
PKG_L10N_DESCRIPTION_TROPICO1_EN="$PKG_L10N_DESCRIPTION_EN"
PKG_L10N_DESCRIPTION_TROPICO2_FR="$PKG_L10N_DESCRIPTION_FR"
PKG_L10N_DESCRIPTION_TROPICO2_EN="$PKG_L10N_DESCRIPTION_EN"

PKG_DATA_ID_TROPICO1="${GAME_ID_TROPICO1}-data"
PKG_DATA_ID_TROPICO2="${GAME_ID_TROPICO2}-data"
PKG_DATA_DESCRIPTION='data'

# Tropico 2 - Use software rendering by default.

REQUIREMENTS_LIST_TROPICO2='
dos2unix'

# 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

# Use uniform case for both games.

path_archive_contents=$(path_archive_contents)
path_reference="${path_archive_contents}/Maps"
path_alternative="${path_archive_contents}/maps"
if [ -e "$path_alternative" ]; then
	mv "$path_alternative" "$path_reference"
fi

# Tropico - Disable censorship in French version.

case "$(current_archive)" in
	('ARCHIVE_BASE_TROPICO1_FR_'*)
		# Force standard locale to avoid choking on some characters.
		# TODO: Extended expressions would make that easier to read.
		LANG=C sed --in-place \
			--expression='s/^\( \+406 .\+militaire\) \(.\+\)/\1  \2/' \
			"${path_archive_contents}/data2/Tropico.lng"
	;;
esac

# Tropico 2 - Use software rendering by default.

case "$(current_archive)" in
	('ARCHIVE_BASE_TROPICO2_'*)
		ini_file="${path_archive_contents}/Tropico2.ini"
		ini_field='SoftwareDevice'
		ini_value='1'
		sed_expression="s/^${ini_field}=.*$/${ini_field}=${ini_value}/"
		unix2dos --quiet "$ini_file"
		sed --in-place --expression="$sed_expression" "$ini_file"
		dos2unix --quiet "$ini_file"
	;;
esac

# Tropico 2 - Fix a file name encoding, to prevent a crash when selecting this map.

case "$(current_archive)" in
	('ARCHIVE_BASE_TROPICO2_FR_'*)
		if [ -e "${path_archive_contents}/Maps/6-Pičges en eaux troubles.{}" ]; then
			mv \
				"${path_archive_contents}/Maps/6-Pičges en eaux troubles.{}" \
				"${path_archive_contents}/Maps/6-Pièges en eaux troubles.{}"
			mv \
				"${path_archive_contents}/Maps/6-Pičges en eaux troubles.txt" \
				"${path_archive_contents}/Maps/6-Pièges en eaux troubles.txt"
		fi
	;;
esac

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Write launchers.

# Work around WINE_VIRTUAL_DESKTOP lack of context support.
# (./play.it 2.35)
WINE_VIRTUAL_DESKTOP=$(context_value 'WINE_VIRTUAL_DESKTOP')

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	ln --force --symbolic $(path_libraries)/* "\${PATH_PREFIX}"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
