def main():
    """
    Our main entry point. Parse the options, set up logging, go in to
    daemon mode if necessary, setup the asimap library and start
    accepting connections.
    """
    parser = setup_option_parser()
    (options, args) = parser.parse_args()

    # Enter daemon mode early on if it is selected.
    #
    if options.daemonize:
        print "asimap - Entering daemon mode"
        asimap.utils.daemonize()

    # Set up the logger. We log either to files in 'options.logdir' or to
    # stderr. NOTE: It does not make sense to log to stderr if we are running
    # in daemon mode.. maybe we should exit with a warning before we try to
    # enter daemon mode if logdir == 'stderr'
    #
    if options.debug:
        level = logging.DEBUG
    else:
        level = logging.INFO

    log = logging.getLogger("asimap")
    log.setLevel(level)

    if options.logdir == "stderr":
        # Do not log to a file, log to stderr.
        #
        h = logging.StreamHandler()
    else:
        # Rotate on every 10mb, keep 5 files.
        #
        log_file_basename = os.path.join(options.logdir, "asimapd.log")
        h = logging.handlers.RotatingFileHandler(log_file_basename,
                                                 maxBytes = 10485760,
                                                 backupCount = 5)
    h.setLevel(level)
    formatter = logging.Formatter("%(asctime)s %(created)s %(process)d "
                                  "%(levelname)s %(name)s %(message)s")
    h.setFormatter(formatter)
    log.addHandler(h)
    log.info("Starting")

    # XXXX Due to complications with how we detect/load the auth system we have
    #      to import asimap.server _after_ we have defined our log system.
    #
    import asimap.server

    try:
        with open(options.pidfile, "w+") as f:
            f.write("%d\n" % os.getpid())
        log.info("Wrote pid %d in to pid file '%s'" % (os.getpid(),
                                                       options.pidfile))
    except Exception, e:
        log.error("Unable to write PID file '%s': %s" % (options.pidfile,
                                                         str(e)))


    # If we are using SSL you must supply a certificate.
    #
    if options.ssl and options.ssl_certificate is None:
        log.error("If SSL is enabled you need to provide a SSL certificate "
                  "via the --ssl_certificate option")
        exit(-1)

    # Using the location of the server program determine the location of
    # the user_server program (if it was not set via a command line option.)
    #
    user_server_program = os.path.abspath(os.path.join(os.path.dirname(sys.argv[0]),"asimapd_user.py"))

    # Make sure the user server program exists and is executable before we go
    # any further..
    #
    if not os.path.exists(user_server_program) or \
            not os.path.isfile(user_server_program):
        log.error("User server program does not exist or is not a file: '%s'" \
                      % user_server_program)
        exit(-1)

    # Set this as a variable in the asimap.user_server module.
    #
    log.debug("user server program is: '%s'" % user_server_program)
    asimap.user_server.set_user_server_program(user_server_program)

    try:
        if options.port:
            non_ssl_server = asimap.server.IMAPServer(options.interface,
                                                      options.port,
                                                      options)
        if options.ssl:
            ssl_server =  asimap.server.IMAPServer(options.interface,
                                                   options.ssl_port,
                                                   options,
                                                   options.ssl_certificate)
    except socket.error, e:
        log.error("Unable to create server object on %s:%d: socket " \
                  "error: %s" % (options.interface, options.port, e))
        return

    # XXX We should do the loop inside of 'while True' and at the end of each
    #     loop run through all of the subprocess handles and call 'is_alive()'
    #     on them to reap them so that when they go away due to idleness we do
    #     not leave zombie processes waiting around for their parent to reap
    #     them.
    #
    #     We have to do this because subprocesses will stay around after they
    #     have been started up until they have been idle for a certain amount
    #     of time with no active clients.
    #
    asyncore.loop()
    # while True:
    #     try:
    #         asyncore.loop()
    #     except select.error, e:
    #         tb = traceback.format_exc()
    #         log.error("asyncore.loop() returned select.error: %s\n%s" % \
    #                       (str(e), tb))
    #     else:
    #         break

    return

############################################################################
############################################################################
#
# Here is where it all starts
#
dataset/ETHPy150Open scanner/asimap/asimapd.py/main/original