#!/usr/local/bin/cbsd
#v9.2.0
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"

. ${subrdir}/nc.subr
. ${cbsdinit}
. ${mdtools}

[ -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_CMD} -f ${jroot}
${MDCONFIG_CMD} -d -u ${md}

exit 0
