#!/bin/sh
# to be sourced

CONFIG=/etc/sysconfig/compiz
[ -r $CONFIG ] && . $CONFIG

[ "$SESSION" = "failsafe" -o "$SESSION" = "drak3d" ] && COMPIZ=no

if [ "$COMPIZ" = "auto" ]; then
  # auto-start compiz if Xgl is started
  # or if direct rendering and GLX_EXT_texture_from_pixmap are available
  source /etc/init.d/functions
  if grep -q no3ddesktop /proc/cmdline; then
    COMPIZ=no
  elif [ "$XGL" = "yes" ]; then
    COMPIZ=yes
  elif glxinfo 2>/dev/null | awk 'BEGIN { no_direct = 1; no_tfp = 1 } /direct rendering: Yes/ { no_direct = 0 } /GLX_EXT_texture_from_pixmap/ { no_tfp = 0 } END { exit or(no_direct, no_tfp) }'; then
    COMPIZ=yes
  else
    COMPIZ=no
  fi
fi

if [ "$COMPIZ" = "yes" ]; then
  COMPIZ_START=/usr/bin/compiz-start
  case "$SESSION" in
    GNOME)
      export WINDOW_MANAGER=$COMPIZ_START
      ;;
    KDE)
      export KDEWM=$COMPIZ_START
      ;;
    *)
      $COMPIZ_START
      ;;
  esac
fi
