// -------------------------------------------------------------------------------
// Copyright (c)2000 Apache Software Foundation
// -------------------------------------------------------------------------------

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

public class Bootboot {

    static private void runCommand(String[] command) {
        try {
            Runtime runtime = Runtime.getRuntime();
            Process process = runtime.exec(command);
            
            // echo output from process
            
            InputStream in = process.getInputStream();
            byte[] buf = new byte[80];
            int count = 0;
            count = in.read(buf, 0, buf.length);
            while (count != -1) {
                System.out.write(buf, 0, count);
                count = in.read(buf, 0, buf.length);
            }
            
            in = process.getErrorStream();
            count = in.read(buf, 0, buf.length);          
            if (count > 0) {
                System.out.println();
                System.out.println("Error Stream Output:");
             
                while (count != -1) {
                    System.out.write(buf, 0, count);
                    count = in.read(buf, 0, buf.length);
                }
            }
        } catch (IOException e) {
            System.out.println("OUCHY: " + e);
            return;
        }
            
    }
   
    /**
     * Command line entry point.
     */
    public static void main(String[] args) {
        
        String javaHome = System.getProperty("java.home");
        if (javaHome.endsWith("jre")) {
            javaHome = javaHome.substring(0, javaHome.length() - 4);
        }
        
        System.out.println("Java home is " + javaHome);
        
        String[] compileCommand = new String[] 
            { "javac", "-classpath", javaHome + "/lib/tools.jar", "Bootstrap.java" };

        runCommand(compileCommand);
        
        String[] runBootstrap = new String[] 
            { "java", "-cp", "." + File.pathSeparator + 
                             javaHome + "/lib/tools.jar", "Bootstrap"};

        System.out.println("Classpath = " + runBootstrap[2]);
        
        runCommand(runBootstrap);

    }
}