#!/bin/sh
#
# configure and launch mplayer
#
# runlevels: geexbox, debug

echo "### Starting MPlayer ###"

# initialise return values
echo "0" > /tmp/mp_result

# get MPlayer to load config files from /etc/mplayer directory
export MPLAYER_HOME=/etc/mplayer

# tty used for the video display and commands input and debugging
TTY=4
DBGTTY=1

player_loop()
{
  # start mplayer or fbi and keep them launched
  while true; do
    if [ -n "`pidof lircd`" ]; then
      irpty /etc/lircrc -- mp_wrapper
    else
      mp_wrapper
    fi
    test `cat /tmp/mp_result` -eq 165 -a -x /usr/bin/fbi && fbi_wrapper
    test `cat /tmp/mp_result` -eq 166 && break
    test `cat /tmp/mp_result` -eq 167 && ( [ -f /var/dvdnav ] && mplayer dvdnav:// || mplayer dvd:// ) > /dev/null 2>&1
    test `cat /tmp/mp_result` -eq 169 && (chvt $DBGTTY; /bin/sh < /dev/tty$DBGTTY; chvt $TTY)
  done
}

# disable console blanking and cursor blinking for a proper MPlayer start
echo -e "\033[9;0]\033[?25l\033[?1;;c" >/dev/tty$TTY

# disable kernel messages to avoid MPlayer screen corruption
echo 0 > /proc/sys/kernel/printk

# default directory
echo -n /mnt/ > /tmp/mp_current_path

# non-HDTV version
chvt $TTY
player_loop < /dev/tty$TTY

exit 0
