import configparser
import io


def modify_setup_cfg(content: str) -> str:
    """Modify setup.cfg."""
    cfg = configparser.ConfigParser()
    cfg.read_string(content)
    # change name of package
    cfg.set("metadata", "name", "myst-docutils")
    # move dependency on docutils and sphinx to extra
    install_requires = []
    sphinx_extra = [""]
    for line in cfg.get("options", "install_requires").splitlines():
        if line.startswith("docutils"):
            sphinx_extra.append(line)
        elif line.startswith("sphinx"):
            sphinx_extra.append(line)
        else:
            install_requires.append(line)
    cfg.set("options", "install_requires", "\n".join(install_requires))
    cfg.set("options.extras_require", "sphinx", "\n".join(sphinx_extra))

    stream = io.StringIO()
    cfg.write(stream)
    return stream.getvalue()