#!/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 trace --dest www.example.com
      Trace from here to www.example.com

  task trace --source ps2.example.org --dest www.example.com
      Trace from ps2.example.org to www.example.com

  task trace --length 512 --dest www.example.com
      Use 512-byte probe packets

  task trace --sendwait PT3S --dest www.example.com
      Wait 3 seconds between probes
"""
                                            )

opt_parser.add_option("--algorithm",
                      help="Trace algorithm",
                      action="store", type="string",
                      dest="algorithm")

opt_parser.add_option("--as",
                      help="Find AS for each hop",
                      action="store_true",
                      dest="as_")
opt_parser.add_option("--no-as",
                      help="Don't find AS for each hop",
                      action="store_false",
                      dest="as_")

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

opt_parser.add_option("--ip-version",
                      help="IP Version",
                      action="store", type="int",
                      dest="ipversion")

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

opt_parser.add_option("--probe-type",
                      help="Probe type",
                      action="store", type="string",
                      dest="probetype")

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

opt_parser.add_option("--first-ttl",
                      help="First TTL value",
                      action="store", type="int",
                      dest="firstttl")

opt_parser.add_option("--flow-label",
                      help="IPv6 flow label (ignored for IPv4)",
                      action="store", type="int",
                      dest="flowlabel")

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

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

opt_parser.add_option("--hops",
                      help="Maximum number of hops",
                      action="store", type="int",
                      dest="hops")

opt_parser.add_option("--queries",
                      help="Queries sent per hop",
                      action="store", type="int",
                      dest="queries")

opt_parser.add_option("--hostnames",
                      help="Resolve IPs to host names",
                      action="store_true",
                      dest="hostnames")
opt_parser.add_option("--no-hostnames",
                      help="Don't resolve IPs to host names",
                      action="store_false",
                      dest="hostnames")

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

opt_parser.add_option("--wait",
                      help="Wait time",
                      action="store", type="string",
                      dest="wait")

opt_parser.add_option("--sendwait",
                      help="Wait time between probes",
                      action="store", type="string",
                      dest="sendwait")


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


(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.algorithm is not None:
   result['algorithm'] = options.algorithm

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

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

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

if options.probetype is not None:
   result['probe-type'] = options.probetype

if options.fragment:
   result['fragment'] = True

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

if options.flowlabel is not None:
   result['flow-label'] = options.flowlabel
   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.hops is not None:
   result['hops'] = options.hops

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

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

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

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

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

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

if options.as_ is not None:
   result['as'] = options.as_ if \
       options.as_ is not None else True

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


pscheduler.succeed_json(result)
