#!/bin/sh
#
# configure MPlayer's menu
#
# runlevels: geexbox, debug, configure

echo "### Configuring MPlayer's menu ###"

# remove CD from menu if no cd drive is available
test -e /dev/cdrom || sed -i 's/.*loadfile vcd.*//' /etc/mplayer/menu.conf
test -e /dev/cdrom || sed -i 's/.*loadfile cdda.*//' /etc/mplayer/menu.conf

# remove DVD from the menu if no dvd drive is available
test -e /dev/dvd || sed -i 's/.*play_dvd.*//' /etc/mplayer/menu.conf

if [ -e /var/use_xorg ]; then
  test -x /usr/bin/feh  || ( echo -n "" > /etc/img_ext; sed -i 's/.*set_menu view_img_timeout.*//' /etc/mplayer/menu.conf )
else
  test -x /usr/bin/fbi -a -e /dev/fb0 || ( echo -n "" > /etc/img_ext; sed -i 's/.*set_menu view_img_timeout.*//' /etc/mplayer/menu.conf )
fi

# disable audio device switching if no respective bluetooth devices are defined in /etc/asound.conf

if [ -e /etc/asound.conf ]; then
  if ! grep -q ^pcm.headset /etc/asound.conf && ! grep -q ^pcm.bluetooth /etc/asound.conf; then
    sed -i 's/.*set_menu ao_switch.*//' /etc/mplayer/menu.conf
  else
    if ! grep -q ^pcm.headset /etc/asound.conf; then
    sed -i 's/.*device=headset.*//' /etc/mplayer/menu.conf
    fi
    if ! grep -q ^pcm.bluetooth /etc/asound.conf; then
    sed -i 's/.*device=bluetooth.*//' /etc/mplayer/menu.conf
    fi
  fi
else
  sed -i 's/.*set_menu ao_switch.*//' /etc/mplayer/menu.conf
fi

# disable obex options if no config file found or options disabled in config file

if ! [ -e /etc/obex ]; then
  sed -i 's/.*set_menu obex_settings.*//' /etc/mplayer/menu.conf
else
  . /etc/obex
  if [ "$OBEXFS_ENABLE" = "true" -o "$OBEXSERVER_ENABLE" = "true" ]; then
    if ! [ "$OBEXFS_ENABLE" = "true" -a -x /usr/bin/obexfs ]; then
      sed -i 's/.*mount_mobile.*//' /etc/mplayer/menu.conf && sed -i 's/.*obexfs.*//' /etc/mplayer/menu.conf
    fi
    if ! [ "$OBEXSERVER_ENABLE" = "true" -a -x /usr/bin/sobexsrv ]; then
      sed -i 's/.*start_obex_server.*//' /etc/mplayer/menu.conf && sed -i 's/.*sobexsrv.*//' /etc/mplayer/menu.conf
    fi
  else
    sed -i 's/.*set_menu obex_settings.*//' /etc/mplayer/menu.conf
  fi
fi

# disable mount cd/dvd image options if no fuseiso installed

#if ! [ -x /usr/bin/fuseiso ]; then
#  sed -i 's/.*set_menu cdvdimg_settings.*//' /etc/mplayer/menu.conf
#fi

# disable autoplay

if ! [ -x /usr/bin/autoplay ]; then
  sed -i 's/.*set_menu autoplay_mode.*//' /etc/mplayer/menu.conf
fi

[ -x /codecs/unrar -a -f /usr/lib/libstdc++.so.* ] \
  && mp_set_option unrarexec /codecs/unrar \
  && ( [ -e /usr/lib/libstdc++.so.6 ] || ln -s /usr/lib/libstdc++.so.5 /usr/lib/libstdc++.so.6 )

# build a single file extensions filter
cat /etc/file_ext > /etc/extensions
cat /etc/list_ext >> /etc/extensions
cat /etc/img_ext >> /etc/extensions
cat /etc/cdvdimg_ext >> /etc/extensions

exit 0
