# The session of Total.js Developer. Each line is one thing that starts with Openbox.
xsetroot -cursor_name left_ptr &
# the compositor: GLX on a real computer, xrender in a virtual machine or a container (without a real GPU, a GL window such as the
# terminal stays see-through under a GLX compositor, and xrender needs no 3D). It starts BEFORE the panel and the session waits for it:
# the panel restarts itself when a compositor appears, and in that moment it could end (see tint2-exits.log)
if ! pgrep -u "$(id -u)" -x picom >/dev/null; then
  if [ "$(totaljs-picom-backend)" = xrender ]; then BACKEND="--backend xrender --no-vsync"; else BACKEND="--backend glx"; fi
  picom --config "$HOME/.config/picom/picom.conf" $BACKEND --daemon
  for _ in 1 2 3 4 5 6; do pgrep -u "$(id -u)" -x picom >/dev/null && break; sleep 0.5; done
  pgrep -u "$(id -u)" -x picom >/dev/null || picom --config "$HOME/.config/picom/picom.conf" --backend xrender --no-vsync --daemon
  sleep 0.5
fi
totaljs-wallpaper &
# the widget on the wallpaper (the profile that the person chose; the package totaljs-conky provides it)
command -v totaljs-conky >/dev/null && totaljs-conky restore &
# the panel is started again if it ends (a panel that is gone leaves the session without a way to reach anything), and how it ended is kept
pgrep -u "$(id -u)" -x tint2 >/dev/null || (
  mkdir -p "$HOME/.cache"
  # only while the Openbox that started it is alive: when the person logs out, this loop has to end with the session (it would start a second panel in the next one)
  OPENBOX="$(pgrep -u "$(id -u)" -x openbox | head -1)"
  while [ -n "${OPENBOX}" ] && kill -0 "${OPENBOX}" 2>/dev/null; do
    tint2 -c "$HOME/.config/tint2/tint2rc"
    echo "$(date +%T) tint2 ended with status $?" >> "$HOME/.cache/tint2-exits.log"
    sleep 2
  done
) &
# the tray: once each. The autostart entries of the system (/etc/xdg/autostart) would start them a second time, and they are turned off in ~/.config/autostart
totaljs-launch-once nm-applet nm-applet &
# pasystray talks to the audio server (pipewire-pulse): if it starts before the server answers it shows an icon of "not connected" (a white disc) and does not recover,
# so it waits for the server (up to a minute)
if command -v pactl >/dev/null 2>&1; then
  ( for _ in $(seq 1 60); do pactl info >/dev/null 2>&1 && break; sleep 1; done; totaljs-launch-once pasystray pasystray ) &
else
  totaljs-launch-once pasystray pasystray &
fi
# Openbox has no agent of Polkit of its own, and the tools of the desktop that need a password ask for it through one
for agent in /usr/lib/policykit-1-gnome/polkit-gnome-authentication-agent-1 /usr/lib/polkit-gnome/polkit-gnome-authentication-agent-1; do
  if [ -x "$agent" ]; then totaljs-launch-once polkit-gnome-authentication-agent-1 "$agent" & break; fi
done
totaljs-launch-once dunst dunst &
# Openbox started by LightDM sometimes loads its configuration "cold" on the first login (window decorations and menus that do nothing until a reconfigure):
# once the session has settled, it loads it again
( sleep 3; openbox --reconfigure ) &
