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

echo "### Starting MPlayer ###"

# disable console blanking and cursor blinking for a proper MPlayer start
echo -e "\033[9;0]"
echo -e "\033[?25l"

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

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

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

RET=0

# start mplayer with gdb when built with debugging options
if test -x /usr/bin/gdb; then
  echo "r /usr/share/mplayer/background.avi -loop 0" > /gdb_cmd
  gdb -x /gdb_cmd /usr/bin/mplayer
else
  echo "0" > /tmp/mp_result
  # disable fbdev cursor
  echo -e "\033[?1;;c" >/dev/tty$TTY
  chvt $TTY
  (
  # 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
    # kill mencoder if it is still running
    [ -x /usr/bin/mp_recorder ] && mp_recorder
    test `cat /tmp/mp_result` -eq 165 -a -x /usr/bin/fbi && fbi_wrapper
    test `cat /tmp/mp_result` -eq 166 && return 0
    test `cat /tmp/mp_result` -eq 167 && mplayer dvd://1 > /dev/null 2>&1
    test `cat /tmp/mp_result` -eq 168 && return 64
  done
  ) </dev/tty$TTY
  RET=$?
  chvt 1
fi

exit $RET
