#!/bin/sh
set -o nounset

oneTimeSetUp() {
	# Load the ./play.it library.
	. lib/libplayit.sh.2
}

setUp() {
	# Set a temporary directory to mess with real files
	TEST_TEMP_DIR=$(mktemp --directory)
	export TEST_TEMP_DIR
}

tearDown() {
	rm --force --recursive "$TEST_TEMP_DIR"
}

# Ensure that the libary compatibility symlink can be created.
test_compat_symlink_install() {
	assertTrue \
		'Failed to create the library compatibility symlink.' \
		"make install DESTDIR=$TEST_TEMP_DIR"

	return 0
}

# Check that icons are always installed with correct permissions.
test_icons_permissions() {
	local \
		PLAYIT_OPTION_PACKAGE='deb' \
		PLAYIT_WORKDIR="$TEST_TEMP_DIR" \
		script_version=20260311.4 \
		GAME_ID='alpha-centauri' \
		PACKAGES_LIST='
		PKG_BIN
		PKG_MOVIES
		PKG_DATA'

	icon_get_resolution() { printf '42x42' ; }

	local fake_icon_directory="${TEST_TEMP_DIR}/icons"
	mkdir --parents "$fake_icon_directory"
	local fake_icon_path="${fake_icon_directory}/icon.png"
	touch "$fake_icon_path"
	chmod 600 "$fake_icon_path"

	icons_include_from_directory 'PKG_DATA' 'APP_MAIN' "$fake_icon_directory"

	assertEquals \
		'Icons have been installed without read permissions for all users.' \
		'644' \
		"$(stat --format=%a $(package_path 'PKG_DATA')$(path_icons)/42x42/apps/alpha-centauri.png)"

	unset -f icon_get_resolution

	return 0
}

# Check that only the correct Unit3D binaries are included for a given architecture.
test_unity3d_binaries_inclusion_restricted_by_architecture() {
	local \
		PLAYIT_CONTEXT_PACKAGE \
		UNITY3D_NAME='Tyranny' \
		PACKAGES_LIST='PKG_BIN' \
		PKG_BIN_ARCH='64' \
		APP_MAIN_TYPE='native'

	set_current_package 'PKG_BIN'

	assertTrue \
		'Correct architecture Unity3D binary missing from files list.' \
		"content_files 'GAME_BIN' | grep --quiet --regexp='\<${UNITY3D_NAME}.x86_64$'"

	assertFalse \
		'Wrong architecture Unity3D binary included in files list.' \
		"content_files 'GAME_BIN' | grep --quiet --regexp='\<${UNITY3D_NAME}.x86$'"

	return 0
}

# Check that the correct application is linked to a given icon.
test_icon_matching_application() {
	local \
		APPLICATIONS_LIST='
		APP_MAIN
		APP_EDITOR
		APP_HOF
		APP_HOF_EDITOR' \

	assertEquals \
		'Wrong application matched for a given icon, due to a similar prefix between applications.' \
		'APP_HOF_EDITOR' \
		"$(icon_application 'APP_HOF_EDITOR_ICON')"

	return 0
}

# Check launchers_generation support for package-specific applications list.
test_launchers_generation_context_support() {
	local \
		APPLICATIONS_LIST_BIN_WINE='
		APP_MAIN
		APP_EDITOR_MAP
		APP_EDITOR_CAMPAIGN' \
		APPLICATIONS_LIST_BIN_VCMI='
		APP_MAIN' \
		PACKAGES_LIST='
		PKG_BIN_WINE
		PKG_BIN_VCMI
		PKG_DATA'

	information_launchers_generation() { return 0; }
	launcher_generation_checks() { printf '%s\n' "$2"; }
	launcher_write_script() { return 0; }
	launcher_write_desktop() { return 0; }

	set_default_package 'PKG_BIN_WINE'

	assertEquals \
		'Failed to fetch the package-specific value of the applications list from launchers_generation.' \
		'APP_MAIN' \
		"$(launchers_generation 'PKG_BIN_VCMI')"

	unset -f launcher_generation_checks launcher_write_script launcher_write_desktop

	return 0
}

# Check that Linux workarounds are not applied to Windows games.
test_unity3d_preload_native_only() {
	local \
		UNITY3D_NAME='realMyst' \
		PKG_BIN_ARCH='64' \
		APP_MAIN_TYPE

	unity3d_version() { printf '%s' '4.7.2f1'; }

	APP_MAIN_TYPE='native'
	assertNotNull \
		'Failed to apply a Unity3D workaround for native Linux games.' \
		"$(ld_preload_source 'PKG_BIN')"

	APP_MAIN_TYPE='wine'
	assertNull \
		'Applied by accident a Unity3D workaround for native Linux games to a Windows game.' \
		"$(ld_preload_source 'PKG_BIN')"

	unset -f unity3d_version

	return 0
}

# Check that splitting huge files correctly updates the packages list.
test_huge_files_packages_list_update() {
	local \
		PLAYIT_OPTION_PACKAGE='deb' \
		PLAYIT_WORKDIR="$TEST_TEMP_DIR" \
		script_version=20260402.1 \
		GAME_ID='battlefleet-gothic-armada-1' \
		UNREALENGINE4_NAME='BattleFleetGothic' \
		CONTENT_PATH_DEFAULT='.' \
		APP_MAIN_TYPE='wine' \
		PACKAGES_LIST='
		PKG_BIN
		PKG_DATA'
	local \
		HUGE_FILES_DATA="
		${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak" \
		PKG_DATA_ID="${GAME_ID}-data"

	split() {
		touch \
			"$(path_archive_contents)/${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak.1" \
			"$(path_archive_contents)/${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak.2"
	}

	mkdir --parents "$(path_archive_contents)/${UNREALENGINE4_NAME}/Content/Paks"
	touch "$(path_archive_contents)/${UNREALENGINE4_NAME}/Content/Paks/${UNREALENGINE4_NAME}-WindowsNoEditor.pak"
	content_inclusion_default >/dev/null

	assertEquals \
		'Failed to update the list of packages after splitting a huge file.' \
		'PKG_DATA_CHUNK2
PKG_DATA_CHUNK1
PKG_BIN
PKG_DATA' \
		"$(packages_list)"

	unset -f split

	return 0
}
