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

###
# Finale Fantasy 7
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260612.6

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='final-fantasy-7'
GAME_NAME='Final Fantasy VII'

ARCHIVE_BASE_0_NAME='setup_final_fantasy_vii_2.0_gog_v1_(88522).exe'
ARCHIVE_BASE_0_MD5='c70dffd6321e5f7f199c39aa7a4b62ff'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_final_fantasy_vii_2.0_gog_v1_(88522)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='bf1f6e6178c83672292efd5e3524b9ab'
ARCHIVE_BASE_0_SIZE='3782949'
ARCHIVE_BASE_0_VERSION='2.0-1'

CONTENT_PATH_DEFAULT='.'
# Native Windows libraries
# TODO: Check if all libraries are required.
CONTENT_LIBS_MAIN_FILES='
SDL2.dll
xaudio2_9redist.dll'
# TODO: Check if the following libraries can be moved to localisation packages.
CONTENT_LIBS0_MAIN_FILES='
ff7/resources/ff7_1.02/ff7_en
ff7/resources/ff7_1.02/ff7_ja'
# TODO: Check if the Galaxy library is required.
CONTENT_LIBS1_MAIN_FILES='
Galaxy64.dll'
# TODO: Check if the Steam library is required.
CONTENT_LIBS2_MAIN_FILES='
steam_api64.dll'
CONTENT_GAME_MAIN_FILES='
FFVII.exe
Assets
ff7/font
ff7/Shaders
ff7/workingdir/data/battle
ff7/workingdir/data/cd
ff7/workingdir/data/field
ff7/workingdir/data/layout_pc
ff7/workingdir/data/menu
ff7/workingdir/data/midi
ff7/workingdir/data/minigame
ff7/workingdir/data/movies
ff7/workingdir/data/music
ff7/workingdir/data/music_ogg
ff7/workingdir/data/png
ff7/workingdir/data/sound
ff7/workingdir/data/wm
ff7/workingdir/ff7input.cfg'
# TODO: Check if Galaxy settings are required.
CONTENT_GAME0_MAIN_FILES='
GalaxyConfig.json'
# TODO: Check if GOG metadata is required.
CONTENT_GAME1_MAIN_FILES='
goggame-1698970154.info'
CONTENT_GAME_L10N_FR_FILES='
be_loc/be_loc_fr.xml
fr
ff7/workingdir/data/lang-fr'
CONTENT_GAME_L10N_JA_FILES='
be_loc/be_loc_ja.xml
ja
ff7/workingdir/data/lang-ja'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/AppData/Local/FINAL FANTASY VII GOG Edition'

APP_MAIN_EXE='FFVII.exe'
# Set game language based on the installed localisation.
APP_MAIN_PRERUN='
# Set game language based on the installed localisation.
case $(ls be_loc/be_loc_??.xml) in
	(be_loc/be_loc_fr.xml)
		LC_ALL=fr_FR
	;;
	(be_loc/be_loc_ja.xml)
		LC_ALL=ja_JP
	;;
esac
export LC_ALL
'

PACKAGES_LIST='
PKG_MAIN
PKG_L10N_FR
PKG_L10N_JA'

PKG_MAIN_ARCH='64'
PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N'

PKG_L10N_ID="${GAME_ID}-l10n"
PKG_L10N_FR_ID="${PKG_L10N_ID}-fr"
PKG_L10N_JA_ID="${PKG_L10N_ID}-ja"
PKG_L10N_PROVIDES="
$PKG_L10N_ID"
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_JA_DESCRIPTION='Japanese 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
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" -type f | while read -r dll_source; do
		dll_destination="${PATH_PREFIX}${dll_source#"${PATH_LIBRARIES_SYSTEM}"}"
	    mkdir --parents "$(dirname "$dll_destination")"
	    ln --symbolic --force "$dll_source" "$dll_destination"
	done
	EOF
}

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		lang_string='traduction %s :'
		lang_fr='française'
		lang_ja='japonaise'
	;;
	('en'|*)
		lang_string='%s localisation:'
		lang_fr='French'
		lang_ja='Japanese'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions 'PKG_MAIN' 'PKG_L10N_FR'
printf "$lang_string" "$lang_ja"
print_instructions 'PKG_MAIN' 'PKG_L10N_JA'

# Clean up.

working_directory_cleanup

exit 0
