/*****************************************************************************
 *                                                                           *
 *         Licensed Materials - Property of IBM                              *
 *                                                                           *
 *         "Restricted Materials of IBM"                                     *
 *                                                                           *
 *         5799-QRY                                                          *
 *                                                                           *
 *         (C) Copyright IBM Corp. 1998                                      *
 *                                                                           *
 *****************************************************************************/
package com.ibm.devtools.SIPNoTE;

import java.io.*;
import java.net.*;
import java.util.*;

/**
 * Application class for sending a Jephyr message.
 *
 * Upon invocation, the user is prompted to enter the message followed
 * by a '.' to initiate the sending of the message.  The sender's name
 * is pulled from the system.user setting (usually the USER environment
 * variable.
 *
 * @author Bill Nagy
 */
public class jwrite
{
  static Properties props = new Properties();

  /**
   * @param argv A list of userid/instance recipients for the message
   */
  public static void main(String[] argv)
  {
    FileInputStream fin;
    int localport;

    try
      {
	fin = new FileInputStream(".sipnote");
	props.load(fin);
      }
    catch (FileNotFoundException e)
      {
      }
    catch (IOException e)
      {
	System.err.println("I/O Error loading .sipnote file --> "+e);
	return;
      }

    localport = (new Integer(props.getProperty("SNClient.jport",
					       "5062")).intValue());

    if (argv.length < 1)
      {
	System.out.println("Usage: jwrite <userid[s]>");
	return;
      }

    //Want to check to see if all arguments are names
    int numto = argv.length;

    String[] to = new String[numto];
    
    System.arraycopy(argv, 0, to, 0, numto); /*Copy the names to the to list*/

    System.out.println("Enter your message followed by a '.' on a line by itself.");

    String[] messagelines = new String[20];
    int currentline = 0;
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    while(true)
      {
	try
	  {
	    messagelines[currentline] = br.readLine();
	  } catch(IOException e)
	    {
	      System.err.println("I/O Error --> " +e);
	      return;
	    }
	  
	  if (messagelines[currentline] == null)
	    {
	      System.err.println("Error reading input");
	      return;
	    }
	  if (messagelines[currentline].equals("."))
	    break;
	  currentline++;
	  if (currentline == messagelines.length) /* Need to increase array */
	    {
	      String [] tmpVar = new String[messagelines.length + 20];
	      System.arraycopy(messagelines, 0, tmpVar, 0,
			       messagelines.length);
	      messagelines = tmpVar;
	    }
      }

    String [] tmpVar = new String[currentline];
    System.arraycopy(messagelines, 0, tmpVar, 0, currentline);
    messagelines = tmpVar;

    JMessage message = new JMessage(messagelines,
				    System.getProperty("user.name"),
				    to);
    
    try
      {
	Socket server = new Socket("127.0.0.1", localport);
	ObjectOutputStream out =
	  new ObjectOutputStream(server.getOutputStream());
	ObjectInputStream in =
	  new ObjectInputStream(server.getInputStream());
	
	out.writeObject(message);
	out.flush();

	server.close();
      }
    catch(IOException e)
      {
	System.err.println("I/O Error --> " + e);
      }
  }
}
