#!/usr/bin/env python
################################################################################
#\defgroup difxsendfile DiFXsendFile
#
#\brief Send data to a file location on the server.
#
#   Usage:  <b><code>DiFXsendFile [args] [source path] \<destination path\></code></b>
#
#   <i>DiFXsendFile</i> sends data to a specified path destination on the DiFX
#  server.  If two path names are given as arguments the first is assumed to
#  be the full path of a local file from which data will be read, and the second
#  is assumed to be its destination on the server.  If one path name is given
#  as an argument it is interpreted as the destination and data are expected
#  from stdin.
#
#  <h3>Command Line Arguments</h3>
#
#  <table border="0" cellspacing="25">
#  <tr><td><pre><b>-h, --help</b></pre>                   <td>Print help information and quit.
#  <tr><td><pre><b>-H, --hostname <i>NAME</i></b></pre>   <td>Use <i>NAME</i> as the host of the difxServer program.
#                                                          Default is to use DIFX_CONTROL_HOST environment variable.
#  <tr><td><pre><b>-P, --port <i>PORT</i></b></pre>       <td>Use <i>PORT</i> as the TCP port to communicated with the difxServer.
#                                                          Default is to use DIFX_CONTROL_PORT environment variable.
#  </table
#
################################################################################
program = 'DiFXsendFile'
version = '0.1'
author  = 'John Spitzak'
verdate = '20151021'

import sys
import time
import DiFXFileTransfer

difx = None

#===============================================================================
#  MAIN
#===============================================================================
host = None
port = None
source = None
destination = None

try:
	i = 1
	while i < len( sys.argv ):
		#  Check against legal argument types.  Anything we don't recognize is assumed
		#  to be an argument or a path.
		if sys.argv[i] in [ "-h", "--help" ]:
			print('\n%s ver %s  %s  %s' % (program, version, author, verdate))
			print("Send data from either a named file or stdin to a destination file")
			print("on the server.")
			print("Usage: %s [options] [source] <destination>" % ( sys.argv[0] ))
			print("")
			print("Options can include:")
			print("")
			print("   --help")
			print("   -h         Print this help information and quit.")
			print("")
			print("   --hostname NAME")
			print("   -H NAME    Use NAME as the host of the difxServer program.")
			print("              Default is to use DIFX_CONTROL_HOST environment variable.")
			print("")
			print("   --port PORT")
			print("   -P PORT    Use PORT as the TCP port to communicated with the difxServer.")
			print("              Default is to use DIFX_CONTROL_PORT environment variable.")
			print("")
			exit( 0 )
		elif sys.argv[i] in [ "-H", "--hostname" ]:
			host = sys.argv[i+1]
			i = i + 1
		elif sys.argv[i] in [ "-D", "--difx" ]:
			DiFXVersion = sys.argv[i+1]
			i = i + 1
		elif sys.argv[i] in [ "-P", "--port" ]:
			port = int( sys.argv[i+1] )
			i = i + 1
		else:
			#  There may only be a destination, or there might be a source and a
			#  destination.  We'll sort that out below.
			if source == None:
				source = sys.argv[i]
			else:
				destination = sys.argv[i]
		i = i + 1
		
except RuntimeError:
	print("Usage: %s [options] [source] <destination>" % ( sys.argv[0] ))
	exit( 0 )
	
#  If only one path was specified, it is a destination - so we have to fix our
#  interpretation of the command line and read data from stdin.
if destination == None:
	destination = source
	data = sys.stdin.read()
else:
	f = open( source, "r" )
	data = f.read()
	
#  Open a new connection to the difxServer...
difx = DiFXFileTransfer.Client()
difx.connect( host, port )

if difx.socketOK:
	difx.monitor()
	fileSize = difx.sendFile( destination, data )
	print("wrote " + str( fileSize ) + " bytes")
difx.close()