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

###
# Dr Livingstone, I Presume?
# send your bug reports to bugs@dotslashplay.it
###

script_version=20260419.2

PLAYIT_COMPATIBILITY_LEVEL='2.35'

GAME_ID='dr-livingstone-i-presume'
GAME_NAME='Dr Livingstone, I Presume?'

GAME_ID_DEMO="${GAME_ID}-demo"
GAME_NAME_DEMO="$GAME_NAME (demo)"

ARCHIVE_BASE_0_NAME='setup_dr_livingstone_i_presume_220419_0803_(55205).exe'
ARCHIVE_BASE_0_MD5='d6fdb1ce7ee00f5068b1d93f4592dad0'
ARCHIVE_BASE_0_TYPE='innosetup'
ARCHIVE_BASE_0_PART1_NAME='setup_dr_livingstone_i_presume_220419_0803_(55205)-1.bin'
ARCHIVE_BASE_0_PART1_MD5='36b0f92cde1b8ccbd42f339fbc752ada'
ARCHIVE_BASE_0_SIZE='5300000'
ARCHIVE_BASE_0_VERSION='2022.04.19-gog55205'
ARCHIVE_BASE_0_URL='https://www.gog.com/game/dr_livingstone_i_presume'

# This free demo used to be distributed by GOG.com.
ARCHIVE_BASE_DEMO_0_NAME='setup_dr_livingstone_i_presume_demo_210726_1631_(48828).exe'
ARCHIVE_BASE_DEMO_0_MD5='57f69d1473e4c24706fe2cc70c56d62b'
ARCHIVE_BASE_DEMO_0_TYPE='innosetup'
ARCHIVE_BASE_DEMO_0_PART1_NAME='setup_dr_livingstone_i_presume_demo_210726_1631_(48828)-1.bin'
ARCHIVE_BASE_DEMO_0_PART1_MD5='4adc9914d1629ff3849f9d1d1f139b5c'
ARCHIVE_BASE_DEMO_0_SIZE='3800000'
ARCHIVE_BASE_DEMO_0_VERSION='2021.07.26-gog48828'

UNREALENGINE4_NAME='DrLivingstone'
# 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='
DbgHelp/dbghelp.dll
NVIDIA/NVaftermath/Win64/GFSDK_Aftermath_Lib.x64.dll
Ogg/Win64/VS2015/libogg_64.dll
PhysX3/Win64/VS2015/APEX_Clothing_x64.dll
PhysX3/Win64/VS2015/ApexFramework_x64.dll
PhysX3/Win64/VS2015/APEX_Legacy_x64.dll
PhysX3/Win64/VS2015/NvCloth_x64.dll
PhysX3/Win64/VS2015/PhysX3Common_x64.dll
PhysX3/Win64/VS2015/PhysX3Cooking_x64.dll
PhysX3/Win64/VS2015/PhysX3_x64.dll
PhysX3/Win64/VS2015/PxFoundation_x64.dll
PhysX3/Win64/VS2015/PxPvdSDK_x64.dll
Steamworks/Steamv147/Win64/steam_api64.dll
Vorbis/Win64/VS2015/libvorbis_64.dll
Vorbis/Win64/VS2015/libvorbisfile_64.dll
Windows/XAudio2_9/x64/xaudio2_9redist.dll'
# TODO: Check if the Steam library is required.
UNREALENGINE4_LIBRARIES_THIRDPARTY="$UNREALENGINE4_LIBRARIES_THIRDPARTY
Steamworks/Steamv147/Win64/steam_api64.dll"
UNREALENGINE4_LIBRARIES_PLUGINS='
OnlineSubsystemGOG/Source/ThirdParty/GalaxySDK/Libraries/Galaxy64.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkAudioInput.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkCompressor.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkDelay.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkExpander.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkFlanger.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkGain.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkGuitarDistortion.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkHarmonizer.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkMatrixReverb.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkMotion.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkParametricEQ.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkPeakLimiter.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkPitchShifter.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkRecorder.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkReflect.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkRoomVerb.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSilenceGenerator.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSineTone.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSoundEngineDLL.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSoundSeedAir.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSoundSeedImpact.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkStereoDelay.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkSynthOne.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkTimeStretch.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkToneGen.dll
Wwise/ThirdParty/x64_vc160/Release/bin/AkTremolo.dll
Wwise/ThirdParty/x64_vc160/Release/bin/Auro.dll
Wwise/ThirdParty/x64_vc160/Release/bin/iZotope.dll
Wwise/ThirdParty/x64_vc160/Release/bin/McDSP.dll'
# TODO: Check if the Galaxy library is required.
UNREALENGINE4_LIBRARIES_PLUGINS="$UNREALENGINE4_LIBRARIES_PLUGINS
OnlineSubsystemGOG/Source/ThirdParty/GalaxySDK/Libraries/Galaxy64.dll"

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

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_BIN_DEPENDENCIES_GSTREAMER_PLUGINS='
video/quicktime, variant=(string)iso'

PKG_DATA_ID="${GAME_ID}-data"
PKG_DATA_ID_DEMO="${GAME_ID_DEMO}-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_PLUGINS_FILES=$(
	printf '%s' "$UNREALENGINE4_LIBRARIES_PLUGINS" | list_clean |
		sed --expression="s;^;$(unrealengine4_name)/Plugins/;"
)
content_inclusion 'UNREALENGINE4_THIRDPARTY' 'PKG_BIN' "$(path_libraries)"
content_inclusion 'UNREALENGINE4_PLUGINS'    'PKG_BIN' "$(path_libraries)"
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" -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
