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

###
# Dragon Age: Origins
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260709.3

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=dragon-age-origins
GAME_NAME='Dragon Age: Origins'

ARCHIVE_BASE_2_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205).exe'
ARCHIVE_BASE_2_MD5=18e2e1f3fba4184ca15d6170d0a2ce51
ARCHIVE_BASE_2_TYPE=innosetup
ARCHIVE_BASE_2_PART1_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-1.bin'
ARCHIVE_BASE_2_PART1_MD5=0735b7a919fe9b1a7e7d31cfea156ee5
ARCHIVE_BASE_2_PART2_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-2.bin'
ARCHIVE_BASE_2_PART2_MD5=c7f59542be194793d3f1664b2b079d64
ARCHIVE_BASE_2_PART3_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-3.bin'
ARCHIVE_BASE_2_PART3_MD5=87ef8829f56c5b985b7f3eb1893249de
ARCHIVE_BASE_2_PART4_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-4.bin'
ARCHIVE_BASE_2_PART4_MD5=04f49cccc87d268fef2a7a4cafce27e8
ARCHIVE_BASE_2_PART5_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-5.bin'
ARCHIVE_BASE_2_PART5_MD5=e44d862bd7221d065be30cd80de4d568
ARCHIVE_BASE_2_PART6_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.8_(78205)-6.bin'
ARCHIVE_BASE_2_PART6_MD5=e97793771570a38174e202704297fe37
ARCHIVE_BASE_2_SIZE=38342773
ARCHIVE_BASE_2_VERSION=1.05-3

ARCHIVE_BASE_1_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885).exe'
ARCHIVE_BASE_1_MD5=fa14b944ca5ce04e462afe61e932f1b0
ARCHIVE_BASE_1_TYPE=innosetup
ARCHIVE_BASE_1_PART1_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-1.bin'
ARCHIVE_BASE_1_PART1_MD5=910ff0ff7db44dd44c9b7fbc01439226
ARCHIVE_BASE_1_PART2_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-2.bin'
ARCHIVE_BASE_1_PART2_MD5=317484fbbe084fdfac86af933a3827d3
ARCHIVE_BASE_1_PART3_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-3.bin'
ARCHIVE_BASE_1_PART3_MD5=c7a236056afe1e24070e747760cab8d9
ARCHIVE_BASE_1_PART4_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-4.bin'
ARCHIVE_BASE_1_PART4_MD5=0b08303e31e29708cc679d92b5b9c607
ARCHIVE_BASE_1_PART5_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-5.bin'
ARCHIVE_BASE_1_PART5_MD5=dead382df73a84c02ee54a5b34050fcc
ARCHIVE_BASE_1_PART6_NAME='setup_dragon_age_origins_-_ultimate_edition_1.05_gog_0.5_(76885)-6.bin'
ARCHIVE_BASE_1_PART6_MD5=9c94a20453a52d5284c89aa4507949e0
ARCHIVE_BASE_1_SIZE=38343327
ARCHIVE_BASE_1_VERSION=1.05-2

