#!/bin/sh
# Script used by kdesktop to eject a removable media (CDROM/Tape/SCSI/Floppy)
# Relies on the 'eject' program, 'cdcontrol' on *BSD
#
# Copyright GPL v2 by David Faure <david@mandrakesoft.com>
#

eject_fallback ()
{
       echo "FALL BACK TO EJECT: $1"
       eject $1
}

fatal_error ()
{
       echo "FATAL ERROR: $1"
       exit 1;
}

quiet=0
if test "$1" = "-q"; then
  quiet=1
  shift
fi

if test "$1" = "--help"; then
  "Usage: $0 <name> where name is a device or a mountpoint."
   exit 0
fi

if test -z "$1"; then
  for dev in /dev/cdrom /dev/dvd /dev/dvdram /dev/cdrecorder; do
     if test -e $dev; then
        lp=`readlink $dev`
	if test -n "$lp"; then
	    device=/dev/$lp
	else
	    device=$dev
        fi
        break
    fi
  done
else
  device=`readlink -m -n $1`
fi

echo "Trying to eject device $device ...."
 
# check if HAL userland tools are installed
HAL_FIND_BY_PROP=""
HAL_GET_PROP=""
for dir in /usr/bin /usr/sbin /bin /sbin ; do
	if test -e "${dir}/hal-find-by-property" && test -x "${dir}/hal-find-by-property"; then	
		HAL_FIND_BY_PROP="${dir}/hal-find-by-property";
	fi
	if test -e "${dir}/hal-get-property" && test -x "${dir}/hal-get-property"; then	
		HAL_GET_PROP="${dir}/hal-get-property";
	fi
	if test -n "$HAL_FIND_BY_PROP" && test -n "$HAL_GET_PROP"; then		
		break;
	fi
done
if test -n "$HAL_FIND_BY_PROP" && test -n "$HAL_GET_PROP"; then		
       HAL_major=`${HAL_FIND_BY_PROP} --version | cut -d " " -f 2 | cut -d. -f1`
       HAL_minor=`${HAL_FIND_BY_PROP} --version | cut -d " " -f 2 | cut -d. -f2`
  # HAL umount and eject method exist only for HAL >= 0.5
       if test $HAL_major -eq 0 -a  $HAL_minor -ge 5 -o $HAL_major -gt 0;  then
         # Try to find the UDi from the device name
               BLOCK_UDI=$(${HAL_FIND_BY_PROP} --key block.device --string "$device" | head -1)
               [ -n "$BLOCK_UDI" ] || fatal_error "Can't find UDI for URL $device"
               STORAGE_UDI=$(${HAL_GET_PROP} --udi $BLOCK_UDI --key block.storage_device)
               [ -n "$STORAGE_UDI" ] || fatal_error "Can't find device for volume $BLOCK_UDI"

         # Unmount each device volume
               for VOLUME_UDI in $(${HAL_FIND_BY_PROP} --key block.storage_device --string $STORAGE_UDI); do
                       [ $(${HAL_GET_PROP} --udi $VOLUME_UDI --key block.is_volume) = true ] || continue
                       [ $(${HAL_GET_PROP} --udi $VOLUME_UDI --key volume.is_mounted) = true ] || continue
                       ERROR=$(dcop kded mediamanager unmount $VOLUME_UDI)
                       [ -n "$ERROR" ] && error "$ERROR"
               done
               [ $(${HAL_GET_PROP} --udi $VOLUME_UDI --key storage.requires_eject) = true ] &&
               dbus-send --system --dest=org.freedesktop.Hal "$BLOCK_UDI" org.freedesktop.Hal.Device.Volume.Eject array:string:"" >/dev/null 2>&1
       else
    # fallback to the old eject method
               eject_fallback $device
       fi
else
  # fallback to the old eject method
  eject_fallback $device
fi

# refresh icons if media eject succeeded
if test $? -eq 0; then
  dcop kdesktop default refreshIcons
elif test $quiet -eq 0; then
  fatal_error "Eject $device failed!"
fi

exit 0
