#!/usr/bin/env python
# Copyright (C) 2012 Etienne Robillard 
# All rights reserved.
# <LICENSE=ISC>
#
"""invokes schevo syncdb ... using the native Schevo API."""
import sys, os, commands
from argparse import ArgumentParser

INITDB_CONF = os.environ.get('SCHEVO_INITDB_PATH', '/etc/schevo/initdb.conf')

def get_command_parser():
    p = ArgumentParser(__file__)
    p.loadArgumentsFromFile(INITDB_CONF)

def initdb(dbname):
    retval = None
    if not os.path.exists(dbname):
        # create the db if not already existing
        print "Creating new database file=%s" % dbname
        retval = commands.getoutput('schevo db create %s' % dbname)
    if not retval:
        'schevo db created!'
        return None
    else:
        return retval

if __name__ == '__main__':
    try:
        dbname = os.environ['XDSERVER_ROOTDIR']
    except KeyError:
        raise KeyError("Please set the XDSERVER_ROOTDIR variable to a true value.")
    else:
        ret = initdb(dbname)
        if ret:
            print ret