ARCHIVE_BASE_0_NAME='setup_dragon_age_origins_ultimate_2.1.0.4.exe'
ARCHIVE_BASE_0_MD5=2bfdbc94523ef4c21476f64ef8029479
ARCHIVE_BASE_0_EXTRACTOR=innoextract
ARCHIVE_BASE_0_EXTRACTOR_OPTIONS='--gog'
ARCHIVE_BASE_0_PART1_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-1.bin'
ARCHIVE_BASE_0_PART1_MD5=b6e68b1b3b11fdddea809a5f11368036
ARCHIVE_BASE_0_PART1_EXTRACTOR=unar
ARCHIVE_BASE_0_PART2_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-2.bin'
ARCHIVE_BASE_0_PART2_MD5=71d813d6827941a90422a40088d64b78
ARCHIVE_BASE_0_PART3_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-3.bin'
ARCHIVE_BASE_0_PART3_MD5=2ff9cc2bb41435429ee6277106a6a568
ARCHIVE_BASE_0_PART4_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-4.bin'
ARCHIVE_BASE_0_PART4_MD5=a25c58b43a2e468fcf72446f57542115
ARCHIVE_BASE_0_PART5_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-5.bin'
ARCHIVE_BASE_0_PART5_MD5=4ce5f6dceb01c9a1fc85c759c436b7b2
ARCHIVE_BASE_0_PART6_NAME='setup_dragon_age_origins_ultimate_2.1.0.4-6.bin'
ARCHIVE_BASE_0_PART6_MD5=e2d13b236af30f210e0eb65aec5d137e
ARCHIVE_BASE_0_SIZE=39000000
ARCHIVE_BASE_0_VERSION=1.05-1

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_0='game'
# TODO: Move Awakening expansion to a dedicated package.
CONTENT_GAME_MAIN_FILES='
bin_ship/DAOrigins.exe
bin_ship/DAOriginsConfig.exe
data
modules
Offers
packages'
# TODO: Is the launcher binary required?
CONTENT_GAME0_MAIN_FILES='
DAOriginsLauncher.exe'
# TODO: Are the updater binaries required?
CONTENT_GAME1_MAIN_FILES='
bin_ship/DAUpdater.exe
bin_ship/DAUpdaterSvc.Service.exe'
# TODO: Drop unused libraries.
# TODO: Install libraries in a standard path.
CONTENT_GAME2_MAIN_FILES='
bin_ship/binkw32.dll
bin_ship/DAOriginsGDF.dll
bin_ship/fmod_event.dll
bin_ship/fmodex.dll
bin_ship/icsharpcode.sharpziplib.dll
bin_ship/msvcp71.dll
bin_ship/msvcr71.dll
bin_ship/nxcharacter.dll
bin_ship/nxcooking.dll
bin_ship/speedtreert.dll
bin_ship/umbra.dll
bin_ship/unrar.dll'
# TODO: Is the updater library required?
CONTENT_GAME3_MAIN_FILES='
bin_ship/DAUpdater.Engine.dll'
CONTENT_GAME_L10N_FR_FILES='
Offers/*/audio/vo/fr-fr
packages/*/audio/vo/fr-fr
modules/*/audio/vo/fr-fr
modules/*/locale/fr-fr'
CONTENT_GAME_L10N_EN_FILES='
Offers/*/audio/vo/en-us
packages/*/audio/vo/en-us
modules/*/audio/vo/en-us
modules/*/locale/en-us'
CONTENT_GAME0_L10N_FR_FILES='
Offers/*/talktables/*_fr-fr*.tlk
packages/*/talktables/*_fr-fr*.tlk
modules/*/talktables/*_fr-fr*.tlk'
CONTENT_GAME0_L10N_EN_FILES='
Offers/*/talktables/*_en-us*.tlk
packages/*/talktables/*_en-us*.tlk
modules/*/talktables/*_en-us*.tlk'
CONTENT_GAME_ADDONS_FILES='
Addins/*/core/data
Addins/*/core/env
Addins/*/core/override
Addins/*/core/textures
Addins/*/module/data/coreep1
Addins/*/module/env
Addins/*/module/patch'
CONTENT_GAME0_ADDONS_FILES='
Addins/*/module/audio/sound
Addins/*/talktables/*_pseudo*.tlk'
CONTENT_GAME_ADDONS_L10N_FR_FILES='
Addins/*/module/audio/vo/fr-fr
Addins/*/talktables/*_fr-fr*.tlk'
CONTENT_GAME_ADDONS_L10N_EN_FILES='
Addins/*/module/audio/vo/en-us
Addins/*/talktables/*_en-us*.tlk'
CONTENT_DOC_MAIN_FILES='
manual.pdf'
# TODO: This should be split into localisation packages.
CONTENT_DOC0_MAIN_RELATIVE_PATH='docs'
CONTENT_DOC0_MAIN_FILES='
*.rtf
*.txt'
CONTENT_SETTINGS_RELATIVE_PATH='__support/userdocs'
CONTENT_SETTINGS_RELATIVE_PATH_0='../userdocs/bioware/dragon age/settings'
CONTENT_SETTINGS_FILES='
Addins.xml'
CONTENT_PHYSX_RELATIVE_PATH='redist'
CONTENT_PHYSX_FILES='
PhysX_9.09.0408_SystemSoftware.exe'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/BioWare/Dragon Age'

APP_MAIN_EXE='bin_ship/DAOrigins.exe'

APP_CONFIG_ID=${GAME_ID}-config
APP_CONFIG_NAME="$GAME_NAME - Configuration"
APP_CONFIG_CAT='Settings'
APP_CONFIG_EXE='bin_ship/DAOriginsConfig.exe'

# TODO: Improve files lists so PKG_MAIN can be set first.
PACKAGES_LIST='
PKG_L10N_FR
PKG_L10N_EN
PKG_MAIN
PKG_ADDONS
PKG_ADDONS_L10N_FR
PKG_ADDONS_L10N_EN'

PKG_MAIN_ARCH=32
PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N'
# Easier upgrades from pre-20260709.1 packages
PKG_MAIN_PROVIDES='
dragon-age-origins-data
dragon-age-origins-environment
dragon-age-origins-movies'

PKG_L10N_ID=${GAME_ID}-l10n
PKG_L10N_FR_ID=${PKG_L10N_ID}-fr
PKG_L10N_EN_ID=${PKG_L10N_ID}-en
PKG_L10N_PROVIDES=$PKG_L10N_ID
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'
# Easier upgrades from pre-20260708.1 packages
PKG_L10N_PROVIDES="$PKG_L10N_PROVIDES
dragon-age-origins-l10n-text
dragon-age-origins-l10n-voices"
PKG_L10N_FR_PROVIDES=$PKG_L10N_PROVIDES
PKG_L10N_EN_PROVIDES=$PKG_L10N_PROVIDES

PKG_ADDONS_ID=${GAME_ID}-addons
PKG_ADDONS_DESCRIPTION='Add-on content'
PKG_ADDONS_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_ADDONS_L10N'

PKG_ADDONS_L10N_ID=${PKG_ADDONS_ID}-l10n
PKG_ADDONS_L10N_FR_ID=${PKG_ADDONS_L10N_ID}-fr
PKG_ADDONS_L10N_EN_ID=${PKG_ADDONS_L10N_ID}-en
PKG_ADDONS_L10N_PROVIDES=$PKG_ADDONS_L10N_ID
PKG_ADDONS_L10N_FR_PROVIDES=$PKG_ADDONS_L10N_PROVIDES
PKG_ADDONS_L10N_EN_PROVIDES=$PKG_ADDONS_L10N_PROVIDES
PKG_ADDONS_L10N_FR_DESCRIPTION="${PKG_ADDONS_DESCRIPTION} - French localisation"
PKG_ADDONS_L10N_EN_DESCRIPTION="${PKG_ADDONS_DESCRIPTION} - English localisation"
PKG_ADDONS_L10N_FR_DEPENDENCIES_SIBLINGS='
PKG_L10N_FR'
PKG_ADDONS_L10N_EN_DEPENDENCIES_SIBLINGS='
PKG_L10N_EN'

# 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
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"
set_default_package 'PKG_MAIN'

# Extract game data.

archive_extraction_default

# Drop extra localisations.

path_archive_contents=$(path_archive_contents)
find "${path_archive_contents}" \( \
	-name cs-cz -o \
	-name de-de -o \
	-name es-es -o \
	-name hu-hu -o \
	-name it-it -o \
	-name pl-pl -o \
	-name ru-ru \
\) -exec rm --recursive {} +
find "${path_archive_contents}" \( \
	-name \*cs-cz.tlk -o \
	-name \*cs-cz_f.tlk -o \
	-name \*de-de.tlk -o \
	-name \*de-de_f.tlk -o \
	-name \*es-es.tlk -o \
	-name \*es-es_f.tlk -o \
	-name \*hu-hu.tlk -o \
	-name \*hu-hu_f.tlk -o \
	-name \*it-it.tlk -o \
	-name \*it-it_f.tlk -o \
	-name \*pl-pl.tlk -o \
	-name \*pl-pl_f.tlk -o \
	-name \*ru-ru.tlk -o \
	-name \*ru-ru_f.tlk \
\) -delete
find "${path_archive_contents}" -type d -empty -delete

# Include game data.

content_inclusion_icons
content_inclusion SETTINGS PKG_MAIN "$(path_game_data)/settings"
content_inclusion PHYSX PKG_MAIN "$(path_game_data)/physx"
content_inclusion_default

# Write launchers.

# - Include shipped settings, to unlock expansions.
# - Install shipped build of PhysX.
wineprefix_init_custom() {
	cat <<- 'EOF'
	    # Include shipped settings, to unlock expansions.
	    settings_path="${PATH_PERSISTENT}/wineprefix/users/${USER}/Documents/BioWare/Dragon Age/Settings"
	    if [ ! -e "$settings_path" ]; then
	        mkdir --parents "$(dirname "$settings_path")"
	        cp --recursive "${PATH_GAME_DATA}/settings" "$settings_path"
	    fi
	EOF
	cat <<- EOF
	    # Install shipped build of PhysX.
	    (
	        # env --chdir does not work here, it leads to the following error:
	        # Corrupt installation detected.
	        cd "\${WINEPREFIX}/drive_c/${GAME_ID}"
	        "\$(wine_command)" 'physx/PhysX_9.09.0408_SystemSoftware.exe'
	    )
	EOF
}

launchers_generation

# Build packages.

packages_generation
case "$(messages_language)" in
	('fr')
		lang_string='version %s :'
		lang_fr='française'
		lang_en='anglaise'
	;;
	('en'|*)
		lang_string='%s version:'
		lang_fr='French'
		lang_en='English'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions PKG_MAIN PKG_L10N_FR PKG_ADDONS PKG_ADDONS_L10N_FR
printf "$lang_string" "$lang_en"
print_instructions PKG_MAIN PKG_L10N_EN PKG_ADDONS PKG_ADDONS_L10N_EN

# Clean up.

working_directory_cleanup

exit 0
