#!/usr/bin/env python
# -*- coding: utf-8 -*-
import seutils
import argparse
parser = argparse.ArgumentParser()
parser.add_argument(
    'src', type=str, nargs='+',
    help='One or more paths to root files or directies containing root files to be merged'
    )
parser.add_argument(
    '-o', '--out', type=str, default='merged.root',
    help='Path to target root file'
    )
parser.add_argument(
    '-d', '--dry', action='store_true',
    help='Prints formatted hadd command but does not execute'
    )
parser.add_argument(
    '-p', '--parallel', action='store_true',
    help='Perform the hadd using intermittent chunks'
    )
parser.add_argument(
    '-c', '--chunksize', default=200, type=int,
    help='Perform the hadd using intermittent chunks of chunksize'
    )
parser.add_argument(
    '-n', '--nthreads', default=6, type=int,
    help='Perform the hadd using intermittent chunks with nthreads'
    )
parser.add_argument('-m', '--mgm', type=str, help='MGM to be used')
args = parser.parse_args()

def main():
    if args.mgm:
        mgm = args.mgm
    else:
        mgm = seutils.cli_detect_fnal()
    src = [
        seutils.cli_flexible_format(s, mgm) if (s.startswith('root:') or s.startswith('/store')) else s
        for s in args.src
        ]
    if args.parallel:
        seutils.hadd_chunks(src, args.out, n_threads=args.nthreads, chunk_size=args.chunksize, dry=args.dry)
    else:
        seutils.hadd(src, args.out, dry=args.dry)

if __name__ == '__main__':
    main()