#!/bin/sh
# Show in folder for every account (D-Bus org.freedesktop.FileManager1, which apps call to show a file in its folder):
# Files' own service, Flea's where Flea is installed, else Thunar's (decided 2026-11-08 #45). Each starts the way its
# package registers it, so nothing here names a program's options. A registration whose program is missing or can't
# run (a damaged or half-removed install) is passed over, so Thunar still answers. `flea --default off` gives an
# account Thunar's through its own registration in ~/.local/share/dbus-1/services, which D-Bus reads before this one.
for f in /usr/share/dbus-1/services/com.thisisgm.flea.FileManager1.service \
         /usr/share/dbus-1/services/org.xfce.Thunar.FileManager1.service; do
  line=$(sed -n 's/^Exec=//p' "$f" 2>/dev/null | head -n 1)
  # shellcheck disable=SC2086 # the Exec line is a program and its options: split on purpose
  set -- $line
  [ $# -gt 0 ] || continue
  prog=$(command -v "$1" 2>/dev/null) || continue
  [ -f "$prog" ] && [ -x "$prog" ] || continue
  exec "$@"
done
echo "starchaser-filemanager1: neither Files nor Thunar is installed" >&2
exit 1
