context: /*
    Copyright (C) 2002, 2003, 2004, 2005, 2006 Jeroen Frijters
  
    This software is provided 'as-is', without any express or implied
    warranty.  In no event will the authors be held liable for any damages
    arising from the use of this software.
  
    Permission is granted to anyone to use this software for any purpose,
    including commercial applications, and to alter it and redistribute it
    freely, subject to the following restrictions:
  
    1. The origin of this software must not be misrepresented; you must not
       claim that you wrote the original software. If you use this software
       in a product, an acknowledgment in the product documentation would be
       appreciated but is not required.
    2. Altered source versions must be plainly marked as such, and must not be
       misrepresented as being the original software.
    3. This notice may not be removed or altered from any source distribution.
  
    Jeroen Frijters
    jeroen@frijters.net
    
  */
  using System;
  using System.IO;
  using System.Reflection;
  using IKVM.Internal;
  
  using ikvm.runtime;
  using java.lang.reflect;
  using java.net;
  using java.util.jar;
  using java.io;
  
  using Console = System.Console;
  using System.Diagnostics;
  
  public class Starter
  {
  	private class Timer
  	{
  		private static Timer t;
  		private DateTime now = DateTime.Now;
  
  		internal Timer()
  		{
  			t = this;
  		}
  
  		~Timer()
  		{
  			Console.WriteLine(DateTime.Now - now);
  		}
  	}
  
  	private class SaveAssemblyShutdownHook : java.lang.Thread
  	{
  		private java.lang.Class clazz;
  
  		internal SaveAssemblyShutdownHook(java.lang.Class clazz)
  			: base("SaveAssemblyShutdownHook")
  		{
  			this.clazz = clazz;
  		}
  
  		public override void run()
  		{
  			System.Threading.Thread.CurrentThread.Priority = System.Threading.ThreadPriority.AboveNormal;
  			Console.Error.WriteLine("Saving dynamic assembly...");
  			try
  			{
  				IKVM.Internal.Starter.SaveDebugImage();
  				Console.Error.WriteLine("Saving done.");
  			}
  			catch(Exception x)
  			{
  				Console.Error.WriteLine(x);
  				Console.Error.WriteLine(new System.Diagnostics.StackTrace(x, true));
  				System.Diagnostics.Debug.Assert(false, x.ToString());
  			}
  		}
  	}
  
  	private class WaitShutdownHook : java.lang.Thread
  	{
  		internal WaitShutdownHook()
  			: base("WaitShutdownHook")
  		{
  		}
  
  		public override void run()
  		{
  			Console.Error.WriteLine("IKVM runtime terminated. Waiting for Ctrl+C...");
  			for(;;)
  			{
  				System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);
  			}
  		}
  	}
  
  	[STAThread]	// NOTE this is here because otherwise SWT's RegisterDragDrop (a COM thing) doesn't work
  	[IKVM.Attributes.HideFromJava]
  	static int Main(string[] args)
  	{
  		Tracer.EnableTraceConsoleListener();
  		Tracer.EnableTraceForDebug();
  		System.Collections.Hashtable props = new System.Collections.Hashtable();
  		string classpath = Environment.GetEnvironmentVariable("CLASSPATH");
  		if(classpath == null || classpath == "")
  		{
  			classpath = ".";
  		}
  		props["java.class.path"] = classpath;
  		bool jar = false;
  		bool saveAssembly = false;
  		bool saveAssemblyX = false;
  		bool waitOnExit = false;
  		bool showVersion = false;
  		string mainClass = null;
  		int vmargsIndex = -1;
          bool debug = false;
          String debugArg = null;
  		for(int i = 0; i < args.Length; i++)
  		{
              String arg = args[i];
  			if(arg[0] == '-')
  			{
  				if(arg == "-help" || arg == "-?")
  				{
  					break;
  				}
  				else if(arg == "-Xsave")
  				{
  					saveAssembly = true;
  					IKVM.Internal.Starter.PrepareForSaveDebugImage();
  				}
  				else if(arg == "-XXsave")
  				{
  					saveAssemblyX = true;
  					IKVM.Internal.Starter.PrepareForSaveDebugImage();
  				}
  				else if(arg == "-Xtime")
  				{
  					new Timer();
  				}
  				else if(arg == "-Xwait")
  				{
  					waitOnExit = true;
  				}
  				else if(arg == "-Xbreak")
  				{
  					System.Diagnostics.Debugger.Break();
  				}
  				else if(arg == "-Xnoclassgc")
  				{
  					IKVM.Internal.Starter.ClassUnloading = false;
  				}
  				else if(arg == "-jar")
  				{
  					jar = true;
  				}
  				else if(arg == "-version")
  				{
  					Console.WriteLine(Startup.getVersionAndCopyrightInfo());
  					Console.WriteLine();
  					Console.WriteLine("CLR version: {0} ({1} bit)", Environment.Version, IntPtr.Size * 8);
  					System.Type type = System.Type.GetType("Mono.Runtime");
  					if(type != null)
  					{
  						Console.WriteLine("Mono version: {0}", type.InvokeMember("GetDisplayName", BindingFlags.InvokeMethod | BindingFlags.Static | BindingFlags.NonPublic, null, null, new object[0]));
  					}
  					foreach(Assembly asm in AppDomain.CurrentDomain.GetAssemblies())
  					{
  						Console.WriteLine("{0}: {1}", asm.GetName().Name, asm.GetName().Version);
  					}
  					string ver = java.lang.System.getProperty("openjdk.version");
  					if(ver != null)
  					{
  						Console.WriteLine("OpenJDK version: {0}", ver);
  					}
  					return 0;
  				}
  				else if(arg == "-showversion")
  				{
  					showVersion = true;
  				}
  				else if(arg.StartsWith("-D"))
  				{
  				    arg = arg.Substring(2);
                      string[] keyvalue = arg.Split('=');
  				    string value;
  					if(keyvalue.Length == 2) // -Dabc=x
  					{
                          value = keyvalue[1];
  					} 
                      else if (keyvalue.Length == 1) // -Dabc
                      {
                          value = "";
                      } 
                      else // -Dabc=x=y
  					{
                          value = arg.Substring(keyvalue[0].Length + 1);
  					}
                      props[keyvalue[0]] = value;
  				}
  				else if(arg == "-ea" || arg == "-enableassertions")
  				{
  					IKVM.Runtime.Assertions.EnableAssertions();
  				}
  				else if(arg == "-da" || arg == "-disableassertions")
  				{
  					IKVM.Runtime.Assertions.DisableAssertions();
  				}
  				else if(arg == "-esa" || arg == "-enablesystemassertions")
  				{
  					IKVM.Runtime.Assertions.EnableSystemAssertions();
  				}
  				else if(arg == "-dsa" || arg == "-disablesystemassertions")
  				{
  					IKVM.Runtime.Assertions.DisableSystemAssertions();
  				}
  				else if(arg.StartsWith("-ea:") || arg.StartsWith("-enableassertions:"))
  				{
  					IKVM.Runtime.Assertions.EnableAssertions(arg.Substring(arg.IndexOf(':') + 1));
  				}
  				else if(arg.StartsWith("-da:") || arg.StartsWith("-disableassertions:"))
  				{
  					IKVM.Runtime.Assertions.DisableAssertions(arg.Substring(arg.IndexOf(':') + 1));
  				}
  				else if(arg == "-cp" || arg == "-classpath")
  				{
  					props["java.class.path"] = args[++i];
  				}
  				else if(arg.StartsWith("-Xtrace:"))
  				{
  					Tracer.SetTraceLevel(arg.Substring(8));
  				}
  				else if(arg.StartsWith("-Xmethodtrace:"))
  				{
  					Tracer.HandleMethodTrace(arg.Substring(14));
  				}
                  else if(arg == "-Xdebug")
                  {
                      debug = true;
                  }
                  else if (arg == "-Xnoagent")
                  {
                      //ignore it, disable support for oldjdb
                  }
                  else if (arg.StartsWith("-Xrunjdwp") || arg.StartsWith("-agentlib:jdwp"))
                  {
                      debugArg = arg;
                      debug = true;
                  }
                  else if (arg.StartsWith("-Xreference:"))
                  {
                      Startup.addBootClassPathAssemby(Assembly.LoadFrom(arg.Substring(12)));
                  }
                  else if (arg.StartsWith("-Xms")
                      || arg.StartsWith("-Xmx")
                      || arg.StartsWith("-Xss")
                      || arg == "-Xmixed"
  
gt:                     || arg == "-Xint"