#!/usr/bin/env bash

function setup_posix() {
	require_commands cc

	# Create some sample text files
	echo "This is a local text file." >| "${LOCAL_DIR}"/local.txt
	echo "This is a remote text file." >| "${REMOTE_DIR}"/remote.txt
}

function test_posix() {
	# XrdPosix is not yet supported with musl libc
	cc -dumpmachine | grep -q musl && return

	# Make sure all symbols are actually resolved correctly
	MISSING_SYMBOLS=$(env XRDPOSIX_REPORT=1 xrdposix-cat /dev/null 2>&1)

	if grep -q 'Unable to resolve' <<< "${MISSING_SYMBOLS}"; then
		echo "XrdPosix cannot resolve some symbols:"
		echo "${MISSING_SYMBOLS}"
		exit 1
	fi

	# Check that cat with the local files still works

	assert xrdposix-cat "${LOCAL_DIR}"/local.txt
	assert xrdposix-cat "${REMOTE_DIR}"/remote.txt

	assert_failure xrdposix-cat /this/does/not/exist.txt

	# Check that cat with remote file URL works

	assert xrdposix-cat "${HOST}"/remote.txt

	assert_failure xrdposix-cat "${HOST}"/does/not/exist.txt

	# Check XrdPosix with link to remote
	ln -sf "${HOST}"/remote.txt local-to-remote.txt
	assert xrdposix-cat local-to-remote.txt

	ln -sf  "${HOST}"/does/not/exist local-to-stale.txt
	assert_failure xrdposix-cat local-to-stale.txt

	# Use XrdPosix via virtual mount point

	export XROOTD_VMP="${HOST#root://}:/xrootd/=/"

	assert xrdposix-cat /xrootd/remote.txt

	# Check that statx (which is Linux-only) works with local paths and URLs

	if [[ $(uname) == "Linux" ]]; then
		assert xrdposix-statx .
		assert xrdposix-statx "${HOST}"/
		assert xrdposix-statx "${LOCAL_DIR}"/local.txt
		assert xrdposix-statx "${REMOTE_DIR}"/remote.txt
		assert xrdposix-statx "${HOST}"/remote.txt

		assert_failure xrdposix-statx /this/does/not/exist.txt
		assert_failure xrdposix-statx "${HOST}"/this/does/not/exist.txt

		assert xrdposix-statx /xrootd/remote.txt

		# test links
		assert xrdposix-statx local-to-remote.txt
		assert xrdposix-statx local-to-stale.txt
		assert_failure xrdposix-statx -L local-to-stale.txt
		assert xrdposix-statx -L local-to-remote.txt
	fi
	# cleanup
	rm -f local-to-remote.txt
	rm -f local-to-stale.txt
}
