#!/usr/bin/env python
"""
    S3 Notify - calls the "run" function of a python module upon detecting changes to an S3 bucket
                Also outputs resulting JSON object to stdout

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

try:
    if os.environ.get("DEVMODE", False):
        raise        
    import jldaws #@UnusedImport
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 #@UnusedImport

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

DESC="Amazon S3 notify when detecting changes to bucket / prefix - %s" % __version__
DEFAULTS={
          "polling_interval": 30
          ,"prefix": ""
          }

def main():
    try:
        import jldaws.do_setup  #@UnusedImport
        import logging
        import jldaws.do_checks #@UnusedImport
        
        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('-mn', dest='module_name', type=str, help="Python module to use OR 'None'", default=None)
        
        parser.add_argument('-e',  dest="propagate_error", help="Propagate S3 access errors", action="store_true", default=False)        
        parser.add_argument('-a',  dest="always", help="Always ouput - even if there are no changes", action="store_true", default=False)
        parser.add_argument('-r',  dest="prefix", help="Prefix", type=str, action="store", default=DEFAULTS["prefix"])
        parser.add_argument('-debug', dest="enable_debug",     action="store_true", help="Enable debug information")
        parser.add_argument('-j',  dest="enable_json",      action="store_true", help="Outputs JSON object to stdout")
        parser.add_argument('-p',  dest="polling_interval", type=int, action="store", help="Polling interval (seconds)", default=DEFAULTS["polling_interval"])        
        args=parser.parse_args()
        
        from jldaws.script_s3notify import run
        run(args)

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

sys.exit(main())