import pathlib
import click
import shutil


def ensure_tmp_dir(path):
    """
    Acquire temporal directory, asking user for overwrite existing if necessary.

    :param path: directory to acquire
    :return: empty temporal directory
    """
    p = pathlib.Path(path)
    if p.exists():
        if click.confirm(f"Tmp directory '{p.absolute()}' already exists.\nOverwrite it's content?"):
            shutil.rmtree(p)
        else:
            raise click.ClickException(
                f"Tmp directory '{p.absolute()}' already exists. Delete it or specify different directory.")
    p.mkdir()
    return p