#!/bin/bash

mp=$1

if [ "${mp::6}" = "/sftp/" ]; then
    # Check if directory exists
    if [ -d "${mp}" ]; then
        # Check if directory is mounted
        if mountpoint -q "${mp}"; then
            # Unmount directory
            if ! umount "${mp}"; then
                exit 1
            fi
        fi
        # Remove directory. Never remove its content, if it is not empty
        # then something is still mounted on it or it holds user data.
        if ! rmdir "${mp}"; then
            exit 1
        fi
    fi
fi
