#!/bin/sh

# Check if the CD drive has already been mounted by mmounter
mmounted=0
if [ "`pidof mmounter`" != "" ] ; then
  if [ "`grep /dev/cdrom /etc/mtab`" != "" ] ; then
    mmounted=1
  fi
fi

if [ $mmounted == 1 ] ; then
  mmumount /dev/cdrom
else
  # I realise this is a really cheap way of making this pull in the
  # tray, but I couldn't think of a more reliable way to do it.
  timetaken=`/usr/bin/time -f %e eject /dev/cdrom 2>&1 | sed "s/\.//"`
  if [ $timetaken -lt 30 ] ; then
    eject -t /dev/cdrom
  fi
fi
