from pathlib import Path
from typing import Dict


def license_destination(
    destination: Path, libname: str, filename: str, license_directories: Dict[str, str]
) -> Path:
    """Given the (reconstructed) library name, find appropriate destination"""
    normal = destination / libname
    if normal.is_dir():
        return normal / filename
    lowercase = destination / libname.lower()
    if lowercase.is_dir():
        return lowercase / filename
    if libname in license_directories:
        return destination / license_directories[libname] / filename
    # fallback to libname.LICENSE (used for nondirs)
    return destination / "{}.{}".format(libname, filename)