#!/usr/bin/env python3

#
# Development Order #4:
# 
# This file encodes CLI arguments as JSON data in a test spec,
# as defined by the datatypes in validate.py
# 
# This can be tested directly using the following syntax:
# ./cli-to-spec --option argument
#

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

# TODO: Write an epilog.
opt_parser = pscheduler.FailingOptionParser(epilog=
"""
    This software is a plugin for the pScheduler project that adds a
    disk-to-disk feature to the testing suite. This test supports three
    tools: globus, curl and ftp.

    GridFTP test:
        pscheduler task --tool globus disk-to-disk \
        --source ftp://sunn-dtn.es.net:2811/data1/10M.dat \
        --dest file:///tmp/test.out --timeout PT10S

    Standard FTP test:
        pscheduler task --tool ftp disk-to-disk \
        --source ftp://speedtest.tele2.net/1KB.zip \
        --dest /tmp/test.out --timeout PT5S
   
      

"""
)

# Add all potential command line options here
# Check https://docs.python.org/2/library/optparse.html for more
# documentation on the opt parser


opt_parser.add_option("--duration",
                      help="Duration of idle test.",
                      action="store", type="string",
                      dest="duration")

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

opt_parser.add_option("--host",
                      help="Host where the test should be run",
                      action="store", type="string",
                      dest="host")

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


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

opt_parser.add_option("--cleanup",
                      help="If specified then test file is not deleted after transfer",
                      action="store_false",
                      dest="cleanup")

opt_parser.add_option("--parallel", 
                      help="Number of parallel streams to use with GridFTP transfers",
                      action="store", type="int",
                      dest="parallel")

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

result = { 'schema': 1 }

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

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

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

if options.parallel is None:
  result["parallel"] = 1
else:
  if options.parallel <= 1:
    pscheduler.fail('Error: Number of parallel streams must be greater than 1')
  result["parallel"] = options.parallel

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

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

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

#if result['testtype'] not in [ 'api', 'system' ]:
#    pscheduler.fail('Invalid test type. Choose api or system.')

pscheduler.succeed_json(result)
