#!/usr/bin/env python3

#
# Convert command-line options to a test specification for http test
#

import re
import optparse
import pscheduler
import sys

if len(sys.argv) > 1:

    # Args are on the command line
    args = sys.argv[1:]

else:

    # Args are in a JSON array on stdin
    json_args = pscheduler.json_load(exit_on_error=True)
    args = []

    if not isinstance(json_args,list):
        pscheduler.fail("Invalid JSON for this operation")
    for arg in json_args:
        if not ( isinstance(arg, str)
                 or isinstance(arg, int)
                 or isinstance(arg, float) ):
            pscheduler.fail("Invalid JSON for this operation")
    args = [ str(arg) for arg in json_args ]


# Gargle the arguments

opt_parser = pscheduler.FailingOptionParser()


opt_parser.add_option("--source",
                      help="Source address",
                      action="store", type="string",
                      dest="source")

opt_parser.add_option("--source-node",
                      help="Host to run the test",
                      action="store", type="string",
                      dest="source_node")

opt_parser.add_option("--dest",
                      help="pScheduler server",
                      action="store", type="string",
                      dest="dest")

opt_parser.add_option("--timeout",
                      help="Timeout for query",
                      action="store", type="string",
                      dest="timeout")

(options, remaining_args) = opt_parser.parse_args(args)

if len(remaining_args) != 0:
    pscheduler.fail("Unusable arguments: %s" % " ".join(remaining_args))

schema = pscheduler.HighInteger(1)
result = { }

if options.source is not None:
    result['source'] = options.source

if options.source_node is not None:
    result['source-node'] = options.source_node

if options.dest is not None:
   result["dest"] = options.dest
 
if options.timeout is not None:
    result['timeout'] = options.timeout

result['schema'] = schema.value()

pscheduler.succeed_json(result)
