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

# do not start if Freevo is present
if test -x /usr/bin/freevo; then
  exit 0
fi

echo "### Starting MPlayer ###"

# still display mplayer output while in debug mode
grep -q debugging /proc/cmdline && mp_set_option really-quiet no

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

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

# default directory
echo -n /mnt/ > /tmp/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 -f /etc/debug; 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[9;0]\033[?25l\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
    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
    test `cat /tmp/mp_result` -eq 169 && (chvt 1; /bin/sh < /dev/tty1; chvt $TTY)
  done
  ) </dev/tty$TTY
  RET=$?
  chvt 1
fi

exit $RET
