#!/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 rtt --dest ps.example.com
      Measure round trip time from here to ps.example.com

  task rtt --source ps2.example.org --dest ps.example.com
      Measure round trip time from ps2.example.org to www.example.com

  task rtt --length 512 --dest ps.example.com
      Use 512-byte packets

  task rtt --ttl 12 --dest ps.example.com
      Limit time to live to 12 hops
"""
                                            )


opt_parser.add_option("-c","--count",
                      help="Test count",
                      action="store", type="int",
                      dest="count")

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

opt_parser.add_option("--flow-label",
                      help="Flow label",
                      action="store", type="int",
                      dest="flow_label")

opt_parser.add_option("--fragment",
                      help="Allow packet fragmentation",
                      action="store_true",
                      dest="fragment")
opt_parser.add_option("--no-fragment",
                      help="Don't allow packet fragmentation",
                      action="store_false",
                      dest="fragment")

opt_parser.add_option("--hostnames",
                      help="Look up hostnames from IPs",
                      action="store_true",
                      dest="hostnames")
opt_parser.add_option("--no-hostnames",
                      help="Don't look up hostnames from IPs",
                      action="store_false",
                      dest="hostnames")

opt_parser.add_option("-i","--interval",
                      help="Time to wait between packets sent",
                      action="store", type="string",
                      dest="interval")

opt_parser.add_option("--ip-version",
                      help="IP version to use",
                      action="store", type="int",
                      dest="ip_version")

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

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

opt_parser.add_option("--suppress-loopback",
                      help="Suppress multicast loopback",
                      action="store_true",
                      dest="suppress_loopback")
opt_parser.add_option("--no-suppress-loopback",
                      help="Don't suppress multicast loopback",
                      action="store_false",
                      dest="suppress_loopback")

opt_parser.add_option("--ip-tos",
                      help="IP type-of-service octet (integer)",
                      action="store", type="int",
                      dest="ip_tos")

opt_parser.add_option("--length",
                      help="Packet length",
                      action="store", type="int",
                      dest="length")

opt_parser.add_option("--ttl",
                      help="Time to live",
                      action="store", type="int",
                      dest="ttl")

opt_parser.add_option("--deadline",
                      help="Deadline for all measurements to complete",
                      action="store", type="string",
                      dest="deadline")

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

opt_parser.add_option("--port",
                      help="TCP or UDP port for protocols that use one",
                      action="store", type="int",
                      dest="port")

opt_parser.add_option("--protocol",
                      help="Protocol used to measure round trip time",
                      action="store", type="string",
                      dest="protocol")


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

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


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

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

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

if options.flow_label is not None:
   result['flow-label'] = options.flow_label

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

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

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

if options.ip_version is not None:
   result['ip-version'] = options.ip_version

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.suppress_loopback is not None:
   result['suppress-loopback'] = options.suppress_loopback

if options.ip_tos is not None:
   result['ip-tos'] = options.ip_tos

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

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

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

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

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

if options.protocol is not None:
   result['protocol'] = options.protocol
   schema.set(2 if options.protocol in ['icmp', 'twamp'] else 4)

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

pscheduler.succeed_json(result)
