#!/bin/sh
# DPHMS payload v27: STOP kills EVERYTHING the desktop started, not just the obvious parts.
# v26 left xfconfd, plank, xterm, the dphms-session script and stray dbus-daemons alive. A surviving
# xfconfd from a dead bus answers (or hangs) the next START's settings calls, which is why theme
# writes kept failing ("Failed to set property") until a full reboot.
VNC=/root/.vnc
[ -f "$VNC/xvnc.pid" ] && kill "$(cat "$VNC/xvnc.pid")" 2>/dev/null
rm -f "$VNC/xvnc.pid" "$VNC/session.pid"
# 1. the session script and its launcher first, so nothing restarts what we kill below
pkill -f 'dbus-run-session' 2>/dev/null
pkill -f '/usr/local/bin/dphms-session' 2>/dev/null
# 2. desktop programs
for p in xfce4-session xfwm4 xfdesktop xfce4-panel plank xfce4-terminal xterm thunar Thunar \
         xfsettingsd xfce4-notifyd xfconfd firefox firefox-esr; do
  pkill -x "$p" 2>/dev/null
done
pkill -f 'xfce4/xfconf/xfconfd' 2>/dev/null
pkill -f 'at-spi' 2>/dev/null
# 3. the X server and any session bus (the fixed abstract bus name must be free for the next START)
pkill -f 'Xvnc :1' 2>/dev/null
pkill -f 'dbus-daemon.*--session' 2>/dev/null
pkill -f 'dbus-daemon.*dphms' 2>/dev/null
sleep 1
# 4. anything that ignored SIGTERM
for p in Xvnc xfconfd xfce4-panel xfwm4 xfdesktop plank dbus-daemon; do pkill -9 -x "$p" 2>/dev/null; done
# 5. stale state that makes the next START say "already running"
rm -f /tmp/.X1-lock /tmp/.X11-unix/X1 /tmp/xdg-runtime/bus 2>/dev/null
left=0
for p in Xvnc xfwm4 xfce4-panel xfconfd dbus-daemon; do pgrep -x "$p" >/dev/null 2>&1 && left=$((left+1)); done
echo "DPHMS OS desktop stopped (leftover process types: $left)."
