#!/bin/bash

# #####################################################################
# Copyright (C) 2022 VMware, Inc.  All rights reserved. -- VMware Confidential
#
# ####################################################################

skip_gdm_xsession=

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

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

   case "$DISTRO_ID" in
      ${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" = "yes" ]; then
   xhost +si:localuser:`id -un`
   exec $@
fi

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

exec "$GDM_XSESSION" "$@"
