import pathlib


def symlink_to(
    link_destination: pathlib.Path,
    link_path: pathlib.Path,
    *,
    target_is_directory: bool = False,
    overwrite_symlink: bool = False,
) -> bool:
    """Create a symbolic link.
    This function works analogous to os.symlink, but optionally allows
    overwriting symbolic links, and instead of raising exceptions returns
    True on success and False on failure.
    """

    # Bail if the destination is a symbolic link and we do not overwrite links
    if link_path.is_symlink():
        # Python 3.9+: Could use .readlink() here and return True if correct
        if not overwrite_symlink:
            return False
    elif link_path.exists():
        # If it is not a symbolic link AND exists, then also bail.
        return False

    # Symlinks can't be directly overwritten, so create a temporary symlink next
    # to where it should go, and then rename on top of a potentially existing link.
    tmp_link = link_path.parent / f".tmp.{link_path.name}"
    tmp_link.symlink_to(link_destination, target_is_directory=target_is_directory)
    try:
        tmp_link.replace(link_path)
    except PermissionError as e:
        if getattr(e, "winerror", None) == 5:
            # Windows can't rename on top, so delete and retry
            link_path.unlink()
            tmp_link.replace(link_path)
        else:
            raise

    return True