#!/bin/sh

if [ $# -ne 1 ]; then
	echo "Usage: $0 cd|fd"
	exit 2
fi

# determine toplevel mount point of amd
fs=`/usr/sbin/amq | grep ' toplvl ' | cut -d' ' -f1`
if [ "$fs" = "" ]; then
	echo "Cannot determine amd toplevel directory"
	exit 2
fi

# append name of medium
case "$1" in
  cd|fd) fs=$fs/$1;;
  *)	 echo "Usage: $0 cd|fd"; exit 2;;
esac

# is the medium mounted?
if /usr/sbin/amq | grep -q "^$fs" >/dev/null 2>&1; then
	# if yes, try to unmount it
	sync
	/usr/sbin/amq -u $fs
	sleep 2
	if /usr/sbin/amq | grep -q "^$fs" >/dev/null 2>&1; then
		# failed, bail out
		echo -n "Cannot unmount $fs; in use by:"
		fuser -uv -m $fs
		echo ""
		exit 1
	fi
else
	echo "$fs not mounted"
fi

case $1 in
  cd)	eject;; # eject CD-ROM
  fd)	echo "Ok to remove disk";;
esac
