#!/usr/bin/env python3
import argparse
from libnmap.parser import NmapParser
from jackal import Host, Service, Core


def parse_nmap(disable_save, input_file):
    parser = NmapParser()
    print("Jackal importing file: {}".format(input_file))
    report = parser.parse_fromfile(input_file)
    print(report)
    for nmap_host in report.hosts:
        # TODO use core to do this.
        host = Host.get(nmap_host.address, ignore=404)
        if not host:
            host = Host(address=nmap_host.address)
            if not disable_save:
                host.save()
        tags = host.tags.extend(['nmap', nmap_host.status])
        tags = list(set(host.tags))
        services = host.services
        for service in nmap_host.services:
            services.append(service.get_dict())
        if not disable_save:
            host.update(tags=tags, services=services)
    print("Nmap import done")

if __name__ == '__main__':
    core = Core(use_pipe=False)
    if core.arguments.file:
        parse_nmap(core.arguments.disable_save, core.arguments.file)
