#!/bin/sh
# uam -- pmount helper script
# (c) 2008-2011 Michał Górny
# Released under the terms of the 3-clause BSD license

LIBDIR=/usr/lib/udev/uam

. "${LIBDIR}"/uam-common.sh

unset DEVNAME DEVPATH

find_devpath() {
	local x hadargterm
	hadargterm=

	for x; do
		case "${x}" in
			--)
				hadargterm=1
				continue
				;;
			-*)
				# Ignore pmount args
				[ -n "${hadargterm}" ] || continue
				;;
		esac

		DEVPATH=${x}
		break
	done
}

find_devpath "${@}"

if [ -z "${DEVPATH}" ]; then
	pumount "${@}"
	exit ${?}
fi

mp=$(mp_find "${DEVPATH}")

if [ -z "${mp}" ]; then
	# Passed a mountpoint?

	mp=${DEVPATH}
	DEVPATH=$(mp_used "${mp}")

	if [ -z "${DEVPATH}" ]; then
		DEVPATH=${mp}
		conf_read TRACE=

		summary "is not mounted."
		debug "... not mounted."
		exit 1
	fi
fi

conf_read TRACE=

debug "Running uam-pumount on ${DEVPATH}."

# for hooks
MOUNTPOINT=${mp%%/}

hook_exec pre-umount

debug "... found ${DEVPATH} mounted in ${mp}, trying to umount."

pumount "${@}"

RET=${?}
if [ ${RET} -eq 0 ]; then
	debug "...... pumount successful."
	summary "unmounted sucessfully."
else
	debug "...... unable to pumount device."
	summary "pumount failed."
fi
hook_exec post-umount

exit ${RET}
