#!/usr/bin/env python

import sys
from Detector.dir_root import DIR_REPO  #, DIR_LOG_AT_START
from Detector.UtilsLogging import STR_LEVEL_NAMES, logging  # , init_logger
logger = logging.getLogger(__name__)

SCRNAME = sys.argv[0].split('/')[-1]

DESCRIPTION = 'Deployment of calibration constants'

USAGE = '\n%s -e <experiment> -r <run-number> -d <detector> -C <calib-type> [-D] [-L <logging-mode>] [...]' % SCRNAME\
      + '\n%s -x exp=<experiment>:run=<run-number>:dir=<path-to-xtc>:stream=0-79 -d <detector> -C <calib-type>' % SCRNAME\
      + ' -c <path-to-calib> -R <run-range> -D'\
      + '\n\nREGULAR COMMAND EXAMPLE:'\
      + '\n  %s -e xpptut15 -r 260 -d XcsEndstation.0:Epix100a.1 -C gain -c ./calib --runrange 10-20 -D' % SCRNAME\
      + '\n  %s -x exp=xpptut15:run=260:dir=/cds/data/psdm/xpp/xpptut15/xtc -d XcsEndstation.0:Epix100a.1 -C gain -c ./calib -D' % SCRNAME\
      + '\n\n  Input parameters to select dataset:                  # DataSource argument:'\
      + '\n    -e xpptut15 -r 260                                 # exp=xpptut15:run=260'\
      + '\n    -e xpptut15 -r 260                                 # exp=xpptut15:run=260:smd'\
      + '\n    -e xpptut15 -r 260 -x :dir=<path>/xtc:stream=0-79  # exp=xpptut15:run=260:dir=<path>/xtc:stream=0-79'\
      + '\ne.g. -x :dir=/cds/data/drpsrcf/mfx/mfxlv4920/xtc/      # or /cds/data/drpsrcf/mfx/mfxlv4920/xtc/<file-name.xtc>'\
      + '\n\nTEST COMMANDS:'\
      + '\nEx.1  for epix100a     : %s -e xpptut15 -r 260 -d XcsEndstation.0:Epix100a.1 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.2  for Cspad CXI    : %s -e xpptut15 -r 380 -d CxiDs1.0:Cspad.0 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.3  for Cspad CXI    : %s -e xpptut15 -r 380 -d CxiDs2.0:Cspad.0 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.4  for Cspad XPP    : %s -e xpptut15 -r 320 -d XppGon.0:Cspad.0 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.5  for cspad2x2     : %s -e xpptut15 -r 460 -d MecTargetChamber.0:Cspad2x2.3 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.6  for pnCCD        : %s -e xpptut15 -r 450 -d Camp.0:pnCCD.1 -C gain -D -c ./calib' % SCRNAME\
      + '\nEx.7  test for epix100a: %s -x exp=xpptut15:run=260 -d XcsEndstation.0:Epix100a.1 -C status_extra -D -c ./calib' % SCRNAME\
      + '\nEx.8  test for epix100a: %s -x exp=xpplw3319:run=293 -d XppGon.0:Epix100a.3 -C status_extra -D -c ./calib' % SCRNAME\
      + '\n\nHelp:  %s -h\n' % SCRNAME


if len(sys.argv)<2:
    print('%s\n%s\n%s' % (40*'_', DESCRIPTION, USAGE))
    sys.exit('MISSING COMMAND LINE PARAMETERS')


def argument_parser():
    import argparse

    d_dirrepo  = DIR_REPO
    d_exp      = None # 'xpptut15'
    d_det      = None # 'XcsEndstation.0:Epix100a.1'
    d_run      = None # 260
    d_runrange = '0-end'
    d_tstamp   = None # 20180910111049
    d_dsnamex  = None # ':smd:dir=<path>/xtc/ or <path>/xtc/<file-name>.xtc or exp=mfxlx5520:run=100:smd:dir=/cds/data/...:stream=0-79'
    d_dircalib = None # './calib'
    d_ctype    = 'gain'
    d_deploy   = False
    d_loglev   = 'INFO'
    d_dirmode  = 0o2775
    d_filemode = 0o664
    d_group    = 'ps-users'

    h_exp     = 'experiment name, default = %s' % d_exp
    h_det     = 'detector name, default = %s' % d_det
    h_run     = 'run number of the existing dataset to get run time-stamp, detector full name etc., default = %s' % str(d_run)
    h_dsnamex = 'dataset string or xtc file name or extension after exp=<exp>:run=<run> beginning with colon ":", default = %s' % d_dsnamex
    h_runrange= 'validity run range for output calibration file, default = %s' % str(d_runrange)
    h_tstamp  = 'non-default time stamp (YYYYmmddHHMMSS) to search for constants in repo. '\
                'By default run time is used, default = %s' % str(d_tstamp)
    h_dirrepo = 'non-default repository of calibration results, default = %s' % d_dirrepo
    h_dircalib= 'non-default deployment calib directory, different from standard one, default = %s' % d_dircalib
    h_ctype   = 'calibration type [gain, common_mode, etc.]. For status_extra constants of all status_* types are merged, default = %s' % str(d_ctype)
    h_deploy  = 'deploy constants to the calib dir, default = %s' % d_deploy
    h_loglev  = 'logging mode, one of %s, default = %s' % (STR_LEVEL_NAMES, d_loglev)
    h_dirmode = 'mode for all mkdir, default = %s' % oct(d_dirmode)
    h_filemode= 'mode for all saved files, default = %s' % oct(d_filemode)
    h_group   = 'group ownership for all files, default = %s' % d_group

    parser = argparse.ArgumentParser(usage=USAGE, description=DESCRIPTION)

    parser.add_argument('-e', '--exp',     default=d_exp,     type=str, help=h_exp)
    parser.add_argument('-d', '--det',     default=d_det,     type=str, help=h_det)
    parser.add_argument('-r', '--run',     default=d_run,     type=str, help=h_run)
    parser.add_argument('-x', '--dsnamex', default=d_dsnamex,  type=str, help=h_dsnamex)
    parser.add_argument('-t', '--tstamp',  default=d_tstamp,  type=int, help=h_tstamp)
    parser.add_argument('-o', '--dirrepo', default=d_dirrepo, type=str, help=h_dirrepo)
    parser.add_argument('-c', '--dircalib',default=d_dircalib,type=str, help=h_dircalib)
    parser.add_argument('-C', '--ctype',   default=d_ctype,   type=str, help=h_ctype)
    parser.add_argument('-l', '--loglev',  default=d_loglev,  type=str, help=h_loglev)
    parser.add_argument('--dirmode',       default=d_dirmode, type=int, help=h_dirmode)
    parser.add_argument('--filemode',      default=d_filemode,type=int, help=h_filemode)
    parser.add_argument('--group',         default=d_group,   type=str, help=h_group)
    parser.add_argument('-D', '--deploy',  default=d_deploy, action='store_true', help=h_deploy)
    parser.add_argument('-R', '--runrange',default=d_runrange,type=str, help=h_runrange)

    return parser


if __name__ == "__main__":

    parser = argument_parser()
    args = parser.parse_args()
    kwa = vars(args)

    from Detector.RepoManager import init_repoman_and_logger
    repoman = kwa['repoman'] = init_repoman_and_logger(args, parser)

    import Detector.UtilsDeployConstants as udc
    udc.deploy_constants(**kwa)
    repoman.logfile_save()

    sys.exit(0)

# EOF
