import json


def _find_remote_overlay_res(device_name, links_json_path):
    """Get download link for ``overlay_res.ext`` file and related checksum from
    ``overlay_res.ext.link`` json file, based on ``device_name``.

    The ``.link`` file is generally a dict of device names and associated url
    and md5sum.

    .. code-block:: python3
        {
            "device_1": {
                            "url": "https://link.to/overlay.xclbin",
                            "md5sum": "da1e100gh8e7becb810976e37875de38"
                        }.
            "device_2": {
                            "url": "https://link.to/overlay.xclbin",
                            "md5sum": "da1e100gh8e7becb810976e37875de38"
                        }
        }

    Expected return content from the ``.link`` json file is a dict with two
    entries:

    .. code-block:: python3

        {
            "url": "https://link.to/overlay.xclbin",
            "md5sum": "da1e100gh8e7becb810976e37875de38"
        }

    Returns `None` if ``device_name`` is not found.

    If the ``.link`` file contains a *url* and *md5sum* entries at the top
    level, these are returned and no device-based resolution is performed.

    Parameters
    ----------
        device_name: str
            The target device name
        links_json_path: str
            The full path to the ``.link`` json file
    """
    with open(links_json_path) as f:
        links = json.load(f)
    if "url" in links and "md5sum" in links:
        return {"url": links["url"], "md5sum": links["md5sum"]}
    if device_name in links:
        return links[device_name]
    return None