#!/bin/sh

# The user can specify his prefered WM by setting the WINDOW_MANAGER
# environment variable or setting the
# /desktop/gnome/applications/window_manager/default gconf key.
#
# 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

# sm-client-id value
SMID=
# default-wm value
DEFWM=

#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

# Migrate compiz to compiz-manager if possible and needed
if [ "x$WINDOW_MANAGER" = "xcompiz" -o "x$DEFWM" = "xcompiz" ]; then
  which compiz-manager > /dev/null 2>&1
  if [ $? -eq 0 ]; then
    if [ "x$WINDOW_MANAGER" = "xcompiz" ]; then
      WINDOW_MANAGER="compiz-manager"
    fi
    if [ "x$DEFWM" = "xcompiz" ]; then
      DEFWM="compiz-manager"
    fi
  fi
fi

# Get previously set window manager in gconf
if [ ! "$DEFWM" ]; then
  DEFWM=`gconftool-2 -g /desktop/gnome/applications/window_manager/default 2>/dev/null`
fi

# special case handling for dapper upgrades (this runs only once after the upgrade)
if [ -z "$DEFWM" ] && [ -f /var/lib/gnome-session/dapper-upgrade ]; then
    gconftool-2 -s /desktop/gnome/applications/window_manager/default /usr/bin/metacity --type string
    DEFWM=/usr/bin/metacity
fi

# If not exist, set to compiz (if available) 
if [ ! -x "$DEFWM" ]; then
    if [ -x "/usr/bin/compiz" ]; then
	gconftool-2 -s /desktop/gnome/applications/window_manager/default /usr/bin/compiz --type string
	DEFWM=/usr/bin/compiz
    elif [ -x "/usr/bin/metacity" ]; then
	gconftool-2 -s /desktop/gnome/applications/window_manager/default /usr/bin/metacity --type string
	DEFWM=/usr/bin/metacity
    else
	unset DEFWM
    fi
fi

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

  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"
  if [ $IS_X_COMPOSITED -eq 0 ] ; then
    KNOWN_WM="compiz beryl $KNOWN_WM"
  fi
  # 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

# 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

# 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|mutter)
      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|fvwm2)
      OPT1=-i
      OPT2=$SMID
      ;;
    compiz|compiz-manager)
      OPT1=--sm-client-id
      OPT2=$SMID
      ;;
    beryl)
      OPT1=--sm-client-id
      OPT2=$SMID
      ;;
    #FIXME: add all other windowmanagers here with their proper options
  esac
fi

case `basename $WINDOW_MANAGER` in
  compiz)
    #gtk-window-decorator &
    #OPT3=glib
    #OPT4=gconf
    ;;
  beryl)
    emerald &
    ;;
esac

# Store the selected WM with gconf
gconftool-2 -t string -s /desktop/gnome/applications/window_manager/current "$WINDOW_MANAGER"

exec "$WINDOW_MANAGER" $OPT1 $OPT2 $OPT3 $OPT4

echo "ERROR: No window manager could run!"
