# /etc/X11/Xsession.d/70-dbus-persistance

# Define standard paths
export XDG_RUNTIME_DIR="/run/user/$(id -u)"
export DBUS_SESSION_BUS_ADDRESS="unix:path=$XDG_RUNTIME_DIR/bus"

if [ ! -d "$XDG_RUNTIME_DIR" ]; then
    mkdir -m 700 -p "$XDG_RUNTIME_DIR"
fi

# Clear "orphan" sockets from /tmp
find /tmp -maxdepth 1 -type s -user "$USER" -name "dbus-*" -delete 2>/dev/null
find /tmp -maxdepth 1 -type s -user "root" -name "dbus-*" -delete 2>/dev/null

# Increase the file descriptor limit
#ulimit -n 4096

# Start the daemon in the persistent path if the socket does not already exist
if [ ! -S "$XDG_RUNTIME_DIR/bus" ]; then
    if [ "$(id -u)" -ne 0 ]; then
        dbus-daemon --session --address="$DBUS_SESSION_BUS_ADDRESS" --fork --nopidfile
    fi
fi

if command -v dbus-update-activation-environment >/dev/null; then
    dbus-update-activation-environment --all
fi

