#! /usr/bin/python

# Copyright (C) 2005 Steffen Richter <steffen.richter@icecube.wisc.edu>
#  
# 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
#  

import MySQLdb
import getopt
import os
import sys

ConfigFile = "/usr/local/etc/domcal.properties"
DomCalDirectory = "/mnt/data/testdaq/domcal/new-files"

Verbose = 0
try:
    Options = getopt.getopt(sys.argv[1:], "v", ["verbose"])[0]
except getopt.GetoptError,err:
    print str(err)
    sys.exit(2)

for Option in Options:
    if "-v" == Option[0] or "--verbose" == Option[0]:
        Verbose = 1

NumberOfDomsPassed = 0
NumberOfDoms = 0
ConfigFileContents = open(ConfigFile).read().split('\n')
for Line in ConfigFileContents:
    Tokens=Line.split()
    if Tokens:
        if "icecube.daq.domcal.db.url:" == Tokens[0]:
            [Host,Database]=Tokens[1].replace("jdbc:mysql://","").split("/")
        elif "icecube.daq.domcal.db.user:" == Tokens[0]:
            User = Tokens[1];
        elif "icecube.daq.domcal.db.passwd:" == Tokens[0]:
            Password = Tokens[1];

Con = MySQLdb.connect(host=Host, port=3306,
                      user=User, passwd=Password,
                      db=Database)
Cursor = Con.cursor( )

sql = "SELECT mbid,domhub,card,pair,ab,thename,thedomid FROM domtune WHERE domhub IS NOT NULL ORDER BY domhub,card,pair,ab"
Cursor.execute(sql)

Results = Cursor.fetchall( )
Con.close
for Row in Results:
    NumberOfDoms += 1
    if os.path.exists(DomCalDirectory + "/domcal_"
                      + Row[0].lower() + ".xml"):
        NumberOfDomsPassed += 1
        if Verbose:
            print ("PASS: "
                   + "\n\tMB ID : " + Row[0].lower()
                   + "\n\tName  : " + Row[5]
                   + "\n\tDOM ID: " + Row[6]
                   + "\n\tHub   : " + Row[1]
                   + "\n\tCard  : " + str(Row[2])
                   + "\n\tPair  : " + str(Row[3])
                   + "\n\tAB    : " + Row[4]
                   + "\n"
                   )
    else:
        print ("FAIL: "
               + "\n\tMB ID : " + Row[0].lower()
               + "\n\tName  : " + Row[5]
               + "\n\tDOM ID: " + Row[6] 
               + "\n\tHub   : " + Row[1]
               + "\n\tCard  : " + str(Row[2])
               + "\n\tPair  : " + str(Row[3])
               + "\n\tAB    : " + Row[4]
               + "\n"
               )

if NumberOfDoms == NumberOfDomsPassed:
    print("A DOMCAL XML file exists for all "
          + str(NumberOfDoms) + " DOMs")
else:
    print("Only " +  str(NumberOfDomsPassed) +
          " of " + str(NumberOfDoms) + " DOMs"
          " have a DOMCAL XML file")
    sys.exit(1)
        
