# -*- mode: python -*-
# -*- coding: utf-8 -*-

__author__ = "Silvano Cirujano Cuesta"
__email__ = "silvanociru@gmx.net"
__copyright__ = "Copyright (C) Silvano Cirujano Cuesta 2015"
__license__ = "MPL-2.0"

# Python standard packages
import os
import os.path
import logging
import argparse
import socket
import time
# findups packages
import findups.scanner


def main():
    hostname = socket.gethostname()
    parser = argparse.ArgumentParser()
    parser.add_argument('--db_path', action="append",
                        default=[os.path.join(os.getenv("HOME"), hostname + ".db")],
                        help="Path of the DB")
    parser.add_argument('--debug', action="store_true",
                        help="Enable debugging")
    parser.add_argument('--accuracy', action="store", default=findups.scanner.SAME_SIZE,
                        help="Specify accuracy level")
    parser.add_argument('--device_id', action="store", default=hostname,
                        help="Path of the directory to scan")
    parser.add_argument('action', choices=['scan', 'merge', 'compare'],
                        help="Specify accuracy level")
    parser.add_argument('directories', action="store", nargs='*',
                        help="Paths of the directories to scan")
    ns = parser.parse_args()
    if ns.debug:
        log_level = logging.DEBUG
    else:
        log_level = logging.WARNING
    accuracy = int(ns.accuracy)
    print("*****************************")
    if ns.action == 'scan':
        while ns.directories:
            start = time.time()
            scanner = findups.scanner.DirScanner(device_id=ns.device_id, db_path=ns.db_path[0], log_level=log_level,
                                                 accuracy=accuracy)
            directory = ns.directories.pop()
            n_files, total_size = scanner.scan(directory)
            if not n_files or not total_size:
                continue
            end = time.time()
            duration = end - start
            print('%d files adding up to %d KBs have been scanned in %d seconds' %
                  (n_files, int(total_size/1024), duration))
            print('%d files/second' % (int(n_files/duration)))

if __name__ == '__main__':
    main()
