#!/bin/sh
# Runs the Desktop Profiler frontend that fits the running session: the Qt one (q4os-sw-profiler5)
# under Plasma and anything else that is not Trinity, the TDE one (q4os-sw-profiler) under Trinity -
# whichever of the two is installed when only one is. The first-run setup (85frstrun.sh) and the
# Welcome screens launch the profiler through this, so a Plasma system never has to carry the TDE
# frontend for it. QDSK_SESSION is what the Q4OS session scripts export; print_session.sh derives it
# from the XDG variables when it is not set.
if [ -z "$QDSK_SESSION" ] && [ -f /usr/share/apps/q4os_system/bin/print_session.sh ] ; then
  QDSK_SESSION="$( dash /usr/share/apps/q4os_system/bin/print_session.sh )"
fi
case "$QDSK_SESSION" in
  trinity) WANT="tde" ;;
  ""|unknown)
    # a Trinity session started by tdm carries none of the XDG session variables, but starttde
    # exports TDE_FULL_SESSION (Plasma exports KDE_FULL_SESSION, which stays on the Qt side)
    if [ -n "$TDE_FULL_SESSION" ] ; then WANT="tde" ; else WANT="qt" ; fi ;;
  *) WANT="qt" ;;
esac
if [ "$WANT" = "tde" ] ; then
  PREFER="/usr/bin/swprofiler.exu" ; OTHER="/usr/bin/swprofiler5.exu"
else
  PREFER="/usr/bin/swprofiler5.exu" ; OTHER="/usr/bin/swprofiler.exu"
fi
if [ -x "$PREFER" ] ; then
  exec "$PREFER" "$@"
elif [ -x "$OTHER" ] ; then
  exec "$OTHER" "$@"
fi
echo "desktop-profiler: no Desktop Profiler frontend installed (q4os-sw-profiler or q4os-sw-profiler5)" >&2
exit 127
