#!/bin/sh
# DPHMS payload v38 (hang-proof): same wrapper, now ALSO reached by D-Bus activation + every desktop entry.
# DPHMS payload v32: Thunar under proot. The folder list is plain file access, but Thunar also waits on
# gvfs / udisks / tumbler / polkit over D-Bus. None of those can run here, so Thunar hangs ("This window
# might be busy and is not responding") and the file list never fills in. Make it use local files only.
unset DBUS_STARTER_ADDRESS DBUS_STARTER_BUS_TYPE
export GIO_USE_VFS=local GVFS_DISABLE_FUSE=1 GIO_USE_VOLUME_MONITOR=unix GIO_USE_NETWORK_MONITOR=base \
       GSETTINGS_BACKEND=keyfile NO_AT_BRIDGE=1 GDK_BACKEND=x11 GTK_USE_PORTAL=0 LIBGL_ALWAYS_SOFTWARE=1
case "$1" in --daemon|-q|--quit) exit 0 ;; esac   # v38: never run as a daemon (that is the hang)
# settings written BEFORE launch (xfconf may be unreachable): no thumbnails, no volume manager, list view
XC=/root/.config/xfce4/xfconf/xfce-perchannel-xml
mkdir -p "$XC" /root/.config/Thunar
cat > "$XC/thunar.xml" <<'X'
<?xml version="1.0" encoding="UTF-8"?>
<channel name="thunar" version="1.0">
  <property name="misc-thumbnail-mode" type="string" value="THUNAR_THUMBNAIL_MODE_NEVER"/>
  <property name="misc-full-path-in-title" type="bool" value="true"/>
  <property name="misc-folders-first" type="bool" value="true"/>
  <property name="last-view" type="string" value="ThunarDetailsView"/>
  <property name="last-show-hidden" type="bool" value="true"/>
  <property name="last-side-pane" type="string" value="ThunarShortcutsPane"/>
  <property name="last-separator-position" type="int" value="170"/>
  <property name="last-window-width" type="int" value="900"/>
  <property name="last-window-height" type="int" value="1100"/>
</channel>
X
mkdir -p /root/Desktop /root/Downloads /root/Documents
# v38: helpers Thunar waits on / that paint nothing here. They never answer under proot, so a window
# that talks to them mapped-but-blank (the empty 'root - Thunar' box). Stop them and keep them off.
pkill -x gvfsd 2>/dev/null; pkill -x gvfsd-fuse 2>/dev/null; pkill -x gvfs-udisks2-volume-monitor 2>/dev/null
pkill -x gvfs-goa-volume-monitor 2>/dev/null; pkill -x gvfs-mtp-volume-monitor 2>/dev/null; pkill -x gvfs-gphoto2-volume-monitor 2>/dev/null
pkill -x gvfs-afc-volume-monitor 2>/dev/null; pkill -x tumblerd 2>/dev/null; pkill -x thunar-volman 2>/dev/null; pkill -x udisksd 2>/dev/null
# a running but stuck Thunar would swallow the new window: close it first, then start a fresh one
# v38: always start clean. A daemonised or half-dead Thunar owns org.xfce.FileManager and swallows every
# new request into a window that never paints. Kill any old one (they all run the REAL binary, not this
# wrapper), then start one that is guaranteed to run without --daemon.
# Only replace a Thunar that is BAD: running in --daemon mode, or one with no mapped window at all.
# A healthy Thunar with a visible window is left alone so a second folder opens as a new window.
for p in $(pgrep -x thunar 2>/dev/null); do
  if tr '\0' ' ' < /proc/$p/cmdline 2>/dev/null | grep -q -- '--daemon'; then kill "$p" 2>/dev/null; continue; fi
  if ! timeout 3 xprop -root _NET_CLIENT_LIST 2>/dev/null | grep -q 0x; then kill "$p" 2>/dev/null; fi
done
sleep 0.3
DIR=${1:-/root}
[ -d "$DIR" ] || DIR=/root
# v38: after the window maps, force a full repaint so VNC never shows stale desktop pixels inside it
( sleep 4; command -v xrefresh >/dev/null 2>&1 && DISPLAY=${DISPLAY:-:1} xrefresh 2>/dev/null ) &
exec /usr/bin/thunar "$DIR"
