#!/bin/bash

# whitelist of direct rendering support devices
T="80ee:beef" # VirtualBox Graphics Adapter
DIRECT_PCIIDS="$T"
unset T

function runCompiz() {
	gtk-window-decorator --replace &
	exec compiz --ignore-desktop-hints gconf ccp $@
}

DIRECT=`glxinfo | grep "direct rendering: " | head -n 1 | cut -d " " -f 3`
ISSW=`glxinfo | grep "Software Rasterizer" -c`

# check direct rendering whitelist
found=0
for id in $DIRECT_PCIIDS ;do
  if (lspci -n |grep -q "$id"); then
    found=1
  fi
done

if [ "$found" = "1" ]; then
    unset LIBGL_ALWAYS_INDIRECT
else
    export LIBGL_ALWAYS_INDIRECT=1
fi

HAVETFP=`glxinfo | grep texture_from_pixmap -c`

if ( [ $DIRECT == "Yes" ] && [ $ISSW == 0 ] && [ $HAVETFP -gt 2 ] ); then 
	runCompiz $@
 fi

# Fall back to metacity
exec metacity $@ &
