#!/usr/bin/env python3
# Copy unseen files

import sys
if sys.version_info < (3, 3):
    sys.stderr.write("Sorry, requires at least Python 3.3\n")
    sys.exit(1)

import argparse
import collections
import os
import pickle
import shlex
import shutil


def read_seen():
    global seen
    try:
        with open(os.path.expanduser('~/.local/share/cp-unseen/seen.pickle'), 'rb') as f:
            seen = pickle.load(f)
    except FileNotFoundError:
        seen = set()


def save_seen():
    global seen
    if not os.path.exists(os.path.expanduser('~/.local/share/cp-unseen')):
        os.mkdir(os.path.expanduser('~/.local/share/cp-unseen'))
    with open(os.path.expanduser('~/.local/share/cp-unseen/seen.pickle'), 'wb') as f:
        pickle.dump(seen, f)


def main():
    global seen, drive

    parser = argparse.ArgumentParser(
        description='Copy unseen files')
    parser.add_argument('folder', type=str, help='folder to copy from')
    # XXX folder to copy *to*!
    args = parser.parse_args()

    read_seen()

    file_list = [os.path.join(dp, f) \
            for dp, _, filenames in os.walk(args.folder) \
            for f in filenames]

    for file1 in file_list:
        if file1 in seen:
            continue

        local_filename = os.path.basename(file1)
        if not os.path.exists(local_filename):
            print(file1)
            shutil.copy(file1, local_filename)
            seen.add(file1)
            save_seen()
        else:
            print("{} exists locally!".format(shlex.quote(local_filename)))
            sys.exit(2)


main()

# vim:tw=100: