#!/usr/bin/env jython

import os.path
import sys
import time
import stilts
from java.lang import *
from java.sql import *
import logging.config

from java.lang import Math
from org.apache.commons.math.special import Erf
from xml.etree.ElementTree import ElementTree, Element, tostring

from pslogger import PSLogger
from scratchdb import ScratchDb

'''
Start of program.
'''

if len(sys.argv) > 1: CONFIG = sys.argv[1]
else:
    print "** Usage: " + sys.argv[0] + " <configPath> [init]"
    sys.exit(1)

# open config file
configDoc = ElementTree(file=CONFIG)

# set up logging
logging.setLoggerClass(PSLogger)
logger = logging.getLogger("setupScratchDb")
logger.setup(configDoc, "setupScratchDb")

scratchDb = ScratchDb(logger, configDoc)

# install stored procedure
logger.info("Installing psfLikelihood() stored procedure")
scratchDb.createPsfLikelihoodFunction()

# install IN tables
logger.info("Installing initialization tables")
tables = stilts.treads("../config/IN/tables.vot")
for table in tables:
    logger.debug("Creating IN table: " + table.name)
    table.write(scratchDb.url + '#' + table.name)

logger.info("Done")

