#!/usr/bin/python3.13
#
# 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 lal.utils import CacheEntry


from ligo.lw import dbtables
from ligo.lw import utils as ligolw_utils
from ligo.lw.utils import segments as ligolw_segments


#
# =============================================================================
#
#                                 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("reading %s ..." % options.segments_file, file=sys.stderr)
	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(ligolw_utils.load_filename(options.segments_file, contenthandler = ligolw_segments.LIGOLWContentHandler, verbose = options.verbose), options.segments_name).coalesce()


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


#
# check for remainders
#


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