
import com.webobjects.foundation.NSArray;
import com.webobjects.foundation.NSMutableArray;
import com.webobjects.foundation.NSDictionary;
import com.webobjects.eoapplication.client.EOClientApplicationSupport;
import com.webobjects.eoapplication.EOApplication;

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

public class JCClient {

	private static String[] _args;
    private static String protocol = "t3";
    static private String	_host;
    static private int		_localPort;
	
	public static void main(String args[]) {
        
		
		_args = new String[]{"-applicationURL", "http://localhost:49955/cgi-bin/WebObjects/MyClient.woa"};
			
		Properties pro = System.getProperties();
		boolean hasProxy = false;
		String proxy = pro.getProperty("proxySet");
		if (proxy != null && proxy.equals("true")) hasProxy = true;
		if (hasProxy) {
			System.out.println("Attempting to contact server...");
			EOClientApplicationSupport.main(proxyArgsFromArgs(_args));
			System.out.println("Server contact successful!");
		}
		else {
			EOClientApplicationSupport.main(_args);
		}
	}
	
	private static String[] proxyArgsFromArgs(String[] args)
 {
         String applicationURL = "";
         int argsLength = args.length;
         int appURLIndex = 0;
         for (int i = 0; i < argsLength; ++i)
         {
           if (args[i].equals("-applicationURL")) {
             applicationURL = args[i + 1];
             appURLIndex = i + 1;
           }
         }

         if (appURLIndex == 0)
         {
           System.out.println("Missing -applicationURL argument. Exiting.");
           System.exit(1);
         }

         String restOfURL;
         String protocolEnd = "://";
         int startIndex = applicationURL.indexOf(protocolEnd);
         startIndex += protocolEnd.length();
         protocol = applicationURL.substring(0, startIndex);
         int endIndex = applicationURL.indexOf("/", startIndex);
         _host = applicationURL.substring(startIndex, endIndex);
         restOfURL = applicationURL.substring(endIndex,
                                                                                         applicationURL.length());

         String hostName;
         int portNumber;
         endIndex = _host.indexOf(":");
         if (endIndex == -1)
         {
                 hostName = _host;
                 portNumber = 80;
         }
         else
         {
                 hostName = _host.substring(0, endIndex);
                 portNumber = Integer.valueOf(_host.substring(endIndex + 1,
                 _host.length())).intValue();
         }

         TCPProxy proxy = null;
         try {
                 proxy = new TCPProxy(hostName, portNumber, 8080, 8888);
         } catch (IOException e) {
                 e.printStackTrace();
                 System.exit(1);
         }
         int localPort = proxy.localListenPort();
//System.out.println("HERE?");
         applicationURL = protocol + "127.0.0.1" + ":" + localPort + restOfURL;

         args[appURLIndex] = applicationURL;

         return args;
 }
}