#!/bin/sh
# SPDX-License-Identifier: ACSL
# SPDX-FileCopyrightText: © 2022 Mopi
# SPDX-FileCopyrightText: © 2022 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2024 Fabien Givors <captnfab@debian-facile.org>
# SPDX-FileCopyrightText: © 2025 Christian Stalp
set -o errexit

###
# The Elder Scrolls 5: Skyrim
# send your bug reports to bugs@dotslashplay.it
###

script_version=20251227.2

PLAYIT_COMPATIBILITY_LEVEL='2.34'

GAME_ID='the-elder-scrolls-5-skyrim'
GAME_NAME='The Elder Scrolls V: Skyrim'

ARCHIVE_BASE_1_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738).exe'
ARCHIVE_BASE_1_MD5='5a73debc2115209be1abd257aa0be329'
## "--collisions rename-all" is used to extract all localised files, not only the English ones.
ARCHIVE_BASE_1_EXTRACTOR='innoextract'
ARCHIVE_BASE_1_EXTRACTOR_OPTIONS='--collisions rename-all --lowercase'
ARCHIVE_BASE_1_PART1_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-1.bin'
ARCHIVE_BASE_1_PART1_MD5='c8d28d38dc2a58102b4553ba61026591'
ARCHIVE_BASE_1_PART2_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-2.bin'
ARCHIVE_BASE_1_PART2_MD5='b262b033d7e30b1d7289e36e0901193c'
ARCHIVE_BASE_1_PART3_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-3.bin'
ARCHIVE_BASE_1_PART3_MD5='e249eaf58788328b47b4ba452cee0339'
ARCHIVE_BASE_1_PART4_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-4.bin'
ARCHIVE_BASE_1_PART4_MD5='3f82c21b9db4bc45adf65389125dd643'
ARCHIVE_BASE_1_PART5_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-5.bin'
ARCHIVE_BASE_1_PART5_MD5='20a789f518bfc5f5654315c6770ebeea'
ARCHIVE_BASE_1_PART6_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-6.bin'
ARCHIVE_BASE_1_PART6_MD5='1fc7001e970abf73f0e2cecfbddd6b9c'
ARCHIVE_BASE_1_PART7_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_0.1.3905696_(64bit)_(70738)-7.bin'
ARCHIVE_BASE_1_PART7_MD5='11c895a76cb12719e6252757b301089b'
ARCHIVE_BASE_1_SIZE='29202245'
ARCHIVE_BASE_1_VERSION='1.6.1179.0.8-gog70738'

ARCHIVE_BASE_0_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094).exe'
ARCHIVE_BASE_0_MD5='0b6d1c931788f66d4a3b02138733f05a'
## "--collisions rename-all" is used to extract all localised files, not only the English ones.
ARCHIVE_BASE_0_EXTRACTOR='innoextract'
ARCHIVE_BASE_0_EXTRACTOR_OPTIONS='--collisions rename-all --lowercase'
ARCHIVE_BASE_0_PART1_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='163b8af958071f85e78613834375209c'
ARCHIVE_BASE_0_PART2_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='c3852b6ae2bcc46fa0e4f085095bd12f'
ARCHIVE_BASE_0_PART3_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-3.bin'
ARCHIVE_BASE_0_PART3_MD5='a44293c54ebf39efd8f32cc4cc154220'
ARCHIVE_BASE_0_PART4_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-4.bin'
ARCHIVE_BASE_0_PART4_MD5='0dc37a5bd97295b5f163435c75868d03'
ARCHIVE_BASE_0_PART5_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-5.bin'
ARCHIVE_BASE_0_PART5_MD5='162f6baf1d24594f67a1710edef30c42'
ARCHIVE_BASE_0_PART6_NAME='setup_the_elder_scrolls_v_skyrim_special_edition_1.6.659.0.8_(64bit)_(59094)-6.bin'
ARCHIVE_BASE_0_PART6_MD5='5274ded2391553e9595b170af66ae0c3'
ARCHIVE_BASE_0_SIZE='29000000'
ARCHIVE_BASE_0_VERSION='1.6.659.0.8-gog59094'

CONTENT_PATH_DEFAULT='.'
## skyrimse.exe requirements.
CONTENT_LIBS_BIN_FILES='
bink2w64.dll
Galaxy64.dll'
CONTENT_GAME_BIN_FILES='
skyrimselauncher.exe
skyrimse.exe
high.ini
low.ini
medium.ini
ultra.ini
skyrimconsoledefault.ini'
CONTENT_GAME_L10N_FR_FILES='
skyrim_default_fr.ini
data/skyrim - voices_fr0.bsa
goggame-1711230643.info@fr-FR'
CONTENT_GAME_L10N_EN_FILES='
skyrim_default_en.ini
data/skyrim - voices_en0.bsa
goggame-1711230643.info@en-US'
CONTENT_GAME_L10N_DE_FILES='
skyrim_default_de.ini
data/skyrim - voices_de0.bsa
goggame-1711230643.info@de-DE'
CONTENT_GAME_DATA_TEXTURES_FILES='
data/skyrim - textures?.bsa'
CONTENT_GAME_DATA_FILES='
data/ccbgssse001-fish.bsa
data/ccbgssse001-fish.esm
data/ccbgssse025-advdsgs.bsa
data/ccbgssse025-advdsgs.esm
data/ccbgssse037-curios.bsa
data/ccbgssse037-curios.esl
data/ccqdrsse001-survivalmode.bsa
data/ccqdrsse001-survivalmode.esl
data/dawnguard.esm
data/dragonborn.esm
data/hearthfires.esm
data/marketplacetextures.bsa
data/_resourcepack.bsa
data/_resourcepack.esl
data/skyrim.esm
data/skyrim - animations.bsa
data/skyrim - interface.bsa
data/skyrim - meshes0.bsa
data/skyrim - meshes1.bsa
data/skyrim - misc.bsa
data/skyrim - shaders.bsa
data/skyrim - sounds.bsa
data/update.esm
data/video
skyrim.ccc'

## DXVK prevents massive fickering of shadows.
## (WINE 10.0)
WINE_DIRECT3D_RENDERER='dxvk'
WINE_PERSISTENT_DIRECTORIES='
users/${USER}/Documents/My Games/Skyrim Special Edition GOG'
## This registry entry is required for the settings window to work correctly.
# shellcheck disable=SC1003
WINE_REGISTRY_INIT='
[HKEY_LOCAL_MACHINE\Software\Wow6432Node\GOG.com\Games\1711230643]
"exe"="C:\\'"${GAME_ID}"'\\skyrimselauncher.exe"
'

APP_MAIN_EXE='skyrimse.exe'
APP_MAIN_ICON='skyrimselauncher.exe'

APP_LAUNCHER_ID="${GAME_ID}-config"
APP_LAUNCHER_NAME="${GAME_NAME} - Config"
APP_LAUNCHER_EXE='skyrimselauncher.exe'
APP_LAUNCHER_CAT='Settings'

PACKAGES_LIST='
PKG_BIN
PKG_L10N_FR
PKG_L10N_EN
PKG_L10N_DE
PKG_DATA_TEXTURES
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_L10N
PKG_DATA'
PKG_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
audio/x-wma, wmaversion=(int)1'

PKG_L10N_ID="${GAME_ID}-l10n"
PKG_L10N_FR_ID="${PKG_L10N_ID}-fr"
PKG_L10N_EN_ID="${PKG_L10N_ID}-en"
PKG_L10N_DE_ID="${PKG_L10N_ID}-de"
PKG_L10N_PROVIDES="
$PKG_L10N_ID"
PKG_L10N_FR_DESCRIPTION='French localisation'
PKG_L10N_EN_DESCRIPTION='English localisation'
PKG_L10N_DE_DESCRIPTION='German localisation'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
PKG_DATA_DEPENDENCIES_SIBLINGS='
PKG_DATA_TEXTURES'

PKG_DATA_TEXTURES_ID="${PKG_DATA_ID}-textures"
PKG_DATA_TEXTURES_DESCRIPTION="$PKG_DATA_DESCRIPTION - textures"

# Load common functions.

PLAYIT_LIB_PATHS="
$PWD
${XDG_DATA_HOME:="${HOME}/.local/share"}/play.it
/usr/local/share/games/play.it
/usr/local/share/play.it
/usr/share/games/play.it
/usr/share/play.it"

if [ -z "$PLAYIT_LIB2" ]; then
	for playit_lib_path in $PLAYIT_LIB_PATHS; do
		if [ -e "${playit_lib_path}/libplayit2.sh" ]; then
			PLAYIT_LIB2="${playit_lib_path}/libplayit2.sh"
			break
		fi
	done
fi
if [ -z "$PLAYIT_LIB2" ]; then
	printf '\n\033[1;31mError:\033[0m\n'
	printf 'libplayit2.sh not found.\n'
	exit 1
fi
# shellcheck source=libplayit2.sh
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
content_inclusion_default

# Fix name of localisation-specific configuration files required by the settings launcher.

config_file='goggame-1711230643.info'
mv \
	"$(package_path 'PKG_L10N_FR')$(path_game_data)/${config_file}@fr-FR" \
	"$(package_path 'PKG_L10N_FR')$(path_game_data)/${config_file}"
mv \
	"$(package_path 'PKG_L10N_EN')$(path_game_data)/${config_file}@en-US" \
	"$(package_path 'PKG_L10N_EN')$(path_game_data)/${config_file}"
mv \
	"$(package_path 'PKG_L10N_DE')$(path_game_data)/${config_file}@de-DE" \
	"$(package_path 'PKG_L10N_DE')$(path_game_data)/${config_file}"

# Write launchers.

## 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
case "$(messages_language)" in
	('fr')
		lang_string='version %s :'
		lang_fr='française'
		lang_en='anglaise'
		lang_de='allemande'
	;;
	('de')
		lang_string='%s version:'
		lang_fr='French'
		lang_en='English'
		lang_de='Deutsch'
	;;
	('en'|*)
		lang_string='%s version:'
		lang_fr='French'
		lang_en='English'
		lang_de='German'
	;;
esac
printf '\n'
printf "$lang_string" "$lang_fr"
print_instructions 'PKG_BIN' 'PKG_L10N_FR' 'PKG_DATA' 'PKG_DATA_TEXTURES'
printf "$lang_string" "$lang_en"
print_instructions 'PKG_BIN' 'PKG_L10N_EN' 'PKG_DATA' 'PKG_DATA_TEXTURES'
printf "$lang_string" "$lang_de"
print_instructions 'PKG_BIN' 'PKG_L10N_DE' 'PKG_DATA' 'PKG_DATA_TEXTURES'

# Clean up.

working_directory_cleanup

exit 0
