from pathlib import Path
import shutil


def check_overwrite_folder(folder, name, overwrite, make_folder):
    """
    This method checks to see if the file at the path specified should be overwritten.

    Args:
        folder (str): The directory where the file might be.
        name (str): The name of the folder
        overwrite (bool): Should the file be overwritten
        make_folder (bool): Should the directory be made if it passes the overwrite test.

    Returns:
        True if the folder can be overwritten and was created (if ``make_folder`` was True) and
        False otherwise.
    """

    folder = Path(folder)
    file_path = folder / name

    if file_path.exists() and not overwrite:
        return False

    if make_folder:
        parts = folder.parts
        parts_path = Path('')

        for part in parts:
            parts_path = parts_path / part

            if not parts_path.exists():
                parts_path.mkdir()

        if file_path.exists():
            shutil.rmtree(file_path)
        
        file_path.mkdir()

    return True