#!/usr/bin/python3

# This program is free software. It comes without any warranty, to
# the extent permitted by applicable law. You can redistribute it
# and/or modify it under the terms of the Do What The Fuck You Want
# To Public License, Version 2, as published by Sam Hocevar. See
# the COPYING file included with this distribution or
# http://sam.zoy.org/wtfpl/COPYING for more details.

import sys
from bps.diff import diff_bytearrays
from bps.io import write_bps
from bps.util import bps_progress

with open(sys.argv[1], 'rb') as source:
	sourcedata = source.read()

with open(sys.argv[2], 'rb') as target:
	targetdata = target.read()

# Smaller block-sizes make for more efficient diffs, but a larger number of
# blocks uses more memory. Since we need to keep the block map for the
# source and target in memory at the same time, calculate a block-size that
# will give us about a 64-byte block on a 32MB file (since I know from
# experience that fits in my RAM).
blocksize = (len(sourcedata) + len(targetdata)) // 1000000 + 1

print("Using blocks of {0} bytes".format(blocksize), file=sys.stderr)

iterable = diff_bytearrays(blocksize, sourcedata, targetdata)

write_bps(bps_progress(iterable), sys.stdout.buffer)
