#!/bin/sh

if [ -r /etc/profile.d/qubes-session.sh ]; then
    # shellcheck disable=SC1091
    . /etc/profile.d/qubes-session.sh
fi

# gvfs-open, kde-open, and possibly others don't wait for editor to be
# closed, which is critical behaviour for DisposableVM (which gets destroyed
# after this process exits).
#
# The `pidwait -A -f "$1"` is rather silly hack, to wait for the process that is
# passed the temporary file path as a parameter, but ignoring parent processes.
#
# This is needed on Xfce template, because exo-open starts the application in
# the background; and in templates built around KDE, because they start the
# file handling process in a way that it is not a child of the shell that
# spawned this script.
#
# We also want to make sure that `xdg-open`'s exit code is emitted back to
# parent process in case of any errors.
export DE=generic
xdg-open "$@"
XDG_OPEN_EXIT_CODE=$?
pidwait -A -f "$1"
exit $XDG_OPEN_EXIT_CODE
