#!/bin/bash
set -e

while read dev
do
	echo
	df -h /mnt/"$dev"
	printf '\nunmount %s [y/n] ' "$dev"
	while true
	do
		read -n1 -u1 -r
		printf '%s\n' "$REPLY" | grep -qE '^[yYnN]$' || continue
		printf '%s\n' "$REPLY" | grep -qE '^[yY]$' &&
			act=y || act=''
		break
	done
	echo
	
	[ $act ] ||
		continue
	
	while true
	do
		umount /mnt/"$dev" &&
			break || sleep 1
	done
	rmdir /mnt/"$dev"

	while true
	do
		cryptsetup close "$dev" &&
			break || sleep 10
	done

done < <(
	find /dev/mapper -iname \*_ov |
	sed -r 's@.*/@@'
)
echo ok
