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


import com.sun.wizards.builder.*;
import com.sun.wizards.builder.resolver.*;
import com.sun.wizards.core.*;
import com.sun.wizards.panels.*;
import com.sun.wizards.nodes.*;
import com.sun.wizards.tasks.*;

import com.sun.install.*;
import com.sun.install.tasks.*;
import com.sun.install.panels.*;
import com.sun.install.nodes.*;
import com.sun.install.products.*;
import com.sun.launcher.*;

import com.sun.solarisinstall.solaris2.tasks.*;
import com.sun.solarisinstall.solaris2.panels.*;

/*
 * This is the Solaris2 wizard builder.  The Solaris2 
 * wizard installs Solaris packages that were not installed by
 * pfinstall and are not on the langcd. 
 *
 */

public class Solaris2InstallBuilder extends ArchiveWriter
{
  private static String stateName = "Solaris2";
  private static String appName = "Solaris 8 Software 2";
  private ResourceResolver classResolver = null;

  public Solaris2InstallBuilder()
  {
    /*
     * Change the name of the output file to "solaris2.class"
     */
    //setArchiveName("solaris2");
    setArchiveName("install");
  }

  protected void setResources()
  {
    classResolver = new FileResolver(getWizardRuntimeClasspath()+File.pathSeparator+System.getProperty("java.class.path"));
    super.setResources();
    addTypePath("Images", ".");

    addResource("Images", "header");
    addResource("Images", "welcome");
    addResources("Images", "solarisWizardLogo.gif");

    // as done in InstallArchiveWriter
    ResourceCollection collection = new ResourceCollection();

    /*
     * Add localized class files for install and uninstall
     */
    String path = getWizardRuntimeClasspath()+File.separator+
	"com"+File.separator+
	"sun"+File.separator+
	"install";

    String[] names = getNames(path, new String[] {".class"});
    for (int index=0; index < names.length; index++) {
      names[index] = "com.sun.install."+(names[index]);
    }
    collection.addResources("RuntimeClasses", names, classResolver);

    names = new String[] {"com.sun.wizards.builder.resolver.ResourceResolver"};
    collection.addResources("RuntimeClasses", names, classResolver);

    /*
     * Force product tree classes to be included. As we will be building
     * the product tree at run time, we must kick off the necessary
     * resource collection methods here, otherwise they will not be in
     * the archive.
     */
    path = getWizardRuntimeClasspath()+File.separator+
	"com"+File.separator+
	"sun"+File.separator+
	"install"+File.separator+
	"products";

    names = getNames(path, new String[] {".class"});
    for (int index=0; index < names.length; index++){
      names[index] = "com.sun.install.products."+(names[index]);
    }
    collection.addResources("RuntimeClasses", names, classResolver);

    path = getWizardRuntimeClasspath()+File.separator+
	   "com"+File.separator+
	   "sun"+File.separator+
	   "wizards"+File.separator+
	   "awt";

    names = getNames(path, new String[] {".class"});
    for (int index=0; index < names.length; index++) {
      names[index] = "com.sun.wizards.awt."+(names[index]);
    }
    collection.addResources("RuntimeClasses", names, classResolver);

    path = getWizardRuntimeClasspath()+File.separator+
	   "com"+File.separator+
	   "sun"+File.separator+
	   "solarisinstall"+File.separator+
	   "solaris2"+File.separator+
	   "tasks";

    names = getNames(path, new String[] {".class"});
    for (int index=0; index < names.length; index++)
      names[index] = "com.sun.solarisinstall.solaris2.tasks."+(names[index]);
    collection.addResources("RuntimeClasses", names, classResolver);

    /*
     * Add icons for install and uninstall.
     */
    path = getWizardRuntimeClasspath()+File.separator+
	"com"+File.separator+
	"sun"+File.separator+
	"install";

    names = getNames(path, new String[] {".gif", ".jpeg", ".jpg", ".jfif"});
    for (int index=0; index < names.length; index++)
      names[index] = "com.sun.install."+(names[index]);
    collection.addResources("Images", names, classResolver);
    addCollection(collection);
  }

