#!/bin/sh
# SPDX-License-Identifier: BSD-2-Clause
# SPDX-FileCopyrightText: © 2020 Antoine Le Gonidec <vv221@dotslashplay.it>
# SPDX-FileCopyrightText: © 2021 Christian Stalp
# SPDX-FileCopyrightText: © 2023 Jacek Szafarkiewicz
set -o errexit

###
# Neverwinter Nights: Enhanced Edition
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260710.2

PLAYIT_COMPATIBILITY_LEVEL=2.35

GAME_ID=neverwinter-nights-1-enhanced-edition
GAME_NAME='Neverwinter Nights: Enhanced Edition'

ARCHIVE_BASE_FR_7_NAME='neverwinter_nights_enhanced_edition_french_89_8193_37_17_85648.sh'
ARCHIVE_BASE_FR_7_MD5=24dd5f8f35b59393d018a67ed9d57652
ARCHIVE_BASE_FR_7_SIZE=6383779
ARCHIVE_BASE_FR_7_VERSION=89.8193.37.17-1

ARCHIVE_BASE_EN_7_NAME='neverwinter_nights_enhanced_edition_89_8193_37_17_85648.sh'
ARCHIVE_BASE_EN_7_MD5=3255edc809ddfefcc92366dcc64290dc
ARCHIVE_BASE_EN_7_SIZE=6383779
ARCHIVE_BASE_EN_7_VERSION=89.8193.37.17-1

ARCHIVE_BASE_FR_6_NAME='neverwinter_nights_enhanced_edition_french_88_8193_36_13_72824.sh'
ARCHIVE_BASE_FR_6_MD5=ed1a605cc15b5416d0eda7840dd4f8d9
ARCHIVE_BASE_FR_6_SIZE=6250881
ARCHIVE_BASE_FR_6_VERSION=88.8193.36.13-1

ARCHIVE_BASE_PL_6_NAME='neverwinter_nights_enhanced_edition_polish_88_8193_36_13_72824.sh'
ARCHIVE_BASE_PL_6_MD5=351e857d4e6a75d18721a3964a937bfe
ARCHIVE_BASE_PL_6_SIZE=6250881
ARCHIVE_BASE_PL_6_VERSION=88.8193.36.13-1

ARCHIVE_BASE_DE_6_NAME='neverwinter_nights_enhanced_edition_german_88_8193_36_13_72824.sh'
ARCHIVE_BASE_DE_6_MD5=2218e12a8d09e1b799f11402c93d1eec
ARCHIVE_BASE_DE_6_SIZE=6250881
ARCHIVE_BASE_DE_6_VERSION=88.8193.36.13-1

ARCHIVE_BASE_EN_6_NAME='neverwinter_nights_enhanced_edition_88_8193_36_13_72824.sh'
ARCHIVE_BASE_EN_6_MD5=922f474f2e1573abc66f803c752f2159
ARCHIVE_BASE_EN_6_SIZE=6250881
ARCHIVE_BASE_EN_6_VERSION=88.8193.36.13-1

ARCHIVE_BASE_FR_5_NAME='neverwinter_nights_enhanced_edition_french_87_8193_35_40_65678.sh'
ARCHIVE_BASE_FR_5_MD5=319194e7a229b24b19f7454d8ed860d6
ARCHIVE_BASE_FR_5_SIZE=6253452
ARCHIVE_BASE_FR_5_VERSION=87.8193.35.40-1

ARCHIVE_BASE_PL_5_NAME='neverwinter_nights_enhanced_edition_polish_87_8193_35_40_65678.sh'
ARCHIVE_BASE_PL_5_MD5=d4c3d4c4af501424a60bf9e09e5a2f01
ARCHIVE_BASE_PL_5_SIZE=6253452
ARCHIVE_BASE_PL_5_VERSION=87.8193.35.40-1

ARCHIVE_BASE_EN_5_NAME='neverwinter_nights_enhanced_edition_87_8193_35_40_65678.sh'
ARCHIVE_BASE_EN_5_MD5=c1191f9e19f1da52f1424025017cf56f
ARCHIVE_BASE_EN_5_SIZE=6253452
ARCHIVE_BASE_EN_5_VERSION=87.8193.35.40-1

ARCHIVE_BASE_FR_4_NAME='neverwinter_nights_enhanced_edition_french_85_8193_33_50393.sh'
ARCHIVE_BASE_FR_4_MD5=19b036e3c07fd4aa9a835de05b4aea1f
ARCHIVE_BASE_FR_4_SIZE=6200000
ARCHIVE_BASE_FR_4_VERSION=85.8193.33-1

