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

import com.sun.wizards.builder.*;
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.solarisinstall.*;
import com.sun.solarisinstall.panels.*;
import com.sun.solarisinstall.nodes.*;
import com.sun.solarisinstall.tasks.*;

import com.sun.launcher.core.*;

/*
 * This is the top level Solaris Web Start installer program.
 *
 * The Java code below creates a sequence of Web Start screens that will 
 * allow the user to install Solaris as well as any specified patches, 
 * scripts, or pkgs.
 */

public class SolarisInstallBuilder extends ArchiveWriter
{
  private static String stateName = "Solaris";
  private static String appName = "Solaris";

  public SolarisInstallBuilder()
  {
    /*
     * Change the name of the output file to "solaris.class"
     */
    setArchiveName("solaris");
  }

  protected void setResources()
  {
    super.setResources();
    addTypePath("Images", ".");

    addResource("Images", "header");
    addResource("Images", "welcome");
    addResource("Images", "DiskIcon");
    addResource("Images", "CheckedIcon");
    addResource("Images", "UncheckedIcon");
    addResource("Images", "PartialCheckedIcon");
    addResource("Images", "collapsedArrow");
    addResource("Images", "expandArrow");
    addResource("Images", "com.sun.install.install");
  }

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

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

    String productName = null;

    wizardState.setPurpose("install");

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

    /*
     * Add the sequence to save the selected system locale
     */
    Sequence sysLocaleSequence =
          wizardState.getSequence("systemLocaleSequence");
    if (sysLocaleSequence == null)
      sysLocaleSequence = new Sequence();

    sysLocaleSequence.addTask(new SystemLocaleTask());
    sysLocaleSequence.addTask(new SaveLaunchLocaleTask());
    wizardState.addSequence("systemLocaleSequence", sysLocaleSequence);

    /*
     *  Add Reboot Task
     */
    RebootTask rebootTask = new RebootTask();

    /*
     * Add the reboot sequence.
     */
    Sequence rebootSequence = new Sequence();
        rebootSequence.addTask(rebootTask);

    /*
     * Save the task in the wizard state.
     */
    wizardState.addSequence(InstallConstants.rebootSequence,
        rebootSequence);

    /*
     * Add the sequence to run the preinstall script
     */
    Sequence runpreinstallSequence =
          wizardState.getSequence("preinstallSequence");
    if (runpreinstallSequence == null)
      runpreinstallSequence = new Sequence();

    runpreinstallSequence.addTask(new PreInstallTask());
    wizardState.addSequence("preinstallSequence", runpreinstallSequence);

    /*
     * Add a sequence to mount any CD
     */
    Sequence CDSequence =
          wizardState.getSequence("mountCDSequence");
    if (CDSequence == null)
      CDSequence = new Sequence();

    CDSequence.addTask(new MountCDTask());
    wizardState.addSequence("mountCDSequence", CDSequence);

    /*
     * Add a sequence to mount the Solaris OS CD
     */
    Sequence SolarisCDSequence =
          wizardState.getSequence("mountSolarisCDSequence");
    if (SolarisCDSequence == null)
      SolarisCDSequence = new Sequence();

    SolarisCDSequence.addTask(new MountSolarisCDTask());
    wizardState.addSequence("mountSolarisCDSequence", SolarisCDSequence);

    /*
     * Add a sequence to mount the Solaris OS Net image 
     */
    Sequence NetSequence =
          wizardState.getSequence("mountNetSequence");
    if (NetSequence == null)
      NetSequence = new Sequence();

    NetSequence.addTask(new MountSolarisNetTask());
    wizardState.addSequence("mountNetSequence", NetSequence);

    /*
     * Add a sequence to Scan for Web Start Readay wizards
     */
    Sequence wsrSequence =
          wizardState.getSequence(ProfileConstants.wsrDataExtract);
    if (wsrSequence == null)
      wsrSequence = new Sequence();

    wsrSequence.addTask(new WSRDataExtractTask());
    wizardState.addSequence(ProfileConstants.wsrDataExtract, wsrSequence);


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

    /*
     * Add the images.
     */
    setImage("com.sun.install.install");
    welcomePanel.addImage("welcome", 35 );

    WizardComposite node = null;

    root.addChild(node=new SetupNode(wizardState, "OS Media setup..."));

    root.addChild(node=new SkipNode(wizardState, "solarisinit","initComplete"));
    WizardComposite node2 = null;

    InitPanel initpanel = null;

    node.addChild(node2 = 
             new SkipNode(wizardState, "mkinit", ProfileConstants.skipMedia));
    node2.addChild(new MediaKitPanel(wizardState, "Select Media Kit..."));
    node.addChild(node2 = 
                new SkipNode(wizardState, "osinit", ProfileConstants.skipOS));
    node2.addChild(new SunOSPanel(wizardState, "Select OS..."));
    node.addChild(node2 = 
         new SkipNode(wizardState, "mkosinit", ProfileConstants.skipMediaOS));
    node2.addChild(new SummaryMediaOSPanel(wizardState, "Combined Summary..."));
    node.addChild(node2 = 
                new SkipNode(wizardState, "cdboot", ProfileConstants.skipCD));
    node2.addChild(new InsertSolarisCDPanel(wizardState, "Insert Solaris CD..."));
    node2.addChild(new LoadingSolarisCDPanel(wizardState, "Loading Solaris CD..."));
    node.addChild(initpanel = new InitPanel(wizardState, "Initializing..."));

    /*
     *  assume initial install by default
     */ 
    wizardState.setData("InitialInstall", new Boolean(true));
    wizardState.setData("UpgradeInstall", new Boolean(false));
    wizardState.setData("oneroot", new Boolean(true));

    /*
     * Upgrade or initial install. Skip this node if system is not upgradeable
     */
    root.addChild(node = new SkipNode(wizardState, "upgrade",
                                         "SolarisInstallNotUpgradeable"));
    UpgradeInstallTypePanel solarisInstall = null;
    node.addChild(solarisInstall = new UpgradeInstallTypePanel(wizardState, 
				      "Upgrade or Initial Install",
				      "UpgradeInstall", "InitialInstall"));
    WizardComposite node1 = null;
    SelectRootPanel selRoot = null;
    node.addChild(node1 = new SkipNode(wizardState, "oneroot", "oneroot"));
    node1.addChild(selRoot = new SelectRootPanel(wizardState, 
						      "Select Version to Upgrade"));
     /*
     * Backup media (for upgrade install) selection panel
     */
    wizardState.setData("DefaultBackupMedia" + "Type", "local_tape");
    wizardState.setData("DefaultBackupMedia" + "Path", "/dev/rmt/0");
    node.addChild(node1 = new SkipNode(wizardState, "initial",
				       "InitialInstall"));
    SolarisBackupMediaPanel backupPanel;
    node1.addChild(backupPanel = new SolarisBackupMediaPanel(wizardState, "Set Backup Media"));
  
    
    WizardComposite pfnode = null;
    /*
     * Pfinstall dry run node
     */
    node.addChild(pfnode = new SkipNode(wizardState, "pfinstalldry", "InitialInstall"));
    pfnode.addChild(new PfinstallTestNode(wizardState,"Verify Upgrade Layout"));
    wizardState.setData("reinit", new Boolean(true));
    node1 = null;
    ReinitPanel reinitPanel = null;
    node.addChild(node1 = new SkipNode(wizardState, "reinit" , "reinit")); 
    node1.addChild(reinitPanel = new ReinitPanel(wizardState, "Updating..."));
    /*
     * Create an install sequence consisting of a product task.
     */
    Sequence installSequence = new Sequence();


    ProfileTask profileTask = new ProfileTask(wizardState);
    installSequence.addTask(profileTask);

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

    wizardState.addSequence("wizardStateInitialization", init);

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

    productName = appName;

    wizardState.setName(productName + "_install");

    CustomInstallTypePanel custominstallType = null;

    root.addChild(custominstallType = 
	new CustomInstallTypePanel(wizardState, "Select Installation Type"));

    custominstallType.setCustomDescription(
      new Msg("com.sun.solarisinstall.SolarisInstall", 
               SolarisInstallResources.MSG_CUSTOM_INSTALL_BLURB,
        new Object[] { appName }));

    /*
     * Skip this node if this is a default install
     */
    WizardComposite customNode = null;
    WizardComposite prodSelNode = null;

    root.addChild(customNode = new SkipNode(wizardState, "custom",
        InstallConstants.standardInstall));

    /*
     *  Custom install
     *  Co-packaged software selection
     */ 

    WizardComposite locNode = null;
    customNode.addChild(
	locNode = new SkipNode(wizardState,"skipLocale","noLocales"));

    /*
     * Locale Panels
     */

    locNode.addChild(new SoftwareLocalePanel(wizardState, "Select Locales"));
    customNode.addChild(
	new SystemLocalePanel(wizardState, "Select System Locale"));

    /*
     * Products Loop
     */

    LoopNode productsLoop = new LoopNode(wizardState, 
				    "AddProductsNode", 
				    "AddProductsComplete", 
				    "AddProductsCount");  

    customNode.addChild(productsLoop);

    productsLoop.addChild(
	prodSelNode = new SkipNode(wizardState,"skipProd","noMedia"));

    prodSelNode.addChild(new ProductSelectionPanel(wizardState, 
                                           "Select Co-Packaged Software"));

    productsLoop.addChild(
	new AddProductsPanel(wizardState, "AddProductsPanel"));

    SkipNode additionalProducts = 
	new SkipNode(wizardState, "AddingProducts", "AddProductsComplete");

    productsLoop.addChild(additionalProducts);

    /*
     * Add Products From CD
     */

    additionalProducts.addChild(
	node1 = new SkipNode(wizardState, "FromCDNode", 
			     "AddProductsFromCD", true));

