#!/usr/bin/env python3

"""Read data from filenames on stdin in 2 ways, comparing them as we go."""

import os
import sys

sys.path.insert(0, os.path.expanduser('~/lib'))
sys.path.insert(0, os.path.expanduser('.'))

import odirect  # noqa: ignore=E402
import readline0  # noqa: ignore=E402

for filename in readline0.readline0():
    sys.stdout.write("comparing filename %s's data via odirect and via normal python file I/O ...\n" % filename)
    odirect_file = odirect.odirect(filename, b'rbd', 1 << 19)
    regular_file = open(filename, 'rb')
    while True:
        # Note: assumption here is that both file I/O methods are going to return all that they can - EG, if we ask for
        # 512K, we're going to get 512K until we hit EOF.  If the two methods return short blocks inconsistently, this
        # test may appear to have failed, when it wouldn't if we were being careful about potential blocking
        # differences
        odirect_block = odirect_file.pull()
        regular_block = regular_file.read(1 << 19)
        if odirect_block != regular_block:
            sys.stderr.write('Error on file %s\n' % filename)
        if not odirect_block:
            # sys.stdout.write('Empty block')
            # sys.stdout.flush()
            break
        # hex_dump(block)
        # sys.stdout.write('%d ' % len(odirect_block))
        # sys.stdout.flush()
    del odirect_file
    regular_file.close()