#!/usr/bin/env python
# GPL. (C) 2014 Paolo Patruno.

# This program is free software; you can redistribute it and/or modify 
# it under the terms of the GNU General Public License as published by 
# the Free Software Foundation; either version 2 of the License, or 
# (at your option) any later version. 
# 
# This program is distributed in the hope that it will be useful, 
# but WITHOUT ANY WARRANTY; without even the implied warranty of 
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
# GNU General Public License for more details. 
# 
# You should have received a copy of the GNU General Public License 
# along with this program; if not, write to the Free Software 
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
# 

import os
os.environ['DJANGO_SETTINGS_MODULE'] = 'rmap.settings'
import django
django.setup()

from rmap import daemon
import pika, subprocess
import rmap.settings

user=rmap.settings.amqpuser
password=rmap.settings.amqppassword
host="localhost"
queue="validatedjson"
outexchange="validated"
routing_key="validated"

DSNFILE="/dev/shm/amqp2amqp_json2bufrd.sqlite"
DSN="sqlite:"+DSNFILE


amqp2amqp_json2bufr = daemon.Daemon(
        stdin="/dev/null",
        stdout=rmap.settings.logfileamqp2amqp_json2bufrd,
        stderr=rmap.settings.errfileamqp2amqp_json2bufrd,
        pidfile=rmap.settings.lockfileamqp2amqp_json2bufrd,
        user=rmap.settings.useramqp2amqp_json2bufrd,
        group=rmap.settings.groupamqp2amqp_json2bufrd
)

credentials=pika.PlainCredentials(user, password)

sendconnection = pika.BlockingConnection(pika.ConnectionParameters(
    host=host,credentials=credentials,heartbeat_interval=0))
sendchannel = sendconnection.channel()

def callback(ch, method, properties, body):
    print " [x] Received message"

    #if properties.user_id is None:
    #    print "Ignore anonymous message"
    #    print " [x] Done"
    #    ch.basic_ack(delivery_tag = method.delivery_tag)
    #    return
  
    try:
        amqp2amqp_json2bufr.procs = [subprocess.Popen(["dbadb","import","--type=json","--wipe-first","--dsn="+DSN], stdin=subprocess.PIPE)]

        outbody,outerr=amqp2amqp_json2bufr.procs[0].communicate(input=body)

        status=amqp2amqp_json2bufr.procs[0].wait()
        if status != 0:
            print "There were some errors executing dbadb import error: ",status,outerr
            #print "skip message: "
            #print body
            #print "---------------------"

    except:
        print "There were some errors executing dbadb import"
        raise


    try:
        amqp2amqp_json2bufr.procs = [subprocess.Popen(["dbadb","export","--dsn="+DSN], stdout=subprocess.PIPE)]

        outbody,outerr=amqp2amqp_json2bufr.procs[0].communicate()

        status=amqp2amqp_json2bufr.procs[0].wait()
        if status != 0:
            print "There were some errors executing dbadb export error: ",status,outerr
            #print "skip message: "
            #print body
            #print "---------------------"

    except:
        print "There were some errors executing dbadb export"
        raise

    try:

        if (outbody == ""):
            print "skip empty output message"
        else:
            print "publish message: "
            #print outbody
            #print "---------------------"
        
            #set user_id property to don't get  "PRECONDITION_FAILED - user_id property set to 'guest' but authenticated user was 'rmap'
            properties.user_id=user

            # Turn on delivery confirmations
            sendchannel.confirm_delivery()

            # Send a message
            if sendchannel.basic_publish(exchange=outexchange,
                                        routing_key=routing_key,
                                         body=outbody,
                                         properties=properties):
                print 'Message publish was confirmed'
            else:
                print 'Message could not be confirmed'

            
            print " [x] message Sent "

    except:
        print "There were some errors publishing message"
        raise        

    finally:
        try:
            os.remove(DSNFILE)
        except:
            pass

    print " [x] Done"
    ch.basic_ack(delivery_tag = method.delivery_tag)


    # TODO how we can pass procs to daemon ? 

def main(self):

    connection = pika.BlockingConnection(pika.ConnectionParameters(
        host=host,credentials=credentials,heartbeat_interval=0))
    channel = connection.channel()
    #channel.queue_declare(queue=queue)

    print ' [*] Waiting for messages. To exit press CTRL+C'


    channel.basic_consume(callback,
                          queue=queue,
                          no_ack=False)

    channel.start_consuming()

    connection.close()
    sendconnection.close()


if __name__ == '__main__':

    import sys, os
    amqp2amqp_json2bufr.cwd=os.getcwd()

    if amqp2amqp_json2bufr.service():

        sys.stdout.write("Daemon started with pid %d\n" % os.getpid())
        sys.stdout.write("Daemon stdout output\n")
        sys.stderr.write("Daemon stderr output\n")

        main(amqp2amqp_json2bufr)  # (this code was run as script)

        for proc in amqp2amqp_json2bufr.procs:
            proc.wait()

        sys.exit(0)
