#!/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

# Avoid looping if the session configuration tells us to use gnome-wm or if
# the user forces gnome-wm via WINDOW_MANAGER
if [ "x$WINDOW_MANAGER" = "xgnome-wm" ]; then
  WINDOW_MANAGER=""
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

  KNOWN_WM="metacity mutter sawfish"

  for wm in $KNOWN_WM; do
    if [ -x /usr/bin/"$wm" ]; then
      WINDOW_MANAGER=/usr/bin/"$wm"
      break
    fi
  done

fi

# Look for the default window manager on the system

if [ -z "$WINDOW_MANAGER" ] ; then
  WINDOW_MANAGER=$(readlink /etc/alternatives/x-window-manager 2>/dev/null)
fi

exec "$WINDOW_MANAGER" "$@"

echo "ERROR: No window manager could run!"
