package com.sun.solarisinstall.nodes;

import java.io.*;
import java.util.*;
import com.sun.wizards.core.*;
import com.sun.wizards.nodes.*;
import com.sun.solarisinstall.panels.*;

/**
 * SetupNode determines how many OS's/media kits exist and sets the 
 * appropriate skip flags
 */
public class SetupNode extends WizardComposite
{
  /**
   * Creates an SetupNode with a route to the root of
   * the server object tree.
   */
  public SetupNode()
  {
  }

  /**
   * Creates an SetupNode with the specified name and WizardState.
   *
   * @param	wizardState	The buildtime WizardState.
   * @param	name		The name for this SetupNode.
   */
  public SetupNode(WizardState wizardState, String name)
  {
    super(wizardState, name);
  }
  
  /**
   * Creates an SetupNode with the specified name, the specified
   * route and wizard manager.
   *
   * @param name	       The name for this SetupNode.
   * @param route	       The route to the appropriate server side object.
   * @param wizardManager      The wizardManager responsible for this component.
   */
  public SetupNode(String name, Route route, WizardTreeManager wizardManager)
  {
    super(name, route, wizardManager);
  }

  /**
   * This method is called when the SetupNode is displayed.
   */
  public void beginDisplay()
  {
    super.beginDisplay();

    WizardTreeManager wizardManager = getTreeManager();

    Route objectRoute = getRoute().getChildServerRoute("profileObject");
    String selectedOS = null;
    String selectedMediaKit = null;

    Boolean cdrootExists =
        (Boolean)wizardManager.callServerObjectMethod(objectRoute.copy(),
        "doesFileExist",
        new String[] {"java.lang.String.class"},
        new Object[] { "/.cdroot" });

    if (! cdrootExists.booleanValue()) {
      /*
       * Its a net install.  Set flag to Skip the Solaris OS CD mount. 
       */
      wizardManager.callServerObjectMethod(
          getRoute(),
          "setData",
          new String[] {"com.sun.wizards.core.Route.class",
                        "java.lang.String.class",
                        "java.lang.Object.class"},
          new Object[] {null, ProfileConstants.skipCD, Boolean.TRUE });

      /*
       * Save Solaris OS net mount path
       */

       String netMnt = null;

       try {
         BufferedReader in = new BufferedReader(new InputStreamReader(
              new BufferedInputStream(new FileInputStream("/.netmnt"))));
                String line = in.readLine();
                in.close();
                netMnt = line.trim();
       } catch (IOException ex) {
          System.err.println("SetupNode: IOException reading /.netmnt");
       }


       wizardManager.callServerObjectMethod(
         	getRoute(),
         	"setData",
         	new String[] {"com.sun.wizards.core.Route.class",
                        "java.lang.String.class",
                        "java.lang.Object.class"},
         	new Object[] {null, ProfileConstants.NetMnt, netMnt });

    }

    String finddevPath = ProfileConstants.findDeviceOut;
    File findDevFile = new File(finddevPath);

    if (cdrootExists.booleanValue() && !findDevFile.exists()) {
        Object[] findDeviceArg = { finddevPath };
        System.out.println("File does not exist: "+findDeviceArg);
        //what do we do here?
        System.exit(98);
    }

    /*
     * Save CDrom device information
     */

    String device =
        (String)wizardManager.callServerObjectMethod(objectRoute.copy(),
        "readInText",
        new String[] {"java.lang.String.class"},
        new Object[] { finddevPath });
    if (Def.DEBUG) System.out.println("\nSetupNode cdrom device: " + device);

    wizardManager.callServerObjectMethod(
         getRoute(),
         "setData",
         new String[] {"com.sun.wizards.core.Route.class",
                        "java.lang.String.class",
                        "java.lang.Object.class"},
         new Object[] {null, ProfileConstants.CDdevice, "/dev/dsk/"+device });

    wizardManager.callServerObjectMethod(
         getRoute(),
         "setData",
         new String[] {"com.sun.wizards.core.Route.class",
                        "java.lang.String.class",
                        "java.lang.Object.class"},
         new Object[] {null, ProfileConstants.CDdeviceRaw,"/dev/rdsk/"+device});

    /*
     * get list of all os's present per os.toc
     */
    Vector osNames =
        (Vector)wizardManager.callServerObjectMethod(objectRoute.copy(),
        "getOSList",
        null, null);

    if (osNames.size() == 0 ) {
      System.out.println("No Solaris OS information found. Exiting...");
      System.exit(1);
    }

    /* Pick first os as "selected" os. If there is more than one
     * os, user may choose different one.
     */
    selectedOS = (String) osNames.elementAt(0);
    wizardManager.callServerObjectMethod(
          getRoute(),
          "setData",
          new String[] {"com.sun.wizards.core.Route.class",
                        "java.lang.String.class",
                        "java.lang.Object.class"},
          new Object[] {null, ProfileConstants.osName, selectedOS});

    /*
     * get list of all mk's present per media_kits.toc
     */
    Vector mediaKitNames =
        (Vector)wizardManager.callServerObjectMethod(objectRoute.copy(),
        "getMediaKitList",
        null, null);

    int mediaSize = mediaKitNames.size();
    if (mediaSize == 0 ) {
      if (Def.DEBUG) System.out.println("MediaKitPanel: No Media Kits");
      wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                          "java.lang.String.class",
                          "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.noMedia, Boolean.TRUE});
    } else {
      selectedMediaKit = (String) mediaKitNames.elementAt(0);
    }

    if (selectedMediaKit != null) {
        wizardManager.callServerObjectMethod(
        getRoute(),
        "setData",
        new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
        new Object[] {null, ProfileConstants.mediaName, selectedMediaKit});
    }

    /*
     * Determine what panels to display (and what text to use 
     * on SummaryMediaOSPanel
     */
    if ( (mediaSize < 2) && (osNames.size() < 2) ){
        wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.skipMedia, Boolean.TRUE});
        wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.skipOS, Boolean.TRUE});
        wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.skipMediaOS, Boolean.TRUE});
    } else if ( mediaSize < 2 ) {
        /*
         * display os panels only, skip mk
         */
        wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.skipMedia, Boolean.TRUE});
 
    } else if ( osNames.size() < 2 ) {
        /*
         * display media kit panels only, skip os
         */
        wizardManager.callServerObjectMethod(
            getRoute(),
            "setData",
            new String[] {"com.sun.wizards.core.Route.class",
                      "java.lang.String.class",
                      "java.lang.Object.class"},
            new Object[] {null, ProfileConstants.skipOS, Boolean.TRUE});
    }

    /*
     * don't display anything, on to next panel
     */
    if (wizardManager.hasGUI())
    {
       wizardManager.nextButtonPressed();
    }
  }

  /**
   * Get the runtime classes required by this panel.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.nodes.SetupNode"});
  }
}

