#!/usr/bin/env python
"""
    jldcomp - compute filename level difference between two directory paths, outputs result on stdout using JSON

    - primary path
    - compare path
    - status filename (file inside the primary path)

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

try:
    import jlddk
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 jlddk

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

DESC="Compare filename level difference between 2 directory paths"
DEFAULTS={
          "polling_interval": 30
          ,"status_filename": "_status"
          ,"wait_status": False
          }

def main():
    try:
        import jlddk.do_setup
        import logging
        import jlddk.do_checks
        from jlddk.tools_sys import info_dump, dnorm
        
        parser=argparse.ArgumentParser(description=DESC, fromfile_prefix_chars='@')
        parser.add_argument('-pp',  dest='primary_path',  type=str, help="Primary path", required=True)
        parser.add_argument('-zp',  dest='compare_path',  type=str, help="Compare path", required=True)
        parser.add_argument('-sf',  dest='status_filename',  type=str, help="Filename of status file", default=DEFAULTS["status_filename"])
        parser.add_argument('-wsf', dest='wait_status',    action="store_true", help="Wait on status file", default=DEFAULTS["wait_status"])
        
        parser.add_argument('-cp',  dest='check_path',       type=str, help="Check path")
        parser.add_argument('-jbn', dest='just_basename',    action="store_true", help="Just compare basename", default=False)
        
        parser.add_argument('-p',   dest="polling_interval", type=int, action="store", help="Polling interval (seconds)", default=DEFAULTS["polling_interval"])
        
        parser.add_argument('-lc',  dest="logconfig", type=str,  help="Logging configuration file", choices=["debug", "info", "warning", "error"])
        parser.add_argument('-ll',  dest="loglevel",  type=str,  help="Logging Level", choices=["debug", "info", "warning", "error"], default="info")        
                
        args=parser.parse_args()
        
        info_dump(vars(args), 20)
        
        from jlddk.script_comp 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())
