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

###
# Grip
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260416.4

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='grip-game'
GAME_NAME='Grip'

ARCHIVE_BASE_0_NAME='setup_grip_1.5.3_v2_(50529).exe'
ARCHIVE_BASE_0_MD5='1180ad9aae91fd168fb32d14c8516b05'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_grip_1.5.3_v2_(50529)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='ee3a558625e2a7d58f9be0bc9cbc6290'
ARCHIVE_BASE_0_PART2_NAME='setup_grip_1.5.3_v2_(50529)-2.bin'
ARCHIVE_BASE_0_PART2_MD5='427b9d550c93423010f688f3dd886704'
ARCHIVE_BASE_0_SIZE='12000000'
ARCHIVE_BASE_0_VERSION='1.5.3-gog50529'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/grip'

UNREALENGINE4_NAME='Grip'
# Experimental support for Unreal Engine 4 shipped libraries.
# No support in ./play.it library yet.
# (./play.it 2.35)
# TODO: Drop unused libraries.
UNREALENGINE4_LIBRARIES_THIRDPARTY='
NVIDIA/NVaftermath/Win64/GFSDK_Aftermath_Lib.dll
Ogg/Win64/VS2015/libogg_64.dll
PhysX/Win64/VS2015/APEX_Clothing_x64.dll
PhysX/Win64/VS2015/APEX_Destructible_x64.dll
PhysX/Win64/VS2015/ApexFramework_x64.dll
PhysX/Win64/VS2015/APEX_Legacy_x64.dll
PhysX/Win64/VS2015/NvCloth_x64.dll
PhysX/Win64/VS2015/PhysX3Common_x64.dll
PhysX/Win64/VS2015/PhysX3Cooking_x64.dll
PhysX/Win64/VS2015/PhysX3_x64.dll
PhysX/Win64/VS2015/PxFoundation_x64.dll
PhysX/Win64/VS2015/PxPvdSDK_x64.dll
Vorbis/Win64/VS2015/libvorbis_64.dll
Vorbis/Win64/VS2015/libvorbisfile_64.dll
Windows/DirectX/x64/WinPixEventRuntime.dll'
UNREALENGINE4_LIBRARIES_RUNTIME='
Nvidia/Ansel/Binaries/ThirdParty/AnselSDK64.dll
Nvidia/GfeSDK/ThirdParty/NVIDIAGfeSDK/redist/Win64/GfeSDK.dll'
UNREALENGINE4_LIBRARIES_PLUGINS='
Blinken/ThirdParty/AlienFXSDK/dll/Win64/LightFX.dll'
UNREALENGINE4_LIBRARIES_BINARIES='
xinput1_3.dll'
# Grip-Win64-Shipping.exe dependencies.
UNREALENGINE4_LIBRARIES_BINARIES="$UNREALENGINE4_LIBRARIES_BINARIES
Discord.dll"

CONTENT_PATH_DEFAULT='.'
# Prevent inclusion of unused libraries.
# (./play.it 2.35)
CONTENT_GAME_BIN_FILES="
${UNREALENGINE4_NAME}/Binaries/Win64/${UNREALENGINE4_NAME}-Win64-Shipping.exe"
CONTENT_GAME0_BIN_FILES="
${UNREALENGINE4_NAME}/Binaries/Win64/Music Player.exe"

HUGE_FILES_DATA="
${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak"

WINE_DIRECT3D_RENDERER='default'

APP_MAIN_EXE="${UNREALENGINE4_NAME}/Binaries/Win64/${UNREALENGINE4_NAME}-Win64-Shipping.exe"
APP_MAIN_ICON_WRESTOOL_OPTIONS='--type=14 --name=123 --language=1033'

PACKAGES_LIST='
PKG_BIN
PKG_DATA'

PKG_BIN_ARCH='64'
PKG_BIN_DEPENDENCIES_SIBLINGS='
PKG_DATA'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_DESCRIPTION='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
# shellcheck source=libplayit.sh.2
. "$PLAYIT_LIB2"

# Run default initialisation actions.

initialization_default "$@"

# Extract game data.

archive_extraction_default

# Include game data.

content_inclusion_icons 'PKG_DATA'
# Include shipped libraries.
CONTENT_UNREALENGINE4_THIRDPARTY_FILES=$(
	printf '%s' "$UNREALENGINE4_LIBRARIES_THIRDPARTY" | list_clean |
		sed --expression='s;^;Engine/Binaries/ThirdParty/;'
)
CONTENT_UNREALENGINE4_RUNTIME_FILES=$(
	printf '%s' "$UNREALENGINE4_LIBRARIES_RUNTIME" | list_clean |
		sed --expression='s;^;Engine/Plugins/Runtime/;'
)
CONTENT_UNREALENGINE4_PLUGINS_FILES=$(
	printf '%s' "$UNREALENGINE4_LIBRARIES_PLUGINS" | list_clean |
		sed --expression="s;^;$(unrealengine4_name)/Plugins/;"
)
CONTENT_UNREALENGINE4_BINARIES_FILES=$(
	printf '%s' "$UNREALENGINE4_LIBRARIES_BINARIES" | list_clean |
		sed --expression="s;^;$(unrealengine4_name)/Binaries/Win64/;"
)
content_inclusion 'UNREALENGINE4_THIRDPARTY' 'PKG_BIN' "$(path_libraries)"
content_inclusion 'UNREALENGINE4_RUNTIME'    'PKG_BIN' "$(path_libraries)"
content_inclusion 'UNREALENGINE4_PLUGINS'    'PKG_BIN' "$(path_libraries)"
content_inclusion 'UNREALENGINE4_BINARIES'   'PKG_BIN' "$(path_libraries)"
content_inclusion_default

# Write launchers.

# Do not install Visual C++ Runtime, as it triggers UE4 Prerequisites failing install attempts.
# The fallback Unreal Engine 4 verbs list should probably be dropped from ./play.it library.
# (./play.it 2.35)
unrealengine4_wine_winetricks_verbs_default() { return 0; }

# Install shipped libraries.
wineprefix_init_custom() {
	cat <<- EOF
	# Install shipped libraries.
	PATH_LIBRARIES_SYSTEM='$(path_libraries)'
	EOF
	cat <<- 'EOF'
	find "$PATH_LIBRARIES_SYSTEM" -iname '*.dll' | 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

# Clean up.

working_directory_cleanup

exit 0
