#!/bin/sh
#130802 BK first version.

MYVIDOUT="xv"
XSERVER="$(readlink /usr/bin/X)"

[ "$XSERVER" = "Xvesa" ] && MYVIDOUT="x11"

VIDEODRIVER='vesa'
if [ "$XSERVER" = "Xorg" ];then
 #xorg.conf does not necessarily tell us what driver is loaded.
 #got this code from /usr/sbin/report-video...
 LOADED="$(grep 'Loading .*/xorg/modules/drivers/.*_drv.so' /var/log/Xorg.0.log | rev | cut -f 1 -d '/' | rev | cut -f 1 -d '_' | tr '\n' ' ')"
 for ALOADED in $LOADED
 do
  aPTN="UnloadModule: \"$ALOADED\""
  bPTN="LoadModule: \"$ALOADED\""
  CNTload=`grep "$bPTN" /var/log/Xorg.0.log | wc -l`
  CNTunload=`grep "$aPTN" /var/log/Xorg.0.log | wc -l`
  [ $CNTload -eq 0 ] && continue
  [ $CNTunload -ge $CNTload ] && continue
  VIDEODRIVER="$ALOADED"
  break
 done
fi
case $VIDEODRIVER in
 vesa) MYVIDOUT="x11" ;;
 modesetting) MYVIDOUT="x11" ;;
esac

if [ "$MYVIDOUT" = "x11" ];then
 if [ "$(grep "<default>'x11'</default>" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml)" = "" ];then
 if [ "$(grep "<default>'xv'</default>" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml)" != "" ];then
  sed -i -e "s%<default>'xv'</default>%<default>'x11'</default>%" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml
  [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
 fi
 fi
else
 if [ "$(grep "<default>'xv'</default>" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml)" = "" ];then
 if [ "$(grep "<default>'x11'</default>" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml)" != "" ];then
  sed -i -e "s%<default>'x11'</default>%<default>'xv'</default>%" /usr/share/glib-2.0/schemas/apps.gnome-mplayer.preferences.gschema.xml
  [ -e /usr/bin/glib-compile-schemas ] && /usr/bin/glib-compile-schemas /usr/share/glib-2.0/schemas
 fi
 fi
fi
#...i think gnome-mplayer will only read this file first time it starts. then it saves its prefs in compiled dconf i think.

#turn off screensaver...
TURNONAFTER=0
if [ -e /root/.xset.sh ];then
 if [ "$(grep '^xset .* s off' /root/.xset.sh)" = "" ];then
  xset s off -dpms
  TURNONAFTER=1
 fi
else
 xset s off -dpms
 TURNONAFTER=1
fi
gnome-mplayer "$@" ###RUN IT###
[ $TURNONAFTER -eq 1 ] && xset s on

###END###
