#!/usr/bin/python
import sys
import os


from auto_align import image_paths

def read_int(path):
    return int(file(path).read())

def read_xy(imgpath):
    """read the .x and .y, raise if anything fails"""
    return map(read_int, ("%s.%s" % (imgpath, axis) for axis in "xy"))

def check_deltas(dir1, dir2):
    """compare the .x and .y files and report changes"""
    
    xdrift, ydrift = (0, 0)
    last_x1, last_y1 = (0, 0)
    for path1 in image_paths(dir1):
        name = os.path.basename(path1)
        try:
            x1, y1 = read_xy(path1)
        except:
            # don't bother if it's not in dir1
            # (could just break here, they are contiguous)
            continue
        path2 = os.path.join(dir2, name)
        try:
            x2, y2 = read_xy(path2)
        except:
            continue

        if x1 == x2 - xdrift and y1 == y2 - ydrift:
            last_x1 = x1
            last_y1 = y1
            continue

        print name, (x1, y1), (x2, y2)
        print "  d1:", (x1 - last_x1, y1 - last_y1)
        print "  d2:", (x2 - last_x1 - xdrift, y2 - last_y1 - ydrift)
        xdrift = x2 - x1
        ydrift = y2 - y1
        print " new drift:", [xdrift, ydrift]
        last_x1 = x1
        last_y1 = y1


if __name__ == "__main__":
    check_deltas(*sys.argv[1:])

