#!/bin/sh
#v9.2.0
globalconf="${workdir}/cbsd.conf";
MYARG=""
MYOPTARG="mdfile jroot md"
MYDESC="unmount image file from jroot by jroot path or by mdfile or by devices"
ADDHELP="jroot - find by mount point\n\
mdfile - find by image file\n\
md - find by /dev/mdXX device\n"

set -e
. ${globalconf}
set +e

. ${subr}
. ${mdtools}

init $*

[ -n "${mdfile}" -a ! -f "${mdfile}" ] && err 1 "No such ${mdfile}"
[ -n "${jroot}" -a ! -d "${jroot}" ] && err 1 "No such ${jroot}"
[ -n "${md}" -a ! -c "/dev/${md}" ] && err 1 "No such /dev/${md}"


if [ -n "${jroot}" ]; then
	md=$( eval find_md_by_mountpath ${jroot} )
	[ -z "${md}" ] && err 1 "No md based devices found for ${jroot}"
elif [ -n "${mdfile}" ]; then
	md=$( eval find_md_by_img ${jroot} )
	[ -z "${md}" ] && err 1 "No md based devices found for ${jroot}"
elif [ -n "${md}" ]; then
	jroot=$( eval find_path_by_md ${md} )
	[ -z "${jroot}" ] && err 1 "No md based devices found for ${jroot}"
fi

umount -f ${jroot}
mdconfig -d -u ${md}
