#!/usr/bin/env python3

import os
import os.path
import sys
import fileinput


def op_backup():
    if len(sys.argv) < 2:
        sys.stdin = os.fdopen(sys.stdin.fileno(), 'rb', 0)
        count = 0
        for f in sys.stdin:
            #f = str(f).rstrip("\n")
            if f[-1] == ord("\n"):
                f = f[0:-1]
            print(count)
            count += 1
            fo.backup(f)
            if count % 1000 == 0:
                dm.commit()
    else:
        for f in sys.argv[1:]:
            fo.backup(f)
    dm.commit()


def op_restore():
    desdir = sys.argv[1]
    if len(sys.argv) < 3:
        for f in sys.stdin:
            f = f.rstrip("\n")
            fo.restore(f, desdir+'/'+f)
    else:
        for f in sys.argv[2:]:
            fo.restore(f, desdir+'/'+f)
    pass


def op_list():
    pass


def usage():
    print("usage:", sys.argv[0], '|'.join(ops))
    pass

ops = {
    "backup" : op_backup ,
    "restore": op_restore,
    "list"   : op_list   ,
}

if __name__ == "__main__":
    import fileops
    import datameta_tree
    import datameta_sqlite
    import datastore_tree
    import datastore_gdbm
    import datastore_s3
    import compress_none
    import compress_gzip
    import compress_lzma
    import compress_bz2
    import crypt_none
    import crypt_aes
    import hash_sha1

    basepath = "/tmp/lfbackup-meta"
    key      = b"12345678"

    hashfact = hash_sha1.Hash_SHA1
    #dm       = datameta_tree.DataMeta_Tree(basepath)
    dm       = datameta_sqlite.DataMeta_Sqlite(basepath+"/db")
    #ds       = datastore_tree.DataStore_Tree(basepath)
    #ds       = datastore_gdbm.DataStore_Gdbm(basepath+"/gdbm", hashfact)
    ds       = datastore_s3.DataStore_S3("test.lfbackup/", hashfact)
    compress = compress_none.Compress_None()
#    compress = compress_gzip.Compress_Gzip()
#    compress = compress_lzma.Compress_Lzma()
#    compress = compress_bz2.Compress_Bz2()
    crypt    = crypt_none.Crypt_None(key)
#    crypt    = crypt_aes.Crypt_AES(key)
    fo = fileops.FileOps(dm, ds, compress, crypt, hashfact)

    if len(sys.argv) < 2 or sys.argv[1] not in ops:
        usage()
        exit(-1)

    op = sys.argv[1]
    sys.argv[0:2] = [" ".join((sys.argv[0], sys.argv[1]))]
    ops[op]()

    exit(0)
