#!/bin/sh
# Ensure gvfsd is running in the current XDG_RUNTIME_DIR before
# running tests.
#
# gvfs 1.59.90+ emits a g_warning when the P2P socket directory is missing.
# GLib's test framework makes all g_warning calls fatal (via g_test_init).
# debhelper 13.10+ creates a fresh XDG_RUNTIME_DIR for tests, where gvfsd has
# not created its socket directory yet, triggering fatal warning.
#
# Starting gvfsd here (after debhelper has set XDG_RUNTIME_DIR) causes it to
# create its socket directory at the right location.

if [ -n "$XDG_RUNTIME_DIR" ] && ! [ -d "$XDG_RUNTIME_DIR/gvfsd" ]; then
    /usr/lib/gvfs/gvfsd --no-fuse </dev/null >/dev/null 2>&1 &
    # Wait up to 5 seconds for gvfsd to create its socket directory
    i=0
    while [ "$i" -lt 50 ] && ! [ -d "$XDG_RUNTIME_DIR/gvfsd" ]; do
        sleep 0.1
        i=$((i + 1))
    done
fi

exec "$@"
