import pathlib
import zipfile


def source_input_directory(user_filepath):
    """Examine program arguments to determine the location of an archive.
    Extract as needed.
    """
    if user_filepath is None:
        return "export"

    path = pathlib.Path(user_filepath)
    if not path.exists():
        raise RuntimeError("Specified path {} does not exist".format(user_filepath))

    if path.is_dir():
        return user_filepath

    if path.is_file():
        if user_filepath.endswith(".zip"):
            extracted_filepath = user_filepath.replace(".zip", "")
            with zipfile.ZipFile(user_filepath, 'r') as zip_file:
                zip_file.extractall(extracted_filepath)
                return extracted_filepath
        else:
            raise RuntimeError("Specified path {} is a file, but not an archive".format(user_filepath))