def main(argv=None):
    """Count the hits from logfile."""
    parser = create_parser('hits_counter', description=__doc__)
    parser.add_argument('--hostname', default='cnx.org',
                        help="hostname of the site (default: cnx.org)")
    parser.add_argument('--log-format',
                        default=LOG_FORMAT_GZ, choices=LOG_FORMATS,
                        help="(default: {})".format(LOG_FORMAT_GZ))
    parser.add_argument('log_file',
                        help="path to the logfile.")
    args = parser.parse_args(argv)

    opener = LOG_FORMAT_OPENERS_MAPPING[args.log_format]

    # Build the URL pattern.
    hostname = args.hostname.replace('.', '\.')
    url_pattern = URL_PATTERN_TMPLT.format(hostname)
    url_pattern = re.compile(url_pattern)

    # Parse the log to structured data.
    with opener(args.log_file) as log:
        hits, start_timestamp, end_timestamp = parse_log(log, url_pattern)

    # Parse the configuration file for the postgres connection string.
    settings = get_app_settings_from_arguments(args)

    # Insert the hits into the database.
    connection_string = settings[config.CONNECTION_STRING]
    db_connection = psycopg2.connect(connection_string)
    with db_connection:
        with db_connection.cursor() as cursor:
            for ident_hash, hit_count in hits.items():
                cursor.execute("""\
                      INSERT INTO document_hits
                        (documentid, start_timestamp, end_timestamp, hits)
                      SELECT module_ident, %s, %s, %s
                      FROM modules WHERE
                        ident_hash(uuid, major_version, minor_version) = %s""",
                               (start_timestamp, end_timestamp, hit_count,
                                ident_hash))
            cursor.execute("SELECT update_hit_ranks();")
    db_connection.close()
    return 0