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

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

script_version=20260310.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='morrowind'
GAME_NAME='The Elder Scrolls III: Morrowind'

ARCHIVE_BASE_EN_1_NAME='setup_the_elder_scrolls_iii_morrowind_goty_1.6.0.1820_gog_0.1_(77582).exe'
ARCHIVE_BASE_EN_1_MD5='c3ea7739b81d457e4c7d91758b762f4e'
ARCHIVE_BASE_EN_1_TYPE='innosetup'
ARCHIVE_BASE_EN_1_PART1_NAME='setup_the_elder_scrolls_iii_morrowind_goty_1.6.0.1820_gog_0.1_(77582)-1.bin'
ARCHIVE_BASE_EN_1_PART1_MD5='1158ea779b50c704c17bedee0ad2612c'
ARCHIVE_BASE_EN_1_SIZE='2209531'
ARCHIVE_BASE_EN_1_VERSION='1.6.0.1820-gog77582'
ARCHIVE_BASE_EN_1_URL='https://www.gog.com/game/the_elder_scrolls_iii_morrowind_goty_edition'

ARCHIVE_BASE_FR_1_NAME='setup_the_elder_scrolls_iii_morrowind_goty_1.6.0.1820_gog_0.1_(french)_(77582).exe'
ARCHIVE_BASE_FR_1_MD5='5f56a00e9256d930f63ab677c43c8b75'
ARCHIVE_BASE_FR_1_TYPE='innosetup'
ARCHIVE_BASE_FR_1_PART1_NAME='setup_the_elder_scrolls_iii_morrowind_goty_1.6.0.1820_gog_0.1_(french)_(77582)-1.bin'
ARCHIVE_BASE_FR_1_PART1_MD5='38571e142c67f9c8edd433902abe1454'
ARCHIVE_BASE_FR_1_SIZE='2239754'
ARCHIVE_BASE_FR_1_VERSION='1.6.0.1820-gog77582'
ARCHIVE_BASE_FR_1_URL='https://www.gog.com/game/the_elder_scrolls_iii_morrowind_goty_edition'

ARCHIVE_BASE_EN_0_NAME='setup_tes_morrowind_goty_2.0.0.7.exe'
ARCHIVE_BASE_EN_0_MD5='3a027504a0e4599f8c6b5b5bcc87a5c6'
ARCHIVE_BASE_EN_0_TYPE='innosetup'
ARCHIVE_BASE_EN_0_SIZE='2300000'
ARCHIVE_BASE_EN_0_VERSION='1.6.1820-gog2.0.0.7'

ARCHIVE_BASE_FR_0_NAME='setup_tes_morrowind_goty_french_2.0.0.7.exe'
ARCHIVE_BASE_FR_0_MD5='2aee024e622786b2cb5454ff074faf9b'
ARCHIVE_BASE_FR_0_TYPE='innosetup'
ARCHIVE_BASE_FR_0_SIZE='2300000'
ARCHIVE_BASE_FR_0_VERSION='1.6.1820-gog2.0.0.7'

