#!/usr/bin/python2.6
# -*- python -*-

import argparse
import logging
import itertools
from lib.python import chkdbcat

class MyCheckDBCatalog(chkdbcat.CheckDBCatalog):
    """Class overriding CheckDBCatalog.notify()"""
    def __init__(self, catrel, arch, osrel, fn_ts, gencat_bin, chkcat, verbose=False):
        super(MyCheckDBCatalog,self).__init__(catrel, arch, osrel, fn_ts, gencat_bin, chkcat)
        self.__verbose = verbose

    def notify_broken(self, date, addr, pkginfo):
        notifier = chkdbcat.InformMaintainer((self._catrel, self._osrel, self._arch),
                                             date, addr, pkginfo)
        notfier.send_mail_broken()

    def notify_unbroken(self, date, addr):
        notifier = chkdbcat.InformMaintainer((self._catrel, self._osrel, self._arch),
                                             date, addr, pkginfo)
        notfier.send_mail_unbroken()


def argparser():
    parser = argparse.ArgumentParser(description='Check Database Catalog.')
    parser.add_argument('--debug', help='enable debug output', required=False, default=False, action='store_const', const=True)
    parser.add_argument('--verbose', help='be verbose', required=False, default=False, action='store_const', const=True)
    parser.add_argument('--arch', required=True,
                        help="i386, sparc",
                        nargs="+")
    parser.add_argument('--catalog-release', required=True,
                        help="unstable, kiel, etc.",
                        nargs="+")
    parser.add_argument('--os-release', required=True,
                        help="SunOS5.10, SunOS5.11",
                        nargs="+")
    parser.add_argument('--timestamp-file',
                        help="timestamp filepath (default: %(default)s)",
                        default='/var/cache/chkdbcat/timestamp.json')
    parser.add_argument('--chkcat-path',
                        help="path to chkcat binary (default: %(default)s)",
                        default='/opt/csw/bin/chkcat')
    parser.add_argument('--gen-catalog-path',
                        help="path to gen-catalog-index (default: %(default)s)",
                        default="/home/web/bin/gen-catalog-index")
    return parser.parse_args()

def main():
    args = argparser()

    if args.debug:
        logging.basicConfig(level=logging.DEBUG)

    had_errors = False
    for crel_arch_orel in itertools.product(args.catalog_release, args.arch, args.os_release):
        catalog_release = crel_arch_orel[0]
        arch = crel_arch_orel[1]
        os_release = crel_arch_orel[2]

        if args.verbose: print("Checking Database Catalog {0} {2} {1}".format(catalog_release, arch, os_release))
        with MyCheckDBCatalog(catalog_release, arch,
                              os_release, args.timestamp_file,
                              args.gen_catalog_path,
                              args.chkcat_path, args.verbose) as checker:
            success = checker.check()
            if args.verbose:
                if success:
                    print("Database Catalog {0} {2} {1}: OK".format(catalog_release, arch, os_release))
                else:
                    print("Database Catalog {0} {2} {1}: FAILED".format(catalog_release, arch, os_release))
                    print("chkcat output")
                    print checker.stderr

            had_errors = had_errors or not success

    exit(0) if had_errors else exit(1)

if __name__ == '__main__':
    main()
