#!/bin/sh
#
# eject CD if booted from cdrom and their is no media files on it
#
# runlevels: geexbox, debug

if test -n "$CDROM"; then
  echo "### Eject CDROM ###"

  (
  # add recognized files extension to the EXTS list
  for i in `cat /etc/file_ext`; do
    if [ -z "$EXTS" ]; then
        EXTS="$i"
    else
        EXTS="$EXTS\|$i"
    fi
  done
  # add recognized images extension to the EXTS list
  if [ -x /usr/bin/fbi -a -e /dev/fb0 ]; then
    for i in `cat /etc/img_ext`; do EXTS="$EXTS\|$i"; done
  fi
  # add recognized playlists extension to the EXTS list
  for i in `cat /etc/list_ext`; do EXTS="$EXTS\|$i"; done
  # check if their is recognized media files on the CD
  if [ -z "`find "$CDROM" | grep -v /GEEXBOX/ | grep -i "\.\($EXTS\)\$"`" ]; then
    # eject the CD (except when started with make exec)
    [ -e /dev/cdrom -a ! -f /EXEC ] && eject
  fi
  )&
fi

exit 0
