#!/bin/sh

PTT_DEVICE="$1"
if [ "x$PTT_DEVICE" == "x" ]; then
	echo -n "USB Device to be ejected/powered off (eg: sdb): "
	read PTT_DEVICE
fi
if [ "x$PTT_DEVICE" == "x" ]; then
	exit
fi

# sg stop
case "$PTT_DEVICE" in
	sdb|sdc|sdd|sde)
		# check mount
		PTT_MNT=`mount | grep $PTT_DEVICE`
		if [ "$PTT_MNT" != "" ]; then
			echo "Some devices are still mounted! aborting"
			exit
		fi
		echo -n "Spinning down $PTT_DEVICE..."
		sg_start -r -S /dev/$PTT_DEVICE && echo " done. Safe to be removed." \
			&& (notify-send "`basename $0`" "$PTT_DEVICE is safe to be removed." &)
		;;
	*)
		echo "$PTT_DEVICE is not a valid disk."
		;;
esac
		
