#!/bin/sh
# Runs the WiFi connection tool frontend that fits the running session: the Qt one
# (q4os-wificonnect5) under Plasma and anything else that is not Trinity, the TDE one
# (q4os-wificonnect-trinity) under Trinity - whichever of the two is installed when only one is.
# The first login script (q4os-base, 85frstrun.sh) starts the tool through this.
# Exit code: 0 - connected, 1 - closed without connecting, 127 - no frontend installed.
# QDSK_SESSION is what the Q4OS session scripts export; print_session.sh derives it from the XDG
# variables when it is not set. (The same script as q4os-swcentre-common's software-centre.)
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/wificonnect.exu" ; OTHER="/usr/bin/wificonnect5.exu"
else
  PREFER="/usr/bin/wificonnect5.exu" ; OTHER="/usr/bin/wificonnect.exu"
fi
if [ -x "$PREFER" ] ; then
  exec "$PREFER" "$@"
elif [ -x "$OTHER" ] ; then
  exec "$OTHER" "$@"
fi
echo "wifi-connect: no WiFi connection tool frontend installed (q4os-wificonnect-trinity or q4os-wificonnect5)" >&2
exit 127