CONTENT_PATH_DEFAULT='.'
CONTENT_PATH_DEFAULT_EN_0='app'
CONTENT_PATH_DEFAULT_FR_0='app'
CONTENT_GAME_MAIN_FILES='
Data Files/Fonts
Data Files/Icons
Data Files/Meshes
Data Files/Music
Data Files/Sound
Data Files/Textures
Knife.ico'
CONTENT_GAME_L10N_FILES='
Morrowind.ini
Data Files/BookArt/MagicStoneMap4.dds
Data Files/BookArt/Morrowind Books HTML.htm
Data Files/BookArt/*.bmp
Data Files/BookArt/*.tga
Data Files/Sound/Vo
Data Files/Splash
Data Files/Video/bethesda logo.bik
Data Files/Video/mw_*.bik
Data Files/Morrowind.bsa
Data Files/Morrowind.esm'
CONTENT_GAME_EXPANSION_TRIBUNAL_FILES='
Data Files/Tribunal.bsa
Data Files/Tribunal.esm'
CONTENT_GAME_EXPANSION_BLOODMOON_FILES='
Data Files/Video/bm_*.bik
Data Files/Bloodmoon.bsa
Data Files/Bloodmoon.esm'
CONTENT_DATAFILES_RELATIVE_PATH='_OfficialPlugins/_unpacked_files'
CONTENT_DATAFILES_FILES='
Icons
Meshes
Sound
Textures
*.esp
*.txt'
CONTENT_DOC_L10N_FILES='
*.txt'
CONTENT_DOC_DATA_FILES='
*.pdf'

APP_MAIN_TYPE='custom'
APP_MAIN_ICON='Morrowind.exe'

PACKAGES_LIST='
PKG_MAIN
PKG_L10N
PKG_EXPANSION_TRIBUNAL
PKG_EXPANSION_BLOODMOON'

PKG_MAIN_DEPENDENCIES_SIBLINGS='
PKG_L10N_BASE'
PKG_MAIN_DEPENDENCIES_COMMANDS='
openmw-iniimporter
openmw-launcher'
# Easier upgrades from pre-20260106.2 packages
PKG_MAIN_PROVIDES="${PKG_MAIN_PROVIDES:-}
the-elder-scrolls-3
the-elder-scrolls-3-data"

PKG_L10N_BASE_ID="${GAME_ID}-l10n"
PKG_L10N_ID_EN="${PKG_L10N_BASE_ID}-en"
PKG_L10N_ID_FR="${PKG_L10N_BASE_ID}-fr"
PKG_L10N_PROVIDES="
$PKG_L10N_BASE_ID"
PKG_L10N_DESCRIPTION_EN='English localization'
PKG_L10N_DESCRIPTION_FR='French localization'
# Easier upgrades from pre-20260106.2 packages
PKG_L10N_PROVIDES="${PKG_L10N_PROVIDES:-}
the-elder-scrolls-3-l10n"

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='data'
# Easier upgrades from pre-20260106.2 packages
PKG_DATA_PROVIDES="${PKG_DATA_PROVIDES:-}
the-elder-scrolls-3-data"

PKG_EXPANSION_TRIBUNAL_ID="${GAME_ID}-tribunal"
PKG_EXPANSION_TRIBUNAL_ID_FR="${PKG_EXPANSION_TRIBUNAL_ID}-fr"
PKG_EXPANSION_TRIBUNAL_ID_EN="${PKG_EXPANSION_TRIBUNAL_ID}-en"
PKG_EXPANSION_TRIBUNAL_PROVIDES="
$PKG_EXPANSION_TRIBUNAL_ID"
PKG_EXPANSION_TRIBUNAL_DESCRIPTION='Tribunal expansion'
PKG_EXPANSION_TRIBUNAL_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_L10N'
# Easier upgrades from pre-20260106.2 packages
PKG_EXPANSION_TRIBUNAL_PROVIDES="${PKG_EXPANSION_TRIBUNAL_PROVIDES:-}
the-elder-scrolls-3-tribunal"

PKG_EXPANSION_BLOODMOON_ID="${GAME_ID}-bloodmoon"
PKG_EXPANSION_BLOODMOON_ID_FR="${PKG_EXPANSION_BLOODMOON_ID}-fr"
PKG_EXPANSION_BLOODMOON_ID_EN="${PKG_EXPANSION_BLOODMOON_ID}-en"
PKG_EXPANSION_BLOODMOON_PROVIDES="
$PKG_EXPANSION_BLOODMOON_ID"
PKG_EXPANSION_BLOODMOON_DESCRIPTION='Bloodmoon expansion'
PKG_EXPANSION_BLOODMOON_DEPENDENCIES_SIBLINGS='
PKG_MAIN
PKG_L10N'
# Easier upgrades from pre-20260106.2 packages
PKG_EXPANSION_BLOODMOON_PROVIDES="${PKG_EXPANSION_BLOODMOON_PROVIDES:-}
the-elder-scrolls-3-bloodmoon"

# 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 '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
content_inclusion 'DATAFILES' 'PKG_MAIN' "$(path_game_data)/Data Files"
content_inclusion_default

# Write launchers.

custom_launcher() {
	launcher_headers
	cat <<- EOF
	PATH_GAME_DATA="$(path_game_data)"
	EOF
	cat <<- 'OUTEREOF'
	OPENMW_CONFIG_PATH="${XDG_CONFIG_HOME:=${HOME}/.config}/openmw"
	OPENMW_CONFIG_FILE="${OPENMW_CONFIG_PATH}/openmw.cfg"
	OPENMW_CONFIG_LAUNCHER_FILE="${OPENMW_CONFIG_PATH}/launcher.cfg"

	# Initialize OpenMW configuration on first launch
	if [ ! -e "$OPENMW_CONFIG_FILE" ]; then
	    mkdir --parents "$OPENMW_CONFIG_PATH"
	    cat > "$OPENMW_CONFIG_FILE" <<- EOF
	    data="${PATH_GAME_DATA}/Data Files"
	    content=Morrowind.esm
	    EOF
	    openmw-iniimporter --ini "${PATH_GAME_DATA}/Morrowind.ini" --cfg "$OPENMW_CONFIG_FILE"
	    if [ ! -e "$OPENMW_CONFIG_LAUNCHER_FILE" ]; then
	        cat > "$OPENMW_CONFIG_LAUNCHER_FILE" <<- EOF
	        [General]
	        firstrun=false
	        EOF
	    fi
	fi

	openmw-launcher

	exit 0
	OUTEREOF
}
launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
