#!/bin/sh

#
#    Revised by the author of the post
#    	http://notesofaprogrammer.blogspot.com/2014/09/ubuntu-1404-trust-and-vmware-unity-mode.html
#    	Revised to return gnome with GNOME Fallback on Ubuntu 14.04 and Mate
#    	Desktop environment
#

#
# vmware-xdg-detect-de --
#
#    Determines which Freedesktop.Org compliant desktop environment we're
#    running under.
#
#    usage: vmware-xdg-detect-de
#
#    Simply prints the detected environment (GNOME, KDE, or XFCE).  Shamelessly
#    lifted from detectDE() routine found in many Portland xdg-* scripts.
#

#   Copyright (c) 2010-2014 VMware, Inc.  All rights reserved.
#   Copyright 2009-2010, Fathi Boudra <fabo@freedesktop.org>
#   Copyright 2009-2010, Rex Dieter <rdieter@fedoraproject.org>
#   Copyright 2006, Kevin Krammer <kevin.krammer@gmx.at>
#   Copyright 2006, Jeremy White <jwhite@codeweavers.com>
#
#   LICENSE:
#
#   Permission is hereby granted, free of charge, to any person obtaining a
#   copy of this software and associated documentation files (the "Software"),
#   to deal in the Software without restriction, including without limitation
#   the rights to use, copy, modify, merge, publish, distribute, sublicense,
#   and/or sell copies of the Software, and to permit persons to whom the
#   Software is furnished to do so, subject to the following conditions:
#
#   The above copyright notice and this permission notice shall be included
#   in all copies or substantial portions of the Software.
#
#   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
#   OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
#   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
#   THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR
#   OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
#   ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
#   OTHER DEALINGS IN THE SOFTWARE.
#
#---------------------------------------------

#--------------------------------------
# Checks for known desktop environments
# set variable DE to the desktop environments name, lowercase

# see https://bugs.freedesktop.org/show_bug.cgi?id=34164
unset GREP_OPTIONS

if [ -n "${XDG_CURRENT_DESKTOP}" ]; then
  #
  # This script deals in (case sensitive) desktop environment names as found in
  # menu-spec (http://standards.freedesktop.org/menu-spec/latest/apb.html),
  #
  xdgDE="${XDG_CURRENT_DESKTOP}"
  DE=`echo "${XDG_CURRENT_DESKTOP}" | tr '[:upper:]' '[:lower:]'`
fi

if [ x"$DE" = x"" ]; then
  # classic fallbacks
  if [ x"$KDE_FULL_SESSION" = x"true" ]; then DE=kde;
  elif [ x"$GNOME_DESKTOP_SESSION_ID" != x"" ]; then DE=gnome;
  elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.gnome.SessionManager > /dev/null 2>&1` ; then DE=gnome;
  elif `dbus-send --print-reply --dest=org.freedesktop.DBus /org/freedesktop/DBus org.freedesktop.DBus.GetNameOwner string:org.mate.SessionManager > /dev/null 2>&1` ; then DE=gnome; # Mate is gnome
  elif xprop -root _DT_SAVE_MODE 2> /dev/null | grep ' = \"xfce4\"$' >/dev/null 2>&1; then DE=xfce;
  elif xprop -root 2> /dev/null | grep -i '^xfce_desktop_window' >/dev/null 2>&1; then DE=xfce
  fi
fi

if [ x"$DE" = x"" ]; then
  # fallback to checking $DESKTOP_SESSION
  case "$DESKTOP_SESSION" in
     gnome)
       DE=gnome;
       ;;
     LXDE)
       DE=lxde;
       ;;
     xfce|xfce4)
       DE=xfce;
       ;;
  esac
fi

if [ x"$DE" = x ]; then
   exit 1
fi

if [ -z "$xdgDE" ]; then
   case "$DE" in
   gnome)
      xdgDE=GNOME
      ;;
   kde)
      xdgDE=KDE
      ;;
   xfce)
      xdgDE=XFCE
      ;;
   esac
fi

if [ x"$DE" = "xunity" ] || [ x"$xdgDE" = "xUnity" ]; then
   #
   # if it is Unity, we check and see if it is a fall-back 
   #
   # Reference:
   #     http://unix.stackexchange.com/questions/116539/how-to-detect-the-desktop-environment-in-a-bash-script
   #
   DE=`echo "$XDG_DATA_DIRS" | sed 's/.*\(xfce\|kde\|gnome\).*/\1/'`

   case "$DE" in
   gnome)
      xdgDE=GNOME
      ;;
   kde)
      xdgDE=KDE
      ;;
   xfce)
      xdgDE=XFCE
      ;;
   esac
fi

echo $xdgDE