import toml


def check_fpm_toml(contents):
    """Checks contents of fpm.toml, returns cut-down version for json index"""

    p = toml.loads(contents)

    fpm_info = {}
    
    # Must be present, copied to json
    required_keys = ["name", "version", "license", "author", 
                       "maintainer", "copyright"]

    # Optionally present, copied to json
    optional_keys = ["description", "executable", "dependencies",
                     "dev-dependencies"]

    # Optionally present, not copied to json
    other_keys = ["test", "library", "build", "extra"]

    # Check for required keys
    for key in required_keys:
        if key not in p:
            raise Exception(f"Missing required key '{key}' in fpm.toml.")

    for key in p.keys():
        if key not in required_keys + optional_keys + other_keys:
            print(f"        (!) Warning: unexpected key '{key}; in fpm.toml")

    # Copy keys to dict for json index
    for key in required_keys:
        fpm_info[key] = p[key]

    for key in optional_keys:
        if key in p:
            fpm_info[key] = p[key]
        else:
            fpm_info[key] = None
    
    return fpm_info