#!/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 
# 

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

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

amqp2amqp_identvalidationd = daemon.Daemon(
        stdin="/dev/null",
        stdout=rmap.settings.logfileamqp2amqp_identvalidationd,
        stderr=rmap.settings.errfileamqp2amqp_identvalidationd,
        pidfile=rmap.settings.lockfileamqp2amqp_identvalidationd,
        user=rmap.settings.useramqp2amqp_identvalidationd,
        group=rmap.settings.groupamqp2amqp_identvalidationd
)

credentials=pika.PlainCredentials(user, password)

sendconnection = pika.BlockingConnection(pika.ConnectionParameters(
    host=host,credentials=credentials))
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
  
    #At this point we can check if we trust this authenticated user... 
    ident=properties.user_id
    print "Received from user: %r" % ident 

    #but we check that message content is with the same ident
    try:
        amqp2amqp_identvalidationd.procs = [subprocess.Popen(["dba_transform","-u",ident], stdin=subprocess.PIPE, stdout=subprocess.PIPE)]

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

        status=amqp2amqp_identvalidationd.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 dba_transform"
        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        

    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))
    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_identvalidationd.cwd=os.getcwd()

    if amqp2amqp_identvalidationd.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_identvalidationd)  # (this code was run as script)

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

        sys.exit(0)
