#!/bin/sh

if [ -z "$HALD_ACTION" ] ; then
    exit 0;
fi

LOG_NAME=$(basename $0)

case $HALD_ACTION in
    add)
	if [ -n "$HAL_PROP_BLOCK_DEVICE" ]; then
	    is_mounted="false"
	    while read fs dir fstype dummy ; do
		if [ "$fs" = "$HAL_PROP_BLOCK_DEVICE" ] ;then
		    is_mounted="true"
		    break;
		fi
	    done < /proc/mounts
	    if [ "$is_mounted" = "false" ] ; then
		initlog -n $LOG_NAME -s "Mounted $HAL_PROP_BLOCK_DEVICE"
		mount "$HAL_PROP_BLOCK_DEVICE"
	    fi
	fi
	;;
    remove)
	if [ -n "$HAL_PROP_BLOCK_DEVICE" ]; then
	    initlog -n $LOG_NAME -s "Trying unmount $HAL_PROP_BLOCK_DEVICE"
	    umount -fl "$HAL_PROP_BLOCK_DEVICE"
	    retval=$?
	    if [ $retval -ne 0 ] ; then
		initlog -n $LOG_NAME -s "Unmount attempt failed. Check for real unmount $HAL_PROP_BLOCK_DEVICE"
	    fi
	    while read fs dir fstype dummy ; do
		if [ "$fs" = "$HAL_PROP_BLOCK_DEVICE" ] ;then
		    initlog -n $LOG_NAME -s "Trying unmount $dir"
		    umount -fl $dir
		    break;
		fi
	    done < /proc/mounts
	    initlog -n $LOG_NAME -s "Unmounted $HAL_PROP_BLOCK_DEVICE"
	fi
	;;
esac
