#!/usr/bin/env python
"""
    S3 Download - automatically download files from S3

    @author: Jean-Lou Dupont
"""
__version__="0.1"
import os, sys, argparse
op=os.path

try:
    import jldaws
except:
    ### must be in dev mode then    
    this_dir=op.dirname(__file__)
    lib_path=op.abspath(op.join(this_dir, ".."))
    sys.path.insert(0, lib_path)
    import jldaws

########################################################################

DESC="Download files from S3 - %s" % __version__ 
DEFAULTS={
          "polling_interval": 30
          ,"prefix": ""
          ,"num_files": 5
          }

def main():
    try:
        import jldaws.do_setup
        import logging
        import jldaws.do_checks
        
        from jldaws.tools_sys import dnorm
        from jldaws.tools_logging import info_dump
        
        parser=argparse.ArgumentParser(description=DESC, fromfile_prefix_chars='@', formatter_class=argparse.RawTextHelpFormatter)
        parser.add_argument('-bn', dest='bucket_name', type=str, help="Bucket name",      required=True)
        parser.add_argument('-r',  dest="prefix", help="Prefix", type=str, action="store", default=DEFAULTS["prefix"])
        
        parser.add_argument('-dp', dest='path_dest',   type=str, help="Destination path", required=True)
        parser.add_argument('-cp', dest='path_check',  type=str, help="Check path", default=None)

        parser.add_argument('-bs',  dest='num_files',   type=int, help="Maximum number of files to process per interval", default=DEFAULTS["num_files"])
        parser.add_argument('-e',  dest="propagate_error", help="Propagate S3 access errors", action="store_true", default=False)        
        parser.add_argument('-jk', dest="just_keys",       help="Just download keys", action="store_true", default=False)        
        parser.add_argument('-dd', dest="dont_dl",         help="Don't download if local file exits", action="store_true", default=False)

        parser.add_argument('-p',  dest="polling_interval", type=int, action="store", help="Polling interval (seconds)", default=DEFAULTS["polling_interval"])        
        args=parser.parse_args()
        
        info_dump(vars(args), 20)
        
        from jldaws.script_s3download import run
        run(**dnorm(vars(args)))

    except KeyboardInterrupt:
        logging.info("..Exiting")
        sys.exit(0)##no probs
        
    except Exception,e:
        logging.error(str(e))
        sys.exit(1)
        
sys.exit(main())