ARCHIVE_BASE_EN_4_NAME='neverwinter_nights_enhanced_edition_85_8193_33_50393.sh'
ARCHIVE_BASE_EN_4_MD5=4e6c4f7bd75791a9c726b7b573cfafe5
ARCHIVE_BASE_EN_4_SIZE=6300000
ARCHIVE_BASE_EN_4_VERSION=85.8193.33-1

ARCHIVE_BASE_EN_3_NAME='neverwinter_nights_enhanced_edition_83_8193_23_47201.sh'
ARCHIVE_BASE_EN_3_MD5=b85d2ea70aa6dba1096e97e944d98141
ARCHIVE_BASE_EN_3_SIZE=6300000
ARCHIVE_BASE_EN_3_VERSION=83.8193.23-1

ARCHIVE_BASE_EN_2_NAME='neverwinter_nights_enhanced_edition_82_8193_20_1_43497.sh'
ARCHIVE_BASE_EN_2_MD5=c80e73f0305ad99355cc089c632b8b0c
ARCHIVE_BASE_EN_2_SIZE=6200000
ARCHIVE_BASE_EN_2_VERSION=82.8193.20.1-1

ARCHIVE_BASE_FR_1_NAME='neverwinter_nights_enhanced_edition_french_81_8193_16_41300.sh'
ARCHIVE_BASE_FR_1_MD5=67f42c1a1604a26fdbcd0cbc800856f5
ARCHIVE_BASE_FR_1_SIZE=6200000
ARCHIVE_BASE_FR_1_VERSION=81.8193.16-1

ARCHIVE_BASE_EN_1_NAME='neverwinter_nights_enhanced_edition_81_8193_16_41300.sh'
ARCHIVE_BASE_EN_1_MD5=a52646002ab14c452731b0636fdc8278
ARCHIVE_BASE_EN_1_SIZE=6200000
ARCHIVE_BASE_EN_1_VERSION=81.8193.16-1

ARCHIVE_BASE_EN_0_NAME='neverwinter_nights_enhanced_edition_80_8193_9_37029.sh'
ARCHIVE_BASE_EN_0_MD5=fb98f859b5f5516fc7df8b00c7264c07
ARCHIVE_BASE_EN_0_VERSION=80.8193.9-1
ARCHIVE_BASE_EN_0_SIZE=5000000

CONTENT_PATH_DEFAULT='data/noarch/game'
# TODO: Is the sh script required?
CONTENT_GAME_MAIN_FILES='
bin/linux-x86/build.txt
bin/linux-x86/launch.sh
bin/linux-x86/nwmain-linux'
# TODO: Dedicated package for the server
CONTENT_GAME0_MAIN_FILES='
bin/linux-x86/nwserver-linux'
CONTENT_GAME1_MAIN_FILES='
data
ovr
lang/??/data'
CONTENT_DOC_MAIN_RELATIVE_PATH='lang'
CONTENT_DOC_MAIN_FILES='
??/docs'

APP_MAIN_EXE='bin/linux-x86/nwmain-linux'
APP_MAIN_ICON='bin/win32/nwmain.exe'

# TODO: Dedicated package for the server
APP_SERVER_ID=${GAME_ID}-server
APP_SERVER_NAME="${GAME_NAME} - server"
APP_SERVER_EXE='bin/linux-x86/nwserver-linux'
APP_SERVER_ICON='bin/win32/nwserver.exe'

PKG_MAIN_ARCH=64
PKG_MAIN_DEPENDENCIES_LIBRARIES='
libc.so.6
libdl.so.2
libgcc_s.so.1
libGL.so.1
libm.so.6
libopenal.so.1
libpthread.so.0
librt.so.1
libstdc++.so.6'
# Easier upgrades from pre-20260710.1 packages
PKG_MAIN_PROVIDES=neverwinter-nights-1-enhanced-edition-data

# 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 archive contents.

archive_extraction_default

# Include game data.

content_inclusion_icons
content_inclusion_default

# Write launchers.

# Force the use of system-provided SDL.
APP_MAIN_PRERUN="$(application_prerun APP_MAIN)
$(launcher_tweak_sdl_override)"
PKG_MAIN_DEPENDENCIES_LIBRARIES="$(dependencies_libraries_list PKG_MAIN)
libSDL2-2.0.so.0"

# Run the game binary from its parent directory.
game_exec_line() {
	local application=$1
	local application_path application_directory application_file
	application_path=$(application_exe $application)
	application_directory=$(dirname "$application_path")
	application_file=$(basename "$application_path")
	cat <<- EOF
	cd "$application_directory"
	"./${application_file}" "\$@"
	EOF
}

launchers_generation

# Build packages.

packages_generation
print_instructions

# Clean up.

working_directory_cleanup

exit 0
