#!/usr/bin/env python3
"""Read an evaluation sample from the configured VERA-Q Linux daemon.

The supplied daemon must already expose /dev/iqrng. Reading can wait for output;
use Ctrl+C to interrupt. This example does not assess entropy or generate keys.
"""
import argparse
from pathlib import Path


def read_sample(source: Path, count: int) -> bytes:
    data = bytearray()
    with source.open("rb", buffering=0) as stream:
        while len(data) < count:
            chunk = stream.read(count - len(data))
            if not chunk:
                raise EOFError("Output ended before the sample was complete.")
            data.extend(chunk)
    return bytes(data)


if __name__ == "__main__":
    parser = argparse.ArgumentParser(description=__doc__)
    parser.add_argument("--source", type=Path, default=Path("/dev/iqrng"))
    parser.add_argument("--bytes", type=int, default=32, dest="count")
    args = parser.parse_args()
    if not 1 <= args.count <= 4096:
        parser.error("--bytes must be between 1 and 4096")
    try:
        print(read_sample(args.source, args.count).hex())
    except (OSError, EOFError) as error:
        parser.exit(1, f"Cannot read the evaluation sample: {error}\n")
