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

###
# Fallout: New Vegas
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260708.4

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=fallout-new-vegas
GAME_NAME='Fallout: New Vegas'

ARCHIVE_BASE_EN_1_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(55068).exe'
ARCHIVE_BASE_EN_1_MD5='
54d7878228b86284d360e8672ce5e85c
2381656fd552e5b93c143336fedbc21d'
ARCHIVE_BASE_EN_1_TYPE=innosetup
ARCHIVE_BASE_EN_1_PART1_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(55068)-1.bin'
ARCHIVE_BASE_EN_1_PART1_MD5='
7ce668d5d084250b7ea445447b05d9b1
e0979d7e1d0145cd7bb6d1ad62419a17'
ARCHIVE_BASE_EN_1_PART2_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(55068)-2.bin'
ARCHIVE_BASE_EN_1_PART2_MD5='
6d86a620514632e5d8c453e27e32dce1
730ae6ea0eb5d41773be6bb19bb15d57'
ARCHIVE_BASE_EN_1_SIZE=9879738
ARCHIVE_BASE_EN_1_VERSION=1.4.0.525-2

ARCHIVE_BASE_FR_1_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(french)_(55068).exe'
ARCHIVE_BASE_FR_1_MD5='
301a3885d574a88ebbe8147ce8f28cd0
841d652c40c7c4bb54830ba768bf48a7'
ARCHIVE_BASE_FR_1_TYPE=innosetup
ARCHIVE_BASE_FR_1_PART1_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(french)_(55068)-1.bin'
ARCHIVE_BASE_FR_1_PART1_MD5='
58eb0e07dc5eb8c1f1bc78dbfa1f9cfb
054a674e5246f8b8c78c02f3ee2095dc'
ARCHIVE_BASE_FR_1_PART2_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(french)_(55068)-2.bin'
ARCHIVE_BASE_FR_1_PART2_MD5='
a3d4bbfd30d2d60389ed2d2b4b5aed07
e57e233aea17d8f4ee23bf90d0318a38'
# TODO: Check the 3rd part integrity if it is available, skip it if it is missing.
# Only one of the 55068 builds comes with 3 parts, the other only include 2.
#ARCHIVE_BASE_FR_1_PART3_NAME='setup_fallout_new_vegas_1.4.0.525(a)_(french)_(55068)-3.bin'
#ARCHIVE_BASE_FR_1_PART3_MD5=c2020bf115acc116204e43ca09a58ef0
ARCHIVE_BASE_FR_1_SIZE=9999844
ARCHIVE_BASE_FR_1_VERSION=1.4.0.525-2

ARCHIVE_BASE_EN_0_NAME='setup_fallout_new_vegas_1.4.0.525_(12010).exe'
ARCHIVE_BASE_EN_0_MD5=be32894fe423302d299fa532e5641079
ARCHIVE_BASE_EN_0_TYPE=innosetup
ARCHIVE_BASE_EN_0_PART1_NAME='setup_fallout_new_vegas_1.4.0.525_(12010)-1.bin'
ARCHIVE_BASE_EN_0_PART1_MD5=245661b2e1435c530763ae281ccecd9f
ARCHIVE_BASE_EN_0_PART2_NAME='setup_fallout_new_vegas_1.4.0.525_(12010)-2.bin'
ARCHIVE_BASE_EN_0_PART2_MD5=705e7097b9c18836118c2e9eb42b19ed
ARCHIVE_BASE_EN_0_SIZE=11000000
ARCHIVE_BASE_EN_0_VERSION=1.4.0.525-1

ARCHIVE_BASE_FR_0_NAME='setup_fallout_new_vegas_french_1.4.0.525_(12010).exe'
ARCHIVE_BASE_FR_0_MD5=da79e8756efb16b211a76756cd8865b3
ARCHIVE_BASE_FR_0_TYPE=innosetup
ARCHIVE_BASE_FR_0_PART1_NAME='setup_fallout_new_vegas_french_1.4.0.525_(12010)-1.bin'
ARCHIVE_BASE_FR_0_PART1_MD5=245661b2e1435c530763ae281ccecd9f
ARCHIVE_BASE_FR_0_PART2_NAME='setup_fallout_new_vegas_french_1.4.0.525_(12010)-2.bin'
ARCHIVE_BASE_FR_0_PART2_MD5=e148a49b1bbcfa4b2662e45691ae606e
ARCHIVE_BASE_FR_0_SIZE=11000000
ARCHIVE_BASE_FR_0_VERSION=1.4.0.525-1

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_EN_0='app'
CONTENT_PATH_DEFAULT_FR_0='app'
CONTENT_GAME_MAIN_FILES='
FalloutNV.exe
Geck.exe
low.ini
medium.ini
high.ini
VeryHigh.ini
MainTitle.wav'
CONTENT_GAME0_MAIN_FILES='
Data/Music
Data/Shaders
Data/Sound
Data/Fallout - Meshes.bsa
Data/Fallout - Misc.bsa
Data/Fallout - Sound.bsa
Data/Fallout - Textures2.bsa
Data/Fallout - Textures.bsa
Data/Update.bsa
Data/FalloutNV_lang.esp'
# TODO: Are the icons required?
CONTENT_GAME2_MAIN_FILES='
FalloutNV.ico
GECKicon.ico'
# FalloutNV.exe requirements
CONTENT_LIBS_MAIN_FILES='
binkw32.dll
libvorbisfile.dll
libvorbis.dll'
# FalloutNVLauncher.exe requirements
CONTENT_LIBS0_MAIN_FILES='
GalaxyWrp.dll
Galaxy.dll'
# Geck.exe requirements
CONTENT_LIBS1_MAIN_FILES='
SSCE5432.dll'
CONTENT_GAME_L10N_FILES='
FalloutNVLauncher.exe
Fallout_default.ini
Data/Credits.txt
Data/CreditsWacky.txt
Data/Fallout - Voices1.bsa
Data/FalloutNV.esm'
# TODO: Move movies to a dedicated package.
CONTENT_GAME0_L10N_FILES='
Data/Video'
CONTENT_GAME_DLC1_FILES='
Data/DeadMoney*'
CONTENT_GAME_DLC2_FILES='
Data/HonestHearts*'
CONTENT_GAME_DLC3_FILES='
Data/OldWorldBlues*'
CONTENT_GAME_DLC4_FILES='
Data/LonesomeRoad*'
CONTENT_GAME_DLC5_FILES='
Data/GunRunnersArsenal*'
CONTENT_GAME_DLC6_FILES='
Data/CaravanPack*'
CONTENT_GAME_DLC7_FILES='
Data/ClassicPack*'
CONTENT_GAME_DLC8_FILES='
Data/MercenaryPack*'
CONTENT_GAME_DLC9_FILES='
Data/TribalPack*'
CONTENT_DOC_L10N_FILES='
Readme.txt
GECKReadme.txt'

WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/FalloutNV'
# Set required registry key.
installed_path=C:\\\\${GAME_ID}/
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\Bethesda Softworks\FalloutNV]
"Installed Path"="'${installed_path}'"'

# TODO: Can the launcher be skipped?
APP_MAIN_EXE='FalloutNVLauncher.exe'
APP_MAIN_ICON='FalloutNV.ico'

# TODO: Move the editor to a dedicated package.
APP_EDITOR_ID=${GAME_ID}-editor
APP_EDITOR_NAME="${GAME_NAME} - Editor"
APP_EDITOR_EXE='Geck.exe'
APP_EDITOR_ICON='GECKicon.ico'

PACKAGES_LIST='
PKG_MAIN
PKG_L10N
PKG_DLC1
PKG_DLC2
PKG_DLC3
PKG_DLC4
PKG_DLC5
PKG_DLC6
PKG_DLC7
PKG_DLC8
PKG_DLC9'

PKG_MAIN_ARCH=32
PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE'
PKG_MAIN_DEPENDENCIES_GSTREAMER_PLUGINS='
application/x-id3'
# Easier upgrades from pre-20260708.2 packages
PKG_MAIN_PROVIDES='
fallout-new-vegas-data'

PKG_L10N_BASE_ID=${GAME_ID}-l10n
PKG_L10N_ID_FR=${PKG_L10N_BASE_ID}-fr
PKG_L10N_ID_EN=${PKG_L10N_BASE_ID}-en
PKG_L10N_PROVIDES=$PKG_L10N_BASE_ID
PKG_L10N_DESCRIPTION_FR='French localisation'
PKG_L10N_DESCRIPTION_EN='English localisation'

