#!/usr/bin/python
# Take data that's been appended to a remote replica of a file, and
# append it to the local replica.

import sys, os, stat, time

start = time.time()

mbox = file(sys.argv[1], 'a')
size = os.fstat(mbox.fileno()).st_size
print "currently %d bytes" % size
morebytes = size + 1
child_stdin, child_stdout = os.popen2(['ssh', '-vC',
                                       sys.argv[2],
                                       'tail', '-c',
                                       '+%d' % morebytes,
                                       sys.argv[3]])
bytesread = 0
try:
    while True:
        buf = child_stdout.read(4096)
        if buf == '': break
        mbox.write(buf)
        bytesread += len(buf)
        sys.stdout.write("got %d bytes so far\r" % bytesread)
        sys.stdout.flush()
finally:
    child_stdin.close()
    child_stdout.close()
    mbox.close()
    duration = time.time() - start
    print "\n%d bytes in %.1f seconds, %.2f bytes/sec" % (bytesread,
                                                          duration,
                                                          float(bytesread)/duration)


