#!/usr/bin/env python
#
# Copyright (C) 2013  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 2 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.

## @file
# A program to check for gaps in frame cache files according to segment lists

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


from optparse import OptionParser
import sqlite3
import sys


from glue import lal
from glue.ligolw import ligolw
from glue.ligolw import lsctables
from glue.ligolw import dbtables
from glue.ligolw import utils
from glue.ligolw.utils import segments as ligolw_segments


class XMLContentHandler(ligolw.LIGOLWContentHandler):
	pass
lsctables.use_in(XMLContentHandler)


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


def parse_command_line():
	parser = OptionParser(
	)
	parser.add_option("-s", "--segments-file", metavar = "filename", help = "Load segment lists from this .xml file (required).")
	parser.add_option("-n", "--segments-name", metavar = "name", help = "Load this segment list from the file (required).")
	parser.add_option("-v", "--verbose", action = "store_true", help = "Be verbose (optional).")

	options, filenames = parser.parse_args()

	required = ("segments_file", "segments_name")
	missing = [opt for opt in required if getattr(options, opt) is None]
	if missing:
		raise ValueError("missing options: %s" % ", ".join("--%s" % opt.replace("_", "-") for opt in missing))

	return options, filenames or [None]


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


#
# parse command line
#


options, filenames = parse_command_line()


#
# load segments and subtract time spanned by cache files
#


if options.segments_file.lower().endswith(".sqlite"):
	if options.verbose:
		print >>sys.stderr, "reading %s ..." % options.segments_file
	seglists = ligolw_segments.segmenttable_get_by_name(dbtables.get_xml(sqlite3.connect(options.segments_file)), options.segments_name).coalesce()
else:
	seglists = ligolw_segments.segmenttable_get_by_name(utils.load_filename(options.segments_file, contenthandler = XMLContentHandler, verbose = options.verbose), options.segments_name).coalesce()


for filename in filenames:
	if options.verbose:
		print >>sys.stderr, "loading %s ..." % (filename or "stdin")
	for line in open(filename) if filename else sys.stdin:
		seglists -= lal.CacheEntry(line).segmentlistdict


#
# check for remainders
#


if any(seglists.values()):
	print >>sys.stderr, "gaps found in cache.  total missing time: %s" % str(abs(seglists))
	sys.exit(1)
if options.verbose:
	print >>sys.stderr, "cache is complete"
