#!/usr/bin/env python

import sys
import getopt

from sqlalchemy import create_engine
from esgcet.model import metadata
from esgcet.config import loadConfig

usage = """Usage:
    esgcreate_tables [options]

    Create the ESG node database.

Options:

    -i init_dir:
        Directory containing all initialization files.
        Recommended: one initialization file for the default sections (esg.ini) and one per project, must match the name format esg.<project>.ini
        If not specified, the default installed init files are read.

    -v: Verbose.
"""

def main(argv):

    try:
        args, lastargs = getopt.getopt(argv, "i:v")
    except getopt.error:
        print sys.exc_value
        print usage
        sys.exit(0)

    init_dir = '/esg/config/esgcet/'
    verbose = False
    for flag, arg in args:
        if flag=='-i':
            init_dir = arg
        elif flag=='-v':
            verbose = True

    config = loadConfig(init_dir)
    
    engine = create_engine(config.getdburl('DEFAULT'), echo=verbose, pool_recycle=3600)
    metadata.create_all(engine)

if __name__=='__main__':
    main(sys.argv[1:])
