#!/bin/sh
# DPHMS payload v27: write the DPHMS theme as FILES, before any XFCE process starts.
# v26 wrote the look through live xfconf-query calls. Those need D-Bus + xfconfd to be healthy, and
# on-device they were not ("Failed to set property", empty panel, stock XFCE mouse wallpaper).
# XFCE reads these XML files at startup, so nothing here depends on the bus. Runs on every start.
# v36: panel background-style=0 (fully transparent) so the wallpaper runs to the very
# top of the screen — the old near-opaque dark strip covered the top of the background.
# DPHMS look: dark grey #121212/#1E1E1E, #333 borders, red accent #FF2A2A/#FF4444.
C=/root/.config/xfce4/xfconf/xfce-perchannel-xml
mkdir -p "$C" /root/.config/gtk-3.0 /root/.config/gtk-4.0 /root/.config/xfce4/panel /root/.local/share/themes
# pick the wallpaper that matches the live screen shape (portrait phone desktop vs landscape)
# size source 1: the geometry the launcher just chose (always correct, needs no X tool)
SZ=$(cat /root/.vnc/geometry 2>/dev/null | tr -d ' \r\n')
# size source 2: root window property (xprop ships with the desktop) -- _NET_DESKTOP_GEOMETRY = w, h
[ -n "$SZ" ] || SZ=$(xprop -root -display "${DISPLAY:-:1}" _NET_DESKTOP_GEOMETRY 2>/dev/null | sed -n 's/.*= *\([0-9]*\), *\([0-9]*\).*/\1x\2/p' | head -1)
SW=${SZ%x*}; SH=${SZ#*x}
WALL=/usr/share/backgrounds/dphms/dphms-dark-1280x720.png
if [ -n "$SW" ] && [ -n "$SH" ] && [ "$SH" -gt "$SW" ] 2>/dev/null; then WALL=/usr/share/backgrounds/dphms/dphms-dark-810x1440.png; fi
[ "$SZ" = "1080x2400" ] && WALL=/usr/share/backgrounds/dphms/dphms-dark-1080x2400.png
[ -f "$WALL" ] || WALL=/usr/share/backgrounds/dphms/dphms-dark-1280x720.png
[ -f "$WALL" ] || WALL=""
# v29 SPLASH: the first thing a viewer sees is the DPHMS boot screen. dphms-session swaps in the real
# wallpaper (saved in /root/.vnc/wallpaper) once the panel, dock and terminal are up.
echo "$WALL" > /root/.vnc/wallpaper
SPL=/usr/share/backgrounds/dphms/dphms-splash-1280x720.png
if [ -n "$SW" ] && [ -n "$SH" ] && [ "$SH" -gt "$SW" ] 2>/dev/null; then SPL=/usr/share/backgrounds/dphms/dphms-splash-810x1440.png; fi
[ "$SZ" = "1080x2400" ] && SPL=/usr/share/backgrounds/dphms/dphms-splash-1080x2400.png
if [ -f "$SPL" ] && [ -n "$WALL" ]; then WALL="$SPL"; echo "  splash: $SPL"; fi
echo "  screen: ${SZ:-unknown}"
# ---- pick themes that really exist (theme folders are case-sensitive) ----
GTK=""; for t in WhiteSur-Dark WhiteSur-dark Adwaita-dark Adwaita; do [ -d /usr/share/themes/$t ] && GTK=$t && break; done
ICON=""; for t in WhiteSur-dark WhiteSur Papirus-Dark Adwaita hicolor; do [ -d /usr/share/icons/$t ] && ICON=$t && break; done
# v33: DPHMS-Finger (red/black middle finger) is installed from the payload copy and preferred
if [ -d /usr/local/share/dphms/cursor/cursors ]; then
  rm -rf /usr/share/icons/DPHMS-Finger; mkdir -p /usr/share/icons /usr/share/icons/default
  cp -a /usr/local/share/dphms/cursor /usr/share/icons/DPHMS-Finger 2>/dev/null
  printf '[Icon Theme]\nInherits=DPHMS-Finger\n' > /usr/share/icons/default/index.theme
fi
CUR=""; for t in DPHMS-Finger WhiteSur-cursors Adwaita default; do [ -d /usr/share/icons/$t ] && CUR=$t && break; done
# ---- DPHMS window theme (v29: SOLID frames shipped as finished images; the old WhiteSur copy was hollow
# because WhiteSur's xfwm4 art is never built or installed in this container) ----
if [ -d /usr/local/share/dphms/xfwm4 ]; then
  mkdir -p /usr/share/themes/DPHMS-Dark/xfwm4
  cp -f /usr/local/share/dphms/xfwm4/* /usr/share/themes/DPHMS-Dark/xfwm4/
  echo "  window theme: DPHMS-Dark installed ($(ls /usr/share/themes/DPHMS-Dark/xfwm4 | wc -l) files)"
else
  echo "  window theme: shipped DPHMS-Dark files missing"
fi
WM=WhiteSur-dark; [ -d /usr/share/themes/DPHMS-Dark/xfwm4 ] && WM=DPHMS-Dark
[ -d /usr/share/themes/$WM/xfwm4 ] || WM=Default
echo "  theme: gtk=${GTK:-none} icons=${ICON:-none} cursor=${CUR:-none} xfwm4=$WM wallpaper=${WALL:-MISSING}"
# ---- xsettings (GTK theme, icons, cursor, font) ----
cat > "$C/xsettings.xml" <<X
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xsettings" version="1.0">
  <property name="Net" type="empty">
    <property name="ThemeName" type="string" value="${GTK:-Adwaita-dark}"/>
    <property name="IconThemeName" type="string" value="${ICON:-hicolor}"/>
  </property>
  <property name="Gtk" type="empty">
    <property name="CursorThemeName" type="string" value="${CUR:-default}"/>
    <property name="CursorThemeSize" type="int" value="48"/>
    <property name="FontName" type="string" value="Sans 10"/>
  </property>
  <property name="Xft" type="empty">
    <property name="Antialias" type="int" value="1"/>
    <property name="Hinting" type="int" value="1"/>
    <property name="HintStyle" type="string" value="hintslight"/>
    <property name="RGBA" type="string" value="none"/>
  </property>
</channel>
X
# ---- window manager ----
cat > "$C/xfwm4.xml" <<X
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfwm4" version="1.0">
  <property name="general" type="empty">
    <property name="theme" type="string" value="$WM"/>
    <property name="use_compositing" type="bool" value="false"/>
    <property name="title_font" type="string" value="Sans Bold 10"/>
    <property name="button_layout" type="string" value="CMH|T"/>
    <property name="workspace_count" type="int" value="2"/>
  </property>
</channel>
X
# ---- wallpaper: cover every monitor name XFCE might use (VNC exposes monitorVNC-0; stock uses monitor0) ----
if [ -n "$WALL" ]; then
  mons=""
  for m in monitor0 monitorVNC-0 monitorVNC-1 monitorscreen; do
    mons="$mons
      <property name=\"$m\" type=\"empty\"><property name=\"workspace0\" type=\"empty\">
        <property name=\"color-style\" type=\"int\" value=\"0\"/>
        <property name=\"rgba1\" type=\"array\"><value type=\"double\" value=\"0.1\"/><value type=\"double\" value=\"0.1\"/><value type=\"double\" value=\"0.1\"/><value type=\"double\" value=\"1\"/></property>
        <property name=\"image-style\" type=\"int\" value=\"3\"/>
        <property name=\"last-image\" type=\"string\" value=\"$WALL\"/>
      </property></property>"
  done
  cat > "$C/xfce4-desktop.xml" <<X
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-desktop" version="1.0">
  <property name="backdrop" type="empty"><property name="screen0" type="empty">$mons
  </property></property>
  <property name="desktop-icons" type="empty">
    <property name="style" type="int" value="2"/>
    <property name="icon-size" type="uint" value="48"/>
    <property name="file-icons" type="empty">
      <property name="show-home" type="bool" value="false"/>
      <property name="show-filesystem" type="bool" value="false"/>
      <property name="show-trash" type="bool" value="false"/>
      <property name="show-removable" type="bool" value="false"/>
    </property>
  </property>
</channel>
X
fi
# ---- panel: one slim dark DPHMS top bar (menu | tasks | tray | clock), written as a complete config ----
P=/root/.config/xfce4/xfconf/xfce-perchannel-xml/xfce4-panel.xml
cat > "$P" <<X
<?xml version="1.0" encoding="UTF-8"?>
<channel name="xfce4-panel" version="1.0">
  <property name="configver" type="int" value="2"/>
  <property name="panels" type="array">
    <value type="int" value="1"/>
    <property name="dark-mode" type="bool" value="true"/>
    <property name="panel-1" type="empty">
      <property name="position" type="string" value="p=6;x=0;y=0"/>
      <property name="length" type="uint" value="100"/>
      <property name="position-locked" type="bool" value="true"/>
      <property name="size" type="uint" value="30"/>
      <property name="icon-size" type="uint" value="20"/>
      <property name="background-style" type="uint" value="0"/>
      <property name="background-alpha" type="uint" value="0"/>
      <property name="plugin-ids" type="array">
        <value type="int" value="1"/><value type="int" value="2"/><value type="int" value="3"/>
        <value type="int" value="4"/><value type="int" value="5"/><value type="int" value="6"/>
      </property>
    </property>
  </property>
  <property name="plugins" type="empty">
    <property name="plugin-1" type="string" value="applicationsmenu">
      <property name="button-title" type="string" value="DPHMS"/>
      <property name="show-button-title" type="bool" value="true"/>
      <property name="button-icon" type="string" value="/usr/share/dphms/dphms-icon.png"/>
    </property>
    <property name="plugin-2" type="string" value="tasklist"/>
    <property name="plugin-3" type="string" value="separator">
      <property name="expand" type="bool" value="true"/>
      <property name="style" type="uint" value="0"/>
    </property>
    <property name="plugin-4" type="string" value="systray"/>
    <property name="plugin-5" type="string" value="pulseaudio"/>
    <property name="plugin-6" type="string" value="clock">
      <property name="digital-format" type="string" value="%a %b %d  %I:%M %p"/>
    </property>
  </property>
</channel>
X
# ---- GTK CSS: DPHMS red accent + dark surfaces on every GTK3/4 app (incl. the panel) ----
cat > /root/.config/gtk-3.0/gtk.css <<'X'
/* DPHMS OS theme: dark grey #121212/#1E1E1E, #333 borders, red accent */
@define-color accent_color #FF4444;
@define-color accent_bg_color #FF2A2A;
@define-color theme_selected_bg_color #FF2A2A;
@define-color theme_selected_fg_color #ffffff;
selection, *:selected { background-color: #FF2A2A; color: #ffffff; }
.xfce4-panel, .xfce4-panel button { background-color: #121212; color: #E0E0E0; border-color: #333333; }
.xfce4-panel button:hover { background-color: #1E1E1E; color: #FF4444; }
menu, .menu, menuitem:hover { border-color: #333333; }
menuitem:hover { background-color: #FF2A2A; color: #ffffff; }
tooltip { background-color: #1E1E1E; color: #E0E0E0; border: 1px solid #333333; }
X
cp /root/.config/gtk-3.0/gtk.css /root/.config/gtk-4.0/gtk.css 2>/dev/null
cat > /root/.config/gtk-3.0/settings.ini <<X
[Settings]
gtk-theme-name=${GTK:-Adwaita-dark}
gtk-icon-theme-name=${ICON:-hicolor}
gtk-cursor-theme-name=${CUR:-default}
gtk-cursor-theme-size=48
gtk-application-prefer-dark-theme=1
gtk-font-name=Sans 10
X
# ---- xfce4-terminal in DPHMS colours ----
mkdir -p /root/.config/xfce4/terminal
cat > /root/.config/xfce4/terminal/terminalrc <<'X'
[Configuration]
FontName=Monospace 11
MiscAlwaysShowTabs=FALSE
MiscMenubarDefault=FALSE
ColorForeground=#E0E0E0
ColorBackground=#121212
ColorCursor=#FF2A2A
ColorPalette=#1e1e1e;#ff2a2a;#4caf50;#ffb300;#5c9dff;#c678dd;#56b6c2;#e0e0e0;#333333;#ff5555;#69db7c;#ffd54f;#8ab4ff;#d79cf0;#7fd0d9;#ffffff
X
echo "  theme files written (no D-Bus needed)"
