#!/usr/bin/env python
#
# Copyright (C) 2005--2009,2012,2014,2015,2017  Kipp Cannon
#
# This program 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.
#
# This program 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 this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.


#
# =============================================================================
#
#                                   Preamble
#
# =============================================================================
#


"""
Add (merge) LIGO LW XML files containing LSC tables.
"""


from optparse import OptionParser


from lal.utils.cache import CacheEntry
from ligo.lw import __version__, __date__
from ligo.lw import ligolw
from ligo.lw import array as ligolw_array
from ligo.lw import lsctables
from ligo.lw import utils as ligolw_utils
from ligo.lw.utils import ligolw_add


__author__ = "Kipp Cannon <kipp.cannon@ligo.org>"


#
# =============================================================================
#
#                                 Command Line
#
# =============================================================================
#


def parse_command_line():
	"""
	Parse the command line, return an options object and a list of URLs.
	"""
	parser = OptionParser(
		version = "Name: %%prog\n%s" % __version__,
		usage = "%prog [options] [url ...]",
		description = "Combines one or more LIGO Light Weight XML files into a single output file.  The output is written to stdout or to the filename specified by --output.  In addition to regular files, many common URL types can be read such as http:// and ftp://.  Input documents that are gzip-compressed are automatically detected and decompressed.  If the output file's name ends in \".gz\", the output document will be gzip-compressed.  Table elements contained in the document will be merged so that there is not more than one table of any given name in the output.  To accomplish this, any tables in the input documents that share the same name must have compatible columns, meaning the same column names with matching types (but not necessarily in the same order)."
	)
	parser.add_option("--ilwdchar-compat", action = "store_true", help = "Use obsolete ilwd:char based table definitions and ID reassignment algorithm (default = use new int_8s based table definitions and ID reassignment algorithm).")
	parser.add_option("-i", "--input-cache", metavar = "filename", action = "append", default = [], help = "Get input files from the LAL cache named filename.")
	parser.add_option("--non-lsc-tables-ok", action = "store_true", help = "OK to merge documents containing non-LSC tables.")
	parser.add_option("-o", "--output", metavar = "filename", help = "Write output to filename (default = stdout).")
	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose.")
	parser.add_option("--remove-input", action = "store_true", help = "Remove input files after writing output (an attempt is made to not delete the output file in the event that it overwrote one of the input files).")
	parser.add_option("--remove-input-except", metavar = "filename", action = "append", default = [], help = "When deleting input files, do not delete this file.")
	options, urls = parser.parse_args()

	urls += [CacheEntry(line).url for cache in options.input_cache for line in open(cache)]

	if len(urls) < 1:
		raise ValueError("no input files!")

	return options, urls


#
# =============================================================================
#
#                                     Main
#
# =============================================================================
#


#
# Command line
#


options, urls = parse_command_line()


if options.ilwdchar_compat:
	from glue.ligolw import ligolw
	from glue.ligolw import array as ligolw_array
	from glue.ligolw import lsctables
	from glue.ligolw import utils as ligolw_utils
	from glue.ligolw.utils import ligolw_add


#
# Input
#


@ligolw_array.use_in
@lsctables.use_in
class ContentHandler(ligolw.LIGOLWContentHandler):
	pass


xmldoc = ligolw_add.ligolw_add(
	ligolw.Document(),
	urls,
	non_lsc_tables_ok = options.non_lsc_tables_ok,
	verbose = options.verbose,
	contenthandler = ContentHandler
)


#
# Output
#


ligolw_utils.write_filename(xmldoc, options.output, verbose = options.verbose)


#
# Remove input
#


if options.remove_input:
	ligolw_add.remove_input(urls, [options.output] + options.remove_input_except, options.verbose)
