#!/bin/sh

# The user can specify his prefered WM by setting the WINDOW_MANAGER
# environment variable.
#
# If this is not set, we search a list of known windowmanagers and use
# the first one that is found in the users's PATH
#
# NOTE: DON'T USE THIS.  Please have your window manager install
# a desktop file and change the gconf key
# /desktop/gnome/session/required_components/windowmanager
#
# 16-05-2009 - Added check for indirect rendering for compiz (stevek)
# 25-08-2008 - Grab default window manager from gconf (stevek)
# 19-03-2008 - Prefer emerald window decorator if installed (stevek)
# 07-02-2008 - Modified to use compiz ccp by default in GSB (stevek)


# sm-client-id value
SMID=default1
# default-wm value
DEFWM=
GLXINFO=/usr/bin/glxinfo
# no indirect by default
INDIRECT=no

# check for texture_from_pixmap in GLX
check_for_texture()
{
if [ $($GLXINFO 2>/dev/null | grep GLX_EXT_texture_from_pixmap -c) -gt 2 ] ; 
then
  return 0;
else
  if [ "$INDIRECT" = "yes" ]; then
    unset LIBGL_ALWAYS_INDIRECT
    INDIRECT="no"
    return 1;
  else
    INDIRECT="yes"
    export LIBGL_ALWAYS_INDIRECT=1
    check_for_texture;
    return $?
  fi
fi
}

#read in the arguments
GET=
for n in "$@" ; do
  case "$GET" in
    smid)
      SMID=$n
      GET=
      ;;
    defwm)
      DEFWM=$n
      GET=
      ;;
    *)
      case "$n" in
        --sm-client-id)
          GET=smid
          ;;
        --default-wm)
          GET=defwm
          ;;
      esac
      ;;
  esac
done

# WINDOW_MANAGER overrides all
if [ -z "$WINDOW_MANAGER" ] ; then
    WINDOW_MANAGER=`gconftool-2 --get /desktop/gnome/session/required_components/windowmanager 2> /dev/null`
fi

# Avoid looping if the session configuration tells us to use gnome-wm or if
# the user forces gnome-wm via WINDOW_MANAGER; use Metacity by default.
if [ "x$WINDOW_MANAGER" = "xgnome-wm" ]; then
  WINDOW_MANAGER="metacity"
fi

if [ -z "$WINDOW_MANAGER" ] ; then
  # Create a list of window manager we can handle, trying to only use the
  # compositing ones when it makes sense

  xdpyinfo 2> /dev/null | grep -q "^ *Composite$" 2> /dev/null
  IS_X_COMPOSITED=$?

  KNOWN_WM="sawfish sawmill enlightenment icewm wmaker fvwm2 qvwm fvwm twm kwm compiz"
  # metacity is still the default wm in GNOME
  KNOWN_WM="metacity $KNOWN_WM"

  OLDIFS=$IFS
  if [ -z "$DEFWM" -o "x$DEFWM" = "xgnome-wm" ]; then
    for wm in $KNOWN_WM ; do
      IFS=":"
      for dir in $PATH ; do
	if [ -x "$dir/$wm" ] ; then
	  WINDOW_MANAGER="$dir/$wm"
    	  break 2
	fi
      done
      IFS=$OLDIFS
    done
  else
    WINDOW_MANAGER=$DEFWM
  fi
  IFS=$OLDIFS
fi

# If no window manager can be found, we default to xterm

if [ -z "$WINDOW_MANAGER" ] ; then
  echo "WARNING: No window manager can be found."
  WINDOW_MANAGER=xterm
fi

# Now create options OPT1, OPT2 and OPT3 based on the windowmanager used
OPT1=
OPT2=
OPT3=
OPT4=
if [ ! -z "$SMID" ] ; then
  case `basename $WINDOW_MANAGER` in
    sawfish|sawmill|metacity)
      OPT1="--sm-client-id=$SMID"
      ;;
    openbox|enlightenment|e16)
      OPT1=--sm-client-id
      OPT2=$SMID
      ;;
    twm)
      OPT1=-clientId
      OPT2=$SMID
      ;;
    lwm)
      OPT1=-s
      OPT2=$SMID
      ;;
    fvwm)
      OPT1=-i
      OPT2=$SMID
      ;;
    compiz)
      OPT1="--sm-client-id=$SMID"
      # if we don't have texture, revert to metacity
      if ! check_for_texture ; then
	      WINDOW_MANAGER="metacity" ;
      else
	# add required compiz switch if glx is indirect
        if [ "${INDIRECT}" = "yes" ]; then
         OPT2=--indirect-rendering ;
        fi;
      fi;
      OPT3=ccp 
      ;;
    #FIXME: add all other windowmanagers here with their proper options
  esac
fi

case `basename $WINDOW_MANAGER` in
  compiz)
    if [ "${INDIRECT}" = "no" ]; then
      export LIBGL_ALWAYS_INDIRECT=1
    fi;
    if [ -x /usr/bin/emerald ]; then
      /usr/bin/emerald --replace &
    else
      gtk-window-decorator --replace &
    fi;
    ;;
  beryl)
    emerald &
    ;;
esac

exec $WINDOW_MANAGER $OPT1 $OPT2 $OPT3 $OPT4

echo "ERROR: No window manager could run!"