#!/bin/bash

###################################################
# Copyright (c) Omnissa, LLC. All rights reserved.
# This product is protected by copyright and intellectual property laws in the
# United States and other countries as well as by international treaties.
# -- Omnissa Restricted
###################################################

skip_gdm_xsession=no

if [ "$1" = "-k" ]; then
   shift

   . /usr/lib/omnissa/viewagent/commonlib.sh
   identify_distribution
   define_constants

   case "$DISTRO_ID" in
      ${DISTRO_ID_RHEL_ROCKY}|\
      ${DISTRO_ID_RHEL_CLIENT}|\
      ${DISTRO_ID_RHEL_SERVER}|\
      ${DISTRO_ID_RHEL_WORKSTATION})
      if [ "$OS_MAJOR" -ge 8 ]; then
         skip_gdm_xsession=yes
      fi
   esac
fi

DBUSLAUNCH=/usr/bin/dbus-launch

if [ -z "$DBUS_SESSION_BUS_ADDRESS" ] && [ -x "$DBUSLAUNCH" ]; then
   eval $($DBUSLAUNCH --exit-with-session --sh-syntax)
fi

GDM_XSESSION=

if [ "$skip_gdm_xsession" = no ]; then
   for path in /etc/gdm{3,}/Xsession
   do
      if [ -x "$path" ]
      then
         GDM_XSESSION="$path"
         break
      fi
   done
fi

if [ -z "$GDM_XSESSION" ]; then
   xhost +si:localuser:`id -un`
   exec $@
else
   exec "$GDM_XSESSION" "$@"
fi
