#!/usr/bin/env python
from __future__ import print_function
import argparse
import BoxUtils

default_max_attempts = 10

def get_args():
    parser = argparse.ArgumentParser(formatter_class=argparse.RawTextHelpFormatter,
                                     description="Mirror a local directory to a remote directory on Box",
                                     epilog='Note: there is a difference in behavior between this program and LFTP\'s\n'
                                            'mirror -R command when it comes to the specification of the remote \n'
                                            'directory. For this program, the remote directory is ALWAYS assumed\n'
                                            'to be the one that should look like the local directory when the\n'
                                            'operation is done. That is, this program doesn\'t care if the remote\n'
                                            'directory ends in a /, the subdirectories of the local dir will be placed\n'
                                            'immediately in the remote directory (whereas LFTP will place the local\n'
                                            'directory itself in the remote directory if the remote directory path\n'
                                            'given ends in a /)')
    parser.add_argument('localdir', help='The local directory to mirror')
    parser.add_argument('remotedir', help='The remote directory to mirror to; it will take on the EXACT structure of the local directory')
    parser.add_argument('--max-files', '-n', help='The maximum number of files to try to copy (default unlimited)')
    parser.add_argument('--max-attempts', '-m', default=10, help='The maximum number of times to attempt to copy the files (default 10)')
    parser.add_argument('--only-missing', '-o', action='store_true', help='Flag that means this will only copy files that do not exist on the remote, rather than copying if the local file is newer')
    parser.add_argument('--no-retry-mkdir', '-e', action='store_false', help='If the mkdir command on the remote fails with an unexpected error, print the error and exit, rather than retrying later')
    parser.add_argument('--verbose', '-v', action='count', help='Increase verbosity; will print more real time information about the behavior of LFTP')

    args = parser.parse_args()
    if args.max_files is not None:
        max_files = int(args.max_files)
    else:
        max_files = args.max_files
    return args.localdir, args.remotedir, max_files, int(args.max_attempts), args.only_missing, args.no_retry_mkdir, args.verbose

def main():
    localdir, remotedir, max_files, max_attempts, only_missing, retry_mkdir, verbosity = get_args()

    result = BoxUtils.mirror_local_to_remote(localdir, remotedir, max_num_files=max_files, number_attempts=max_attempts,
                                             include_different=not only_missing, retry_mkdir=retry_mkdir, verbosity=verbosity)
    if result:
        exit(0)
    else:
        exit(1)

if __name__ == '__main__':
    main()
