#!/usr/bin/env python
"""
    Author: John E. Vincent
	Email:	lusis.org+github.com@gmail.com
"""
import datetime
import argparse
import yaml
import sys

import couchdbkit as couch
from couchdbkit.loaders import FileSystemDocsLoader
from restkit import BasicAuth

from vogeler.vogeler import VogelerServer, VogelerException

class SystemRecord(couch.Document):
    system_name = couch.StringProperty()
    updated_at = couch.DateTimeProperty()

def setup(pargs):
    pass

def load(lp, database):
    try:
        print "Loading design docs from %s" % lp
        loader = FileSystemDocsLoader(lp)
        loader.sync(database, verbose=True)
        print "Design docs loaded"
        return 0
    except:
        raise VogelerException('Failed to load design docs')
        return 1

def process_message(message):
    response = message
    syskey = message['syskey']
    format = message['format']
    del message['syskey']
    del message['format']

    print "Incoming message from: %s" % syskey

    # now we save the record
    # check if the systemrecord already exists
    # if not, create it. if so, update it
    doc = SystemRecord.get_or_create(syskey)

    for k, v in response.iteritems():
        print "Got response for: %s" % k
        try:
            if format == 'list':
                v_split = [z.strip() for z in v.split("\n")]
                doc[k] = v_split

            if format == 'yaml':
                v_dict = yaml.load(v)
                doc[k] = v_dict
        except:
            sys.exit(1)

    try:
        doc.system_name = syskey
        doc.updated_at = datetime.datetime.utcnow()
        doc.save()
    except:
        raise VogelerException("Unable to save record from request")

def startup(pargs):
    global s

    if pargs.config:
        config = pargs.config

    if pargs.setup:
        setup(pargs)

    if pargs.load:
        explicit_load = True
        loadpath = pargs.load
    else:
        explicit_load = False
        loadpath = 'etc/vogeler/_design'

    server = couch.Server(uri=pargs.dbhost)

    db = server.get_or_create_db(pargs.dbname)
    SystemRecord.set_db(db)

    if explicit_load == True:
        rc = load(loadpath, db)
        sys.exit(rc)
    else:
        print "Skipping design document load"

    s = VogelerServer(callback_function=process_message,
                    host=pargs.qhost,
                    username=pargs.quser,
                    password=pargs.qpass)
    s.monitor()

def shutdown():
    print "Shutting down"
    s.close()
    sys.exit(0)

if __name__ == "__main__":
    appdesc = 'Vogeler server daemon'
    parser = argparse.ArgumentParser(description=appdesc)
    parser.add_argument('--config', help='Configuration file', default='/etc/vogeler', required=False)

    runtime = parser.add_argument_group('startup options')
    runtime.add_argument('--dbhost',
            help='CouchDB server uri (i.e. http://localhost:5984 or http://user:pass@localhost:5984)',
            default='http://127.0.0.1:5984',
            required=False)
    runtime.add_argument('--dbname',
            help='CouchDB database name',
            default='system_records',
            required=False)
    runtime.add_argument('--qhost',
            help='RabbitMQ server address',
            default='localhost',
            required=False)
    runtime.add_argument('--quser',
            help='RabbitMQ user',
            default='guest',
            required=False)
    runtime.add_argument('--qpass',
            help='RabbitMQ password',
            default='guest',
            required=False)

    configuration = parser.add_argument_group('setup options')
    configuration.add_argument('--setup', '-s', action='store_true', default=False, help='Setup exchanges and create db/load design docs in CouchDB', required=False)
    configuration.add_argument('--load', '-l', help='Load design docs in CouchDB. Provide full path to desgin docs')

    args = parser.parse_args()

    try:
        startup(args)
    except KeyboardInterrupt:
        shutdown()
# vim: set ts=4 et sw=4 sts=4 sta filetype=python :
