#!/bin/bash
#   userful-storage-unmount
#
#   Jason Kim <jason@userful.com>
#
#   Copyright (c) 2002-2007 by Userful Corporation
#
#   Wrapper script for hal unmount program to control permission.
##########################

logfile="/var/log/userful-start.log"
source /etc/userful/os-info

function check_arch()
{
    case $(uname -m) in
        x86_64|ia64)
        echo "lib64"
        ;;
    *)
        echo "lib"
        ;;
    esac
}

# Check INVOKER of unmount and Original MOUNTER
if [ "$HAL_PROP_INFO_HAL_MOUNT_MOUNTED_BY_UID" != "" ]; then
    if [ "$HAL_PROP_INFO_HAL_MOUNT_MOUNTED_BY_UID" != "$HAL_METHOD_INVOKED_BY_UID" ]; then
	    echo "`date +"%b %d %T"` (unmount $HAL_PROP_BLOCK_DEVICE) Permission denied." >> $logfile
		echo "org.freedesktop.Hal.Device.Volume.PermissionDenied" >&2
		echo "The device was mounted by another USER." >&2
		exit 1
    fi
fi

LANG=en_US.UTF-8

read GIVEN_UNMOUNTOPTIONS
GIVEN_UNMOUNTOPTIONS=${GIVEN_UNMOUNTOPTIONS//[^a-zA-Z0-9_=[:space:]]/_}

echo "`date +"%b %d %T"` (unmount $HAL_PROP_BLOCK_DEVICE) Given Unmount option:$GIVEN_UNMOUNTOPTIONS" >> $logfile

LIB=$(check_arch)

##
## Find hal-storage-unmount and run it.
##
HAL_UNMOUNT_COMMAND=
if [[ -e ./hal-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="./hal-storage-unmount"

elif [[ -e ./hal-system-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="./hal-system-storage-unmount"

elif [[ -e /usr/$LIB/hal/scripts/hal-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="/usr/$LIB/hal/scripts/hal-storage-unmount"

elif [[ -e /usr/$LIB/hal/scripts/hal-system-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="/usr/$LIB/hal/scripts/hal-system-storage-unmount"

elif [[ -e /usr/libexec/hal-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="/usr/libexec/hal-storage-unmount"

elif [[ -e /usr/libexec/hal-system-storage-unmount ]]; then
    HAL_UNMOUNT_COMMAND="/usr/libexec/hal-system-storage-unmount"
fi

echo -e "$GIVEN_UNMOUNTOPTIONS\n" | $HAL_UNMOUNT_COMMAND