  protected void createClientTree()
  {
    super.createClientTree();

    WizardState wizardState = getWizardState();
    WizardComposite root = getRoot();

    String productName = null;

    /*
     * be sure to set purpose same as in SolarisBuilder if
     * it is to be used as a parent of this wizard
     */
    wizardState.setPurpose("install");

    /*
     * productName is used by Welcome Panel
     */
    wizardState.setData(InstallConstants.productName, appName);

    /*
     * Create the tree hierarchy
     */
//WelcomePanel welcomePanel = null;
//root.addChild(welcomePanel = new WelcomePanel(wizardState, "Welcome"));

    /*
     * Add the image.
     */
    setImage("com.sun.install.install");

    WizardComposite node = null;


    /*
     * Create an install sequence consisting of a custom task and
     * an empty product task. At this time, the product task will just
     * be a place-holder for the product tree that will be built (by
     * the BuildSol2ProdTree task) at run time.
     */
    Sequence installSequence = new Sequence();

    Sequence init = wizardState.getSequence("wizardStateInitialization");
    if (init == null) init = new Sequence();
    init.addTask(new InstallCommandLineTask());

    wizardState.addSequence("wizardStateInitialization", init);

    /*
     * Task to build the product tree at run time
     */
    BuildSol2ProdTreeTask buildTask = new BuildSol2ProdTreeTask();
    installSequence.addTask(buildTask);
    /*
     * The actual install task
     */
    wizardState.setData(InstallConstants.standardInstall, new Boolean(true));
    ProductTask productTask = new ProductTask();
    installSequence.addTask(productTask);
    /*
     * Stub for the product tree
     */
    Product product = new Product(productTask, wizardState);
    productTask.setProduct(product);
    wizardState.setData(InstallConstants.productTree, product);
    product.setProperty(InstallConstants.stopOnError, new Boolean(true));

    /*
     * Add the install sequence to the state.
     */
    wizardState.addSequence(InstallConstants.installSequence, installSequence);

    productName = appName;

    wizardState.setName(productName + "_install");

    Object[] msgArgs = { productName };
    Msg label = new Msg("com.sun.install.Install", 
			 InstallResources.MSG_INSTALL_FRAME_TITLE, msgArgs);
    wizardState.setProgressLabel("install", label);

    NoStopProgressPanel panel = null;
    root.addChild(node = new WizardComposite(wizardState, "install"));
    node.addChild(panel = new NoStopProgressPanel(wizardState, "Installing...", "install"));
    
    /*
     * The progress panel should cancel forward.
     */
    ImageVisual imageVisual = new ImageVisual("solarisWizardLogo");
    panel.setVisual(imageVisual);
    panel.setCancelDirection(true);

    root.addChild(new SummaryPanel(wizardState, "Summary"));

    /*
     * Info Message.  Only seen if wizard is run standalone.
     */
    Vector msgs = new Vector();
    msgs.addElement(new Msg("com.sun.launcher.Launcher",
                            LauncherResources.MSG_NO_FUNCTION));
    root.addChild(new InfoPanel(wizardState, "Info Panel", msgs));


  }

  /**
   * Returns filename (without paths or extensions)
   * of all files with the specified extensions from the specified path
   */
  public static String[] getNames(String path, String[] exts)
  {
    Vector names = new Vector();
    StringTokenizer st = new StringTokenizer(path, File.pathSeparator);
    while (st.hasMoreElements())
    {
      String name = st.nextToken();
      File file = new File(name);
      if (file.isDirectory())
      {
        String[] list = file.list();
        for(int index=0; index < list.length; index++)
        {
          File nextFile = new File(file, list[index]);
          if (nextFile.isFile())
          {
            for (int extIndex = 0; extIndex < exts.length; extIndex++)
            {
              if (nextFile.getName().endsWith(exts[extIndex]))
              {
                name = nextFile.getName().substring(0, nextFile.getName().lastIndexOf(exts[extIndex]));
                names.addElement(name);
              }
            }
          }
        }
      }
    }
    String[] result = new String[names.size()];
    for (int index=0; index<result.length; index++)
    {
      result[index] = (String)names.elementAt(index);
    }
    return result;
  }

  public static void main(String args[])
    {
      // set wizard positions
      Properties props = System.getProperties();
      props.put("wizard.xpos", "-1");
      props.put("wizard.ypos", "-1");
      System.setProperties(props);
      Solaris2InstallBuilder cdBuilder = new Solaris2InstallBuilder();
      cdBuilder.writeArchive();
      System.exit(0);
    }
}
