#!/bin/sh
#BK try fix some problems with running mplayer and gmplayer.
#100821 BK: improve DVD detection.
#101007 test for 16-bit color depth. no, removed.
#101218 fixes.
#130301 exact determination of running xorg driver.
#130301 turn off screen blanking.
#150207 modesetting xorg driver needs '-vo x11'

##101007 test color depth...
#COLORDEPTH="`xwininfo -root | grep -o 'Depth: [0-9][0-9]' | rev | cut -f 1 -d ' ' | rev`"
#if [ "$COLORDEPTH" = "16" ];then
# yaf-splash -close box -bg pink -placement center -text "NOTICE: the display color depth is set to 16 bits. Mplayer requires 24-bit depth to work. You will need to run the Xorg Wizard again. (sorry about that, lobby the mplayer developers if you are unhappy about this restriction)" &
#fi

MYVIDOUT="-vo xv"
[ "`readlink /usr/bin/X`" = "Xvesa" ] && MYVIDOUT="-vo x11" #101218

#130301 exact determination of running xorg driver...
#[ -f /etc/X11/xorg.conf ] && [ "`grep 'card0driver' /etc/X11/xorg.conf | grep '"vesa"'`" != "" ] && MYVIDOUT="-vo x11"
VIDEODRIVER='vesa'
if [ "`readlink /usr/bin/X`" = "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
if [ "$VIDEODRIVER" ];then #150207
 case $VIDEODRIVER in
  vesa) MYVIDOUT="-vo x11" ;;
  modesetting) MYVIDOUT="-vo x11" ;; #150207
 esac
fi

PREFGUI="gmplayer"
#[ "`which gimv`" != "" ] && PREFGUI="gimv"

#if a specific file passed, play that...
if [ "${1}" ];then #101218 need quotes if passed param has spaces (rox passes $1 in quotes).
 [ "$PREFGUI" = "gimv" ] && exec gimv "$@"
 #exec gmplayer -really-quiet $MYVIDOUT "$@"
 xset s off -dpms #130301
 nohup gmplayer -really-quiet $MYVIDOUT "$@" &
 xset s on #130301
 exit
fi

#100821 detect if a dvd inserted (code similar in /usr/local/bin/drive_all)...
if [ -e /dev/dvd ];then
 if [ "`cddetect -d/dev/dvd | grep -E 'no disc|tray open'`" = "" ];then
  ONEDRVNAME="`readlink /dev/dvd | rev | cut -f 1 -d '/' | rev`"
  dPATTERN='^/dev/'"$ONEDRVNAME"' '
  if [ "`df | grep "$dPATTERN"`" = "" ];then #only test if not mounted.
   FLAGDVDVIDEO="`dvd+rw-mediainfo /dev/${ONEDRVNAME} | grep 'Mounted Media: .* DVD-ROM'`"
   if [ "$FLAGDVDVIDEO" = "" ];then
    #mount and look for 'video_ts' directory (dir should contain file video_ts.ifo)...
    mkdir -p /mnt/${ONEDRVNAME}
    mount -t iso9660 /dev/${ONEDRVNAME} /mnt/${ONEDRVNAME}
    if [ $? -eq 0 ];then
     [ -d /mnt/${ONEDRVNAME}/video_ts -o -d /mnt/${ONEDRVNAME}/VIDEO_TS ] && FLAGDVDVIDEO="yes"
     umount /mnt/${ONEDRVNAME}
    fi
   fi
   if [ "$FLAGDVDVIDEO" != "" ];then
    [ -f /usr/local/bin/defaultdvdplayer ] && exec defaultdvdplayer
    xset s off -dpms #130301
    nohup gmplayer $MYVIDOUT dvdnav:// &
    xset s on #130301
    exit
   fi
  fi
 fi
fi

#CDTYPE1="`cddetect -d/dev/dvd | grep 'cdtype: audio'`"
#CDTYPE2="`cddetect -d/dev/cdrom | grep 'cdtype: audio'`"
#[ "$CDTYPE1" != "" ] && exec gmplayer $MYVIDOUT 

#fall down...
#exec gmplayer -really-quiet $MYVIDOUT
xset s off -dpms #130301
nohup gmplayer -really-quiet $MYVIDOUT &
xset s on #130301

###END###