    node1.addChild( new AddProdCDPanel( wizardState, "Add Product CD"));
    node1.addChild( new ScanCDPanel(wizardState, "Scanning CD"));

    /*
     * Add Products From a Kiosk Download
     */

    additionalProducts.addChild(
	 node1 = new SkipNode(wizardState, "FromKioskNode", 
			 "AddProductsFromKiosk", true));

    node1.addChild( new ScanKioskPanel(wizardState, "Kiosk Source"));

    /*
     * Add Products From a filesystem
     */

    additionalProducts.addChild(
    	node1 = new SkipNode(wizardState, "FromFileNode", 
			 "AddProductsFromFile", true));

    node1.addChild( new ScanDirPanel(wizardState, "File Source"));

    /*
     * After Add Products Loop
     * If needed, Remount the Solaris OS CD
     */ 

    customNode.addChild( node1 = new SkipNode(wizardState, "remount", 
			ProfileConstants.RemountSolarisCD, true));
    node1.addChild(
	new InsertSolarisCDPanel(wizardState, "Insert Solaris CD"));
    node1.addChild(
	new LoadingSolarisCDPanel(wizardState, "Loading Solaris CD"));

    /*
     * Skip this node if its an upgrade. 
     *
     * Meta Cluster, Disk Selection, and File System layouts
     */

    customNode.addChild(
	node1 = new SkipNode(wizardState, "upgrade3", "UpgradeInstall"));

    WizardComposite testNode = null;
    node1.addChild(testNode = new SkipNode(wizardState, 
					   "skip64", ProfileConstants.no64));
    testNode.addChild(new SixtyFourBitSelectionPanel(wizardState, "64-Bit", 
					ProfileConstants.SixtyFourBit));
    node1.addChild(
	new SolarisClusterPanel(wizardState, "Solaris Configuration"));

    node1.addChild(
	new DiskSelectionPanel(wizardState, "Lay Out File Systems"));

    node1.addChild(
	testNode = new SkipNode(wizardState, "skipTest", "noTest"));
    testNode.addChild(new TestProfilePanel(wizardState, "Gathering"));

    node1.addChild(
	new FileSysSizePanel(wizardState, "Lay Out File Systems"));

    /*
     * Traverse this node if this is an upgrade install
     */
    root.addChild(node = new SkipNode(wizardState, "initial",
        "InitialInstall"));

    wizardState.setData("NoSystemBackup", new Boolean(false));

    /*
     * Pfinstall dry run node
     */
    node.addChild(new PfinstallTestNode(wizardState, "Verify Upgrade Layout"));

    /*
     * Skip this node if this is a custom install
     */
    WizardComposite defaultNode = null;

    root.addChild(defaultNode = new SkipNode(wizardState, "default",
        InstallConstants.standardInstall, true));

    /*
     * Skip this node if its an upgrade. 
     */

    defaultNode.addChild(
	node1 = new SkipNode(wizardState, "default2", "UpgradeInstall"));

    node1.addChild(
	testNode = new SkipNode(wizardState, "skipTest", "noTest"));
    testNode.addChild(new TestProfilePanel(wizardState, "Gathering"));

    /*
     * Verify Panel
     */
    root.addChild(new MyVerifyPanel(wizardState, "Verify"));

    /*
     * Add the progress wizard as a child wizard
     */

    root.addChild(new HierarchyNode(wizardState, "preinstall"));
    root.addChild(new HierarchyNode(wizardState, "install"));

    Vector selectedChildren = new Vector();
    selectedChildren.addElement("./progress.class");
    wizardState.addChild("./progress.class");

    /***
     * Comment out the three code lines following this comment field if 
     * additional software packages, patches, or scripts are not to be added 
     * to the Installer CD and referenced in its os install meta_cluster 
     * tables.
     *
     * The "postinstall.class" runs after the main Solaris OS pfinstall
     * It looks at the /webstart/os/meta_clusters  install tables for 
     * PACKAGES, PATCHES, and SCRIPTS key value pairs.  If it finds  
     * any, it will install the key referenced packages, patches, and run
     * the key referenced scripts.
     ***/ 
     
    root.addChild(new HierarchyNode(wizardState, "postinstall"));
    selectedChildren.addElement("./postinstall.class");
    wizardState.addChild("./postinstall.class");

    wizardState.setSelectedChildren(selectedChildren);
    wizardState.setData("install.logLength", new Integer(1000).toString());
   
    

    /*
     * Using subclassed summary panel
     */
    root.addChild(new SolarisSummaryPanel(wizardState, "Summary"));

   /*
    * Add the server objects
    */
    wizardState.addChildObject("profileObject", new ProfileServerObject());
    wizardState.addChildObject("launchObject", new LaunchServerObject());

  }

  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);
      SolarisInstallBuilder cdBuilder = new SolarisInstallBuilder();
      cdBuilder.writeArchive();
      System.exit(0);
    }
}
