#!/usr/bin/env python3
#
# Convert comamnd-line options to a test specification

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(epilog=
"""Examples:

  task simplestream --dest ps.example.com
      Send data from here to ps.example.com

  task simplestream --test-material "Hello, world!" --dest ps.example.com
      Use a custom string for the data to be sent

  task simplestream --dawdle PT30S --dest ps.example.com
      Wait up to 30 seconds before sending the data

  task simplestream --fail 0.65 --dest ps.example.com
      Force failure 65% of the time
"""
                                            )


opt_parser.add_option("-e", "--dest",
                      help="Destination host",
                      action="store", type="string",
                      dest="dest")

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

opt_parser.add_option("-d", "--dawdle",
                      help="Time to dawdle (ISO8601 Duration)",
                      action="store", type="string",
                      dest="dawdle")

opt_parser.add_option("-i", "--ip-version",
                      help="Force IP version (4 or 6)",
                      action="store", type="int",
                      dest="ip_version")

opt_parser.add_option("-m", "--test-material",
                      help="Test material to be sent",
                      action="store", type="string",
                      dest="test_material")

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

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

opt_parser.add_option("-t", "--timeout",
                      help="Time for receiver to wait (ISO8601 Duration)",
                      action="store", type="string",
                      dest="timeout")

opt_parser.add_option("-f", "--fail",
                      help="Probability of forced failure",
                      action="store", type="float",
                      dest="fail")

opt_parser.add_option("-p", "--port",
                      help="port",
                      action="store", type="int",
                      dest="port")

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

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


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

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

if options.dest_node is not None:
   result['dest-node'] = options.dest_node

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

if options.ip_version is not None:
   result['ip-version'] = options.ip_version
   spec_schema.set(2)

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.test_material is not None:
   result['test-material'] = options.test_material

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

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

if options.port is not None:
   result['port'] = options.port
   spec_schema.set(3)

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

pscheduler.succeed_json(result)
