#!/usr/bin/env python
from __future__ import unicode_literals
import web
import sys
from sys import argv
from urllib import urlencode
from secure_connection import SecureConnection
import config

TEST_REQUEST = """{"command": "cat",
                   "args": ["/etc/group", "$1"],
                   "files": ["test command", "prueba\\n dos"],
                   "sequential": "false",
                   "type": "command"}"""

def send_job(job_id=1, request=None):
    connection = SecureConnection(config.client_daemon_ip, config.client_daemon_port)
    url = '/jobrequest/%s' % job_id
    if not request:
        request = TEST_REQUEST
    post_data = urlencode({'request': request})
    headers = {"Content-type": "application/x-www-form-urlencoded",
        "Accept": "text/plain"}
    connection.request('POST', url, post_data, headers)
    response = connection.getresponse()
    print config.client_daemon_ip, config.client_daemon_port
    print response.status, response.read()

if __name__ == "__main__":
    if len(argv) == 2:
        if argv[1] == 'default':
            send_job()
            sys.exit()

    if len(argv) != 3:
        print "Usage: %s [job_id] [request]" % argv[0]
        print "Usage: %s default" % argv[0]
    else:
        send_job(argv[1], argv[2])
