#!/usr/bin/python3
#
# 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
import ligolw
from ligolw import __author__, __date__, __version__
from ligolw.utils import ligolw_add


#
# =============================================================================
#
#                                 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("-i", "--input-cache", metavar = "filename", action = "append", default = [], help = "Get input files from the LAL cache named filename.  They will be loaded after any files named directly on the command line.")
	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()

	# append the contents of the cache file, if provided, to the urls
	# on the command line
	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.verbose:
	ligolw.set_verbose(True, application = "ligolw_add")


#
# Input, add, output, remove obsoleted input.  Note that if the output
# document was also one of the input documents then it will be deleted if
# the remove_input feature is turned on, and a new file written with the
# same name.
#


ligolw.utils.write_filename(
	ligolw_add.ligolw_add(
		ligolw.Document(),
		urls,
		non_lsc_tables_ok = options.non_lsc_tables_ok,
		remove_input = options.remove_input,
		preserves = options.remove_input_except
	),
	options.output
)