PKG_DLC1_ID=${GAME_ID}-dead-money
PKG_DLC2_ID=${GAME_ID}-honest-hearts
PKG_DLC3_ID=${GAME_ID}-old-world-blues
PKG_DLC4_ID=${GAME_ID}-lonesome-road
PKG_DLC5_ID=${GAME_ID}-gun-runners-arsenal
PKG_DLC6_ID=${GAME_ID}-caravan-pack
PKG_DLC7_ID=${GAME_ID}-classic-pack
PKG_DLC8_ID=${GAME_ID}-mercenary-pack
PKG_DLC9_ID=${GAME_ID}-tribal-pack
PKG_DLC1_ID_FR=${PKG_DLC1_ID}-fr
PKG_DLC2_ID_FR=${PKG_DLC2_ID}-fr
PKG_DLC3_ID_FR=${PKG_DLC3_ID}-fr
PKG_DLC4_ID_FR=${PKG_DLC4_ID}-fr
PKG_DLC5_ID_FR=${PKG_DLC5_ID}-fr
PKG_DLC6_ID_FR=${PKG_DLC6_ID}-fr
PKG_DLC7_ID_FR=${PKG_DLC7_ID}-fr
PKG_DLC8_ID_FR=${PKG_DLC8_ID}-fr
PKG_DLC9_ID_FR=${PKG_DLC9_ID}-fr
PKG_DLC1_ID_EN=${PKG_DLC1_ID}-en
PKG_DLC2_ID_EN=${PKG_DLC2_ID}-en
PKG_DLC3_ID_EN=${PKG_DLC3_ID}-en
PKG_DLC4_ID_EN=${PKG_DLC4_ID}-en
PKG_DLC5_ID_EN=${PKG_DLC5_ID}-en
PKG_DLC6_ID_EN=${PKG_DLC6_ID}-en
PKG_DLC7_ID_EN=${PKG_DLC7_ID}-en
PKG_DLC8_ID_EN=${PKG_DLC8_ID}-en
PKG_DLC9_ID_EN=${PKG_DLC9_ID}-en
PKG_DLC1_PROVIDES=$PKG_DLC1_ID
PKG_DLC2_PROVIDES=$PKG_DLC2_ID
PKG_DLC3_PROVIDES=$PKG_DLC3_ID
PKG_DLC4_PROVIDES=$PKG_DLC4_ID
PKG_DLC5_PROVIDES=$PKG_DLC5_ID
PKG_DLC6_PROVIDES=$PKG_DLC6_ID
PKG_DLC7_PROVIDES=$PKG_DLC7_ID
PKG_DLC8_PROVIDES=$PKG_DLC8_ID
PKG_DLC9_PROVIDES=$PKG_DLC9_ID
PKG_DLC1_DESCRIPTION='Dead Money'
PKG_DLC2_DESCRIPTION='Honest Hearts'
PKG_DLC3_DESCRIPTION='Old World Blues'
PKG_DLC4_DESCRIPTION='Lonesome Road'
PKG_DLC5_DESCRIPTION='Gun Runnersʼ Arsenal'
PKG_DLC6_DESCRIPTION='Caravan Pack'
PKG_DLC7_DESCRIPTION='Classic Pack'
PKG_DLC8_DESCRIPTION='Mercenary Pack'
PKG_DLC9_DESCRIPTION='Tribal Pack'
PKG_DLC_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_L10N'
PKG_DLC1_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC2_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC3_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC4_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC5_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC6_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC7_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC8_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
PKG_DLC9_DEPENDENCIES_SIBLINGS=$PKG_DLC_DEPENDENCIES_SIBLINGS
# Easier upgrades from pre-20260708.1 packages
PKG_DLC1_PROVIDES="${PKG_DLC1_PROVIDES}
fallout-new-vegas-dlc-dead-money"
PKG_DLC2_PROVIDES="${PKG_DLC2_PROVIDES}
fallout-new-vegas-dlc-honest-hearts"
PKG_DLC3_PROVIDES="${PKG_DLC3_PROVIDES}
fallout-new-vegas-dlc-old-world-blues"
PKG_DLC4_PROVIDES="${PKG_DLC4_PROVIDES}
fallout-new-vegas-dlc-lonesome-road"
PKG_DLC5_PROVIDES="${PKG_DLC5_PROVIDES}
fallout-new-vegas-dlc-gun-runners-arsenal"
PKG_DLC6_PROVIDES="${PKG_DLC6_PROVIDES}
fallout-new-vegas-dlc-caravan-pack"
PKG_DLC7_PROVIDES="${PKG_DLC7_PROVIDES}
fallout-new-vegas-dlc-classic-pack"
PKG_DLC8_PROVIDES="${PKG_DLC8_PROVIDES}
fallout-new-vegas-dlc-mercenary-pack"
PKG_DLC9_PROVIDES="${PKG_DLC9_PROVIDES}
fallout-new-vegas-dlc-tribal-pack"

# 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 "$@"

# 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
print_instructions PKG_MAIN PKG_L10N
(
	GAME_NAME=$PKG_DLC1_DESCRIPTION
	set_current_package PKG_DLC1
	print_instructions PKG_DLC1
)
(
	GAME_NAME=$PKG_DLC2_DESCRIPTION
	set_current_package PKG_DLC2
	print_instructions PKG_DLC2
)
(
	GAME_NAME=$PKG_DLC3_DESCRIPTION
	set_current_package PKG_DLC3
	print_instructions PKG_DLC3
)
(
	GAME_NAME=$PKG_DLC4_DESCRIPTION
	set_current_package PKG_DLC4
	print_instructions PKG_DLC4
)
(
	GAME_NAME=$PKG_DLC5_DESCRIPTION
	set_current_package PKG_DLC5
	print_instructions PKG_DLC5
)
(
	GAME_NAME=$PKG_DLC6_DESCRIPTION
	set_current_package PKG_DLC6
	print_instructions PKG_DLC6
)
(
	GAME_NAME=$PKG_DLC7_DESCRIPTION
	set_current_package PKG_DLC7
	print_instructions PKG_DLC7
)
(
	GAME_NAME=$PKG_DLC8_DESCRIPTION
	set_current_package PKG_DLC8
	print_instructions PKG_DLC8
)
(
	GAME_NAME=$PKG_DLC9_DESCRIPTION
	set_current_package PKG_DLC9
	print_instructions PKG_DLC9
)

# Clean up.

working_directory_cleanup

exit 0
