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

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

script_version=20260325.6

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='pharaoh'
GAME_NAME='Pharaoh'

# Pharaoh + Cleopatra

ARCHIVE_BASE_0_NAME='setup_pharaoh_gold_2.1.0.15.exe'
ARCHIVE_BASE_0_MD5='62298f00f1f2268c8d5004f5b2e9fc93'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_SIZE='804849'
ARCHIVE_BASE_0_VERSION='2.1-gog2.1.0.15'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/pharaoh_cleopatra'

# Akhenaten
# cf. https://github.com/dalerank/Akhenaten

ARCHIVE_OPTIONAL_AKHENATEN_1_NAME='akhenaten.linux'
# In that order:
# - release 1638 (0.2.6)
# - release 1466 (0.2.5)
# - release 1036
# - release 784
# - release 205
ARCHIVE_OPTIONAL_AKHENATEN_1_MD5='
b670df5972c9d189f2eebf1df5f2bc96
a7e38f4e0863065754817285e193752b
d35934a08420b82d55ed41405d5bea61
eebce8cd3a284399d271b1f9e2b02461
cdd40aa1ab541035b1a2c5e02cbe5064'
ARCHIVE_OPTIONAL_AKHENATEN_1_SIZE='177630'
ARCHIVE_OPTIONAL_AKHENATEN_1_URL='https://dalerank.itch.io/akhenaten'

# release 108 (still called "Ozyamandias")
ARCHIVE_OPTIONAL_AKHENATEN_0_NAME='ozymandias.linux'
ARCHIVE_OPTIONAL_AKHENATEN_0_MD5='7e52e2e90f5b00069cd312507d55c518'
ARCHIVE_OPTIONAL_AKHENATEN_0_SIZE='31820'

CONTENT_PATH_DEFAULT='app'
CONTENT_GAME_BIN_WINE_FILES='
Pharaoh.exe
Pharaoh.ini'
# Pharaoh.exe requirements
CONTENT_LIBS_BIN_WINE_FILES='
smackw32.dll
mss32.dll
binkw32.dll'
CONTENT_GAME0_BIN_WINE_FILES='
MP3DEC.ASI
mssb16.tsk
*.M3D'
CONTENT_GAME_DATA_FILES='
AUDIO
BINKS
Data
Maps
auto reason phrases.txt
campaign.txt
eventmsg.txt
Figure_model.txt
Figure_model_*.txt
music.txt
Pharaoh_MM.txt
Pharaoh_Model_*.txt
Pharaoh_Text.txt
Tax_Sentiment_Model_*.txt
trade_recommends.txt
Pharaoh2.emp
Pharaoh_MM.eng
Pharaoh_Text.eng
mission1.pak
*.inf'
CONTENT_DOC_DATA_FILES='
Mission Editor Guide.txt
Readme.txt
*.pdf'

USER_PERSISTENT_DIRECTORIES='
Save'
USER_PERSISTENT_FILES='
*.ini'

# WINE - Without a virtual desktop, the game menu is rendered as a black screen after the introduction movie.
# https://bugs.winehq.org/show_bug.cgi?id=59562
# (WINE 11.5)
WINE_VIRTUAL_DESKTOP='auto'

APP_MAIN_EXE_BIN_LINUX='akhenaten.linux'
APP_MAIN_EXE_BIN_WINE='Pharaoh.exe'
APP_MAIN_ICON='cleoicon.ico'

PACKAGES_LIST='
PKG_BIN_WINE
PKG_DATA'
PACKAGES_LIST_AKHENATEN="
PKG_BIN_LINUX
$PACKAGES_LIST"

PKG_BIN_ID="$GAME_ID"
PKG_BIN_PROVIDES="
$PKG_BIN_ID"
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_BIN_LINUX_ID="${PKG_BIN_ID}-akhenaten"
PKG_BIN_LINUX_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_LINUX_DESCRIPTION='using the Akhenaten engine'
PKG_BIN_LINUX_ARCH='64'
PKG_BIN_LINUX_DEPENDENCIES_SIBLINGS="$PKG_BIN_DEPENDENCIES_SIBLINGS"
PKG_BIN_LINUX_DEPENDENCIES_LIBRARIES='
libc.so.6
libm.so.6
libSDL2-2.0.so.0
libSDL2_mixer-2.0.so.0
libz.so.1'
# Easier upgrades from pre-20231105.3 packages
PKG_BIN_LINUX_PROVIDES="$PKG_BIN_LINUX_PROVIDES
pharaoh-ozymandias"

PKG_BIN_WINE_ID="${PKG_BIN_ID}-wine"
PKG_BIN_WINE_PROVIDES="$PKG_BIN_PROVIDES"
PKG_BIN_WINE_DESCRIPTION='using WINE'
PKG_BIN_WINE_ARCH='32'
PKG_BIN_WINE_DEPENDENCIES_SIBLINGS="$PKG_BIN_DEPENDENCIES_SIBLINGS"

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='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"

# Check for the presence of the Akhenaten native engine binary (optional).

archive_initialize_optional \
	'ARCHIVE_AKHENATEN' \
	'ARCHIVE_OPTIONAL_AKHENATEN_1' \
	'ARCHIVE_OPTIONAL_AKHENATEN_0'
if archive_is_available 'ARCHIVE_AKHENATEN'; then
	export PACKAGES_LIST="$PACKAGES_LIST_AKHENATEN"
fi

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default
if archive_is_available 'ARCHIVE_AKHENATEN'; then
	application_exe=$(
		set_current_package 'PKG_BIN_LINUX'
		application_exe 'APP_MAIN'
	)
	install -D --mode=755 \
		"$(archive_path 'ARCHIVE_AKHENATEN')" \
		"$(package_path 'PKG_BIN_LINUX')$(path_game_data)/${application_exe}"
fi

# Write launchers.

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

launchers_generation 'PKG_BIN_WINE'
if archive_is_available 'ARCHIVE_AKHENATEN'; then
	launchers_generation 'PKG_BIN_LINUX'
fi

# Build packages.

packages_generation
if archive_is_available 'ARCHIVE_AKHENATEN'; then
	printf '\n'
	printf 'Akhenaten:'
	print_instructions 'PKG_DATA' 'PKG_BIN_LINUX'
	printf 'WINE:'
	print_instructions 'PKG_DATA' 'PKG_BIN_WINE'
else
	print_instructions
fi

# Clean up.

working_directory_cleanup

exit 0
