#!/bin/bash
#
# (c) 2019-2021 m-privacy GmbH
#
# Last change: ao, 05/Mar/2021
#

RED="\033[31;01m"
BLUE="\033[34;01m"
GREEN="\033[32;01m"
MAGENTA="\033[35;01m"
RESET="\033[00m"

MOUNTPOINT="/tmp/tgpromnt"

if test -z "$1"
then
	echo "Use: $0 number [mountpoint]"
	echo "Umount tgpro\$number image from mountpoint"
	echo "Default mountpoint is $MOUNTPOINT"
	echo "e.g.: $0 1"
	exit
fi

TGNUM="$1"
if test -n "$2"
then
        MOUNTPOINT="$2"
fi

case "$TGNUM" in
	[123456789])
		;;
	[123456789][0123456789])
		;;
	[123456789][0123456789][0123456789])
		;;
	*)
		echo -e "${RED}Number \"$TGNUM\" is not a number${RESET}"
		exit 1
esac

if ! rbd ls | grep "^tgpro$TGNUM$"
then
	echo -e "${RED}Image tgpro$TGNUM does not exist${RESET}"
	exit 2
fi

echo -e "${BLUE}Umount tgpro$TGNUM image now${RESET}"
echo "Press return to continue or CTLR-C to abort"
read dummy

umount "$MOUNTPOINT/home"
umount "$MOUNTPOINT/var/log"
umount "$MOUNTPOINT/var"
umount "$MOUNTPOINT"

rbd unmap "tgpro$TGNUM"

echo -e "${BLUE}Ready.${RESET}"

exit 0
