#!/bin/bash
# Evaluate given command while running with DATALAD_TESTS_TEMP_DIR pointing to
# that temporary filesystem mounted using nfs

set -e

fs=nfs
# NFS export + mount options (must be set on BOTH ends to take effect).
# Default to `async` (the NFS server replies before changes hit stable
# storage) since this script is for transient testing and `sync` adds tens
# of ms to every fsync -- which git/git-annex issue thousands of during
# clone/save/get and which pushed CI jobs into 6h timeouts (see a6fd7add6).
# Set DATALAD_TESTS_NFS_SYNC=1 (or any non-empty value) to use `sync`
# instead, e.g. to reproduce that slowdown locally.
if [ -n "${DATALAD_TESTS_NFS_SYNC:-}" ]; then
    nfs_opts="rw,sync"
else
    nfs_opts="rw,async"
fi

set -u
tmp=$(mktemp -u "${TMPDIR:-/tmp}/datalad-nfs-XXXXX")


uid=$(id -u)
mntorig="$tmp.orig"
mntpoint="$tmp.nfs"

echo "I: mounting $mntorig under $mntpoint via $fs"

set -x

mkdir -p "$mntpoint"
mkdir -p "$mntorig"

if ! dpkg -l nfs-kernel-server | grep '^ii.*nfs-kernel-server'; then
    sudo apt-get install -y nfs-kernel-server
fi

sudo exportfs -o "$nfs_opts" "localhost:$mntorig"
sudo mount -t "$fs" -o "$nfs_opts" "localhost:$mntorig" "$mntpoint"

# should how it was mounted
sudo mount | grep "$mntpoint" | sed -e 's,^,I: ,g'

# Run the actual command
echo "I: running $@"
TMPDIR="$mntpoint" DATALAD_TESTS_TEMP_DIR="$mntpoint" "$@"
ret=$?

echo "I: done, unmounting"
sudo umount "$mntpoint"
sudo exportfs -u "localhost:$mntorig"

rm -rf "$mntpoint" "$mntorig"
exit "$ret"
