#!/usr/bin/python
# Copyright (C) Duncan Macleod (2016)
#
# This file is part of LIGO-Omicron.
#
# LIGO-Omicron is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# LIGO-Omicron is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with LIGO-Omicron.  If not, see <http://www.gnu.org/licenses/>.

"""Merge multiple Omicron ROOT files into one
"""

import os.path
import argparse

from omicron import (io, __version__)

__author__ = 'Duncan Macleod <duncan.macleod@ligo.org>'

parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument('-V', '--version', action='version', version=__version__)
parser.add_argument('filename', nargs='+', help='file to merge')
parser.add_argument('output', help='output file name')
parser.add_argument('-d', '--remove-input', action='store_true', default=False,
                    help='remove input files after writing output, '
                         'default: %(default)s')
parser.add_argument('-s', '--strict', action='store_true', default=False,
                    help='only merge contiguous data, default: %(default)s')

args = parser.parse_args()
io.merge_root_files(args.filename, args.output, strict=args.strict)

# remove input files
if args.remove_input:
    for f in args.filename:
        if os.path.samefile(f, args.output):
            continue
        os.remove(f)
