#!/usr/bin/env jython

import sys
import os
import socket
import stilts
from java.lang import *
from java.sql import *

from config import Config
from scratchdb import ScratchDb
from ipptopspsdb import IppToPspsDb

'''
SetupScratchDb class
'''
class SetupScratchDb(object):

    ## XXX EAM : 2013.01.31 - i have changed the 'config.py' concept
    ## to only include the global static information (from
    ## settings.xml), and stripped out the dynamic information to a
    ## new entity called 'skychunk'

    ## XXX note that setupScratchDb.py fails if there are no valid
    ## entries in the config table (or if it is passed a non-existent
    ## config) This is because ipptopsps.__init__ calls
    ## checkClientStatus, which calls refreshConfig, which calls
    ## readConfig, which attempts to select the matching entry from
    ## the config table.  if the select returns 0 rows, the code
    ## crashes.  even if it were not to crash (eg, if it returned
    ## False), checkClientStatus would enter a waiting loop until the
    ## config existed.
    
    ## since a scratchDb is needed to do basically anything, it seems
    ## like 'setupScratchDb' should not be dependent on the details of
    ## the configs, etc.

    ## what are the options?  I think this is a problem of poor
    ## factoring / dependencies, esp for 'config.py'.  there are two
    ## (or more) aspects to 'config.py' : management of the dynamic
    ## 'config' information (basically, the regions / dvodbs of
    ## interest) and manipulation of the static / system configuration
    ## information.

    ## these concepts should probably be split and the more general
    ## part (settings & logger) placed in one module and the rest elsewhere.

    ## as for setupScratchDb.py, it looks like it only uses the static
    ## settings info and the logger functions, so it should not be a
    ## child of the ipptopsps class

    '''
    Constructor
    '''
    def __init__(self, argv):
        # set up config object
        self.config = Config()

        if len(argv) < 2:
            self.printUsage()
            self.exitProgram("incorrect args")

        # some class constants
        self.HOST = socket.gethostname()
        self.PID = os.getpid()

        # XXX Shouldn't config set up logger? 
        # (not yet, since we set HOST & PID with program...)
        self.logger = self.config.getLogger(self.HOST, self.PID, 1, 0)

        try:
            self.scratchDb = ScratchDb(self.logger, self.config, argv[1])
        except:
            self.exitProgram("Could not connect to a scratch Db")
            raise

    def run(self):

        self.logger.infoSeparator()

        # install stored procedure
        self.logger.infoPair("Installing",  "psfLikelihood() stored procedure")
        self.scratchDb.createPsfLikelihoodFunction()

        # install IN tables
        self.logger.infoPair("Installing", "initialization tables")
        tablepath = self.config.configDir + "tables.IN.vot"
        tables = stilts.treads(tablepath)

        for table in tables:
            # EAM TEST I/O
            self.logger.infoPair("Creating IN table: ", table.name)
            self.logger.debug("Creating IN table: " + table.name)
            table.write(self.scratchDb.url + '#' + table.name)

        # create basic DVO tables
        self.logger.infoPair("Installing", "initialization tables")
        self.scratchDb.createDvoTables()

    def printUsage(self):
        # write message to log or stdout if no logger set up
        msg = os.path.basename(sys.argv[0]) + " <scratchdb_version> "
        try:
            self.logger.errorPair("Usage:", msg)
        except: 
            print "*** Usage: " + msg

'''
Program starts here
'''
try:
    setupScratchDb = SetupScratchDb(sys.argv)
    setupScratchDb.run()
    setupScratchDb.exitProgram("completed")
except: pass
