context: //
  // mkbundle: tool to create bundles.
  //
  // Based on the `make-bundle' Perl script written by Paolo Molaro (lupus@debian.org)
  //
  // Author:
  //   Miguel de Icaza
  //
  // (C) Novell, Inc 2004
  //
  using System;
  using System.Diagnostics;
  using System.Xml;
  using System.Collections.Generic;
  using System.IO;
  using System.IO.Compression;
  using System.Runtime.InteropServices;
  using System.Text;
  using IKVM.Reflection;
  
  
  using System.Threading.Tasks;
  
  class MakeBundle {
  	static string output = "a.out";
  	static string object_out = null;
  	static List<string> link_paths = new List<string> ();
  	static bool autodeps = false;
  	static bool keeptemp = false;
  	static bool compile_only = false;
  	static bool static_link = false;
  	static string config_file = null;
  	static string machine_config_file = null;
  	static string config_dir = null;
  	static string style = "linux";
  	static bool compress;
  	static bool nomain;
  	static bool? use_dos2unix = null;
  	static bool skip_scan;
  	static string ctor_func;
  	
  	static int Main (string [] args)
  	{
  		List<string> sources = new List<string> ();
  		int top = args.Length;
  		link_paths.Add (".");
  
  		DetectOS ();
  		
  		for (int i = 0; i < top; i++){
  			switch (args [i]){
  			case "--help": case "-h": case "-?":
  				Help ();
  				return 1;
  
  			case "-c":
  				compile_only = true;
  				break;
  				
  			case "-o": 
  				if (i+1 == top){
  					Help (); 
  					return 1;
  				}
  				output = args [++i];
  				break;
  
  			case "-oo":
  				if (i+1 == top){
  					Help (); 
  					return 1;
  				}
  				object_out = args [++i];
  				break;
  
  			case "-L":
  				if (i+1 == top){
  					Help (); 
  					return 1;
  				}
  				link_paths.Add (args [++i]);
  				break;
  
  			case "--nodeps":
  				autodeps = false;
  				break;
  
  			case "--deps":
  				autodeps = true;
  				break;
  
  			case "--keeptemp":
  				keeptemp = true;
  				break;
  			case "--static":
  				static_link = true;
  				Console.WriteLine ("Note that statically linking the LGPL Mono runtime has more licensing restrictions than dynamically linking.");
  				Console.WriteLine ("See http://www.mono-project.com/Licensing for details on licensing.");
  				break;
  			case "--config":
  				if (i+1 == top) {
  					Help ();
  					return 1;
  				}
  
  				config_file = args [++i];
  				break;
  			case "--machine-config":
  				if (i+1 == top) {
  					Help ();
  					return 1;
  				}
  
  				machine_config_file = args [++i];
  
  				Console.WriteLine ("WARNING:\n  Check that the machine.config file you are bundling\n  doesn't contain sensitive information specific to this machine.");
  				break;
  			case "--config-dir":
  				if (i+1 == top) {
  					Help ();
  					return 1;
  				}
  
  				config_dir = args [++i];
  				break;
  			case "-z":
  				compress = true;
  				break;
  			case "--nomain":
  				nomain = true;
  				break;
  			case "--style":
  				if (i+1 == top) {
  					Help ();
  					return 1;
  				}
  				style = args [++i];
  				switch (style) {
  				case "windows":
  				case "mac":
  				case "linux":
  					break;
  				default:
  					Console.Error.WriteLine ("Invalid style '{0}' - only 'windows', 'mac' and 'linux' are supported for --style argument", style);
  					return 1;
  				}
  					
  				break;
  			case "--skip-scan":
  				skip_scan = true;
  				break;
  			case "--static-ctor":
  				if (i+1 == top) {
  					Help ();
  					return 1;
  				}
  				ctor_func = args [++i];
  				break;
  			default:
  				sources.Add (args [i]);
  				break;
  			}
  			
  			if (static_link && style == "windows") {
  				Console.Error.WriteLine ("The option `{0}' is not supported on this platform.", args [i]);
  				return 1;
  			}
  		}
  
  		Console.WriteLine ("Sources: {0} Auto-dependencies: {1}", sources.Count, autodeps);
  		if (sources.Count == 0 || output == null) {
  			Help ();
  			Environment.Exit (1);
  		}
  
  		List<string> assemblies = LoadAssemblies (sources);
  		List<string> files = new List<string> ();
  		foreach (string file in assemblies)
  			if (!QueueAssembly (files, file))
  				return 1;
  			
  		GenerateBundles (files);
  		//GenerateJitWrapper ();
  		
  		return 0;
  	}
  
  	static void WriteSymbol (StreamWriter sw, string name, long size)
  	{
  		switch (style){
  		case "linux":
  			sw.WriteLine (
  				".globl {0}\n" +
  				"\t.section .rodata\n" +
  				"\t.p2align 5\n" +
  				"\t.type {0}, \"object\"\n" +
  				"\t.size {0}, {1}\n" +
  				"{0}:\n",
  				name, size);
  			break;
  		case "osx":
  			sw.WriteLine (
  				"\t.section __TEXT,__text,regular,pure_instructions\n" + 
  				"\t.globl _{0}\n" +
  				"\t.data\n" +
  				"\t.align 4\n" +
  				"_{0}:\n",
  				name, size);
  			break;
  		case "windows":
  			sw.WriteLine (
  				".globl _{0}\n" +
  				"\t.section .rdata,\"dr\"\n" +
  				"\t.align 32\n" +
  				"_{0}:\n",
  				name, size);
  			break;
  		}
  	}
  	
  	static string [] chars = new string [256];
  	
  	static void WriteBuffer (StreamWriter ts, Stream stream, byte[] buffer)
  	{
  		int n;
  		
  		// Preallocate the strings we need.
  		if (chars [0] == null) {
  			for (int i = 0; i < chars.Length; i++)
  				chars [i] = string.Format ("{0}", i.ToString ());
  		}
  
  
gt: 		while ((n = stream.Read (buffer, 0, buffer.Length)) != 0) {