#!/usr/bin/env python3
"""Prometheus Bugzilla Exporter"""

import sys
from prombzex import prombzex


def usage():
    """Print program usage"""
    print("Usage: %s CONFIGFILE", sys.argv[0])


def main():
    """Main entrypoint"""
    if len(sys.argv) != 2:
        usage()
        sys.exit(1)

    output = ""
    with open(sys.argv[1], 'r') as config_file:
        bzex = prombzex.PromBZEx(config_file)
    for server in bzex.servers():
        server_output = ""
        bzserver = bzex.server(server)

        for query in bzserver.query_names():
            print("Running: %s::%s..." % (server, query))
            server_output += bzserver.run_query(query)

        if not bzex.config_get('output_file', server, False):
            # Server has no output_file defined, so lump it in the global text
            output += server_output
        else:
            # Write server-specific output
            bzex.write(server_output, server_name=server)

    # Write global output if there is any
    if output != "":
        bzex.write(output)


if __name__ == '__main__':
    main()
