#!/bin/bash

# Copyright (C) 2005, Kay Sievers <kay.sievers@vrfy.org>
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 2.

# Check for environment variables
if [ "$HAL_PROP_BLOCK_DEVICE" == "" ] || [ "$HAL_PROP_INFO_UDI" == "" ] ; then
    echo "Missing or empty environment variable(s)." >&2
    echo "This script should be started by hald." >&2
    exit 1
fi

if [ "$HAL_PROP_VOLUME_IS_MOUNTED" != "true" ]; then
    echo "org.freedesktop.Hal.Device.Volume.NotMounted" >&2
    echo "Device is not mounted." >&2
    exit 1
fi

# read parameters
# "lazy\tforce\n"
# Only allow ^a-zA-Z0-9_= in the string because otherwise someone may
# pass e.g. umask=0600,suid,dev or umask=`/bin/evil`

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

if [ "$GIVEN_UNMOUNTOPTIONS" != "" ]; then
    for OPTION in $GIVEN_UNMOUNTOPTIONS; do
	OPTION_WAS_OK="0"
	for VALID_OPTION in $HAL_PROP_VOLUME_UNMOUNT_VALID_OPTIONS; do
	    if [ "$OPTION" == "$VALID_OPTION" ]; then
		OPTION_WAS_OK="1"
		break
	    fi
	done

	if [ "$OPTION_WAS_OK" == "1" ]; then
		case "$OPTION" in
		    "lazy")
			UNMOUNTOPTIONS="$UNMOUNTOPTIONS -l"
			OPTION_WAS_OK="1"
			;;
		    "force")
			UNMOUNTOPTIONS="$UNMOUNTOPTIONS -f"
			OPTION_WAS_OK="1"
			;;
		    *)
			echo "org.freedesktop.Hal.Device.Volume.UnsupportedUnmountOption" >&2
			echo "The option '$OPTION' is not supported" >&2
			exit 1
		esac
	else
		echo "org.freedesktop.Hal.Device.Volume.InvalidUnmountOption" >&2
		echo "The option '$OPTION' is invalid" >&2
		exit 1
	fi
    done
fi

RESULT=$(umount $UNMOUNTOPTIONS "$HAL_PROP_VOLUME_MOUNT_POINT" 2>&1)
if [ $? -ne 0 ]; then
    case "$RESULT" in
	*busy*)
	    echo "org.freedesktop.Hal.Device.Volume.Busy" >&2
	    echo "Device is busy." >&2
	    ;;
	*"not mounted"*)
	    echo "org.freedesktop.Hal.Device.Volume.NotMounted" >&2
	    echo "Device is not mounted." >&2
	    ;;
	*)
	    echo "org.freedesktop.Hal.Device.Volume.UnknownFailure" >&2
	    echo "Unknown failure." >&2
    esac
    exit 1
fi

# remove directory only if HAL has created it
if [ -e $HAL_PROP_VOLUME_MOUNT_POINT/.created-by-hal ]; then
  rm -f $HAL_PROP_VOLUME_MOUNT_POINT/.created-by-hal
  rmdir --ignore-fail-on-non-empty "$HAL_PROP_VOLUME_MOUNT_POINT"
fi

exit 0
