'Converts a record from version 2 to version 3. Version 3
uses new Nmap structured data for scripts using the ls
library.'

@staticmethod
def migrate_schema_hosts_2_3(doc):

    assert (doc['schema_version'] == 2)
    update = {'$set': {'schema_version': 3}}
    updated_ports = False
    updated_scripts = False
    migrate_scripts = set(['afp-ls', 'nfs-ls', 'smb-ls', 'ftp-anon', 'http-ls'])
    for port in doc.get('ports', []):
        for script in port.get('scripts', []):
            if (script['id'] in migrate_scripts):
                if (script['id'] in script):
                    script['ls'] = xmlnmap.change_ls(script.pop(script['id']))
                    updated_ports = True
                elif ('ls' not in script):
                    data = xmlnmap.add_ls_data(script)
                    if (data is not None):
                        script['ls'] = data
                        updated_ports = True
    for script in doc.get('scripts', []):
        if (script['id'] in migrate_scripts):
            data = xmlnmap.add_ls_data(script)
            if (data is not None):
                script['ls'] = data
                updated_scripts = True
    if updated_ports:
        update['$set']['ports'] = doc['ports']
    if updated_scripts:
        update['$set']['scripts'] = doc['scripts']
    return update
