#!/bin/bash

help()
{
    echo "Usage: $(basename $0) <elektra-path>>"
    echo "elektra-path:         the path inside Eleketre to be unmounted"
    echo "The file is unmounted if and only if it has not been unmounted yet"
    echo "If the file is unmounted successfully or already unmounted the script "
    echo "will terminate successfully. If an error occurs, the script will "
    echo "will terminate with a nonzero exit status"
}

if [ "$#" -ne "1" ]; then
    echo "exactly one argument must be given"
    help
    exit 1  
fi

grepcmd=$(command -v grep) || { >&2 echo "No grep command found"; exit 1; }
awkcmd=$(command -v awk) || { >&2 echo "No awk command found"; exit 1; }
kdbcmd=$(command -v kdb) || { >&2 echo "No kdb command found"; exit 1; }

if ($kdbcmd mount | $grepcmd $1) > /dev/null ; then
   # unmount the file
   $kdbcmd umount $1
   exit $?
else
   # the file is already unmounted
   exit 0
fi
