package com.sun.solarisinstall.tasks;

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

/**
 * This task saves the /a/etc/default/init file and
 * copies the current /etc/default/init file to
 * /a/etc/default/init for the launcher environement.
 */
public class SaveLaunchLocaleTask extends Task
   implements Serializable
{

  /**
   * Creates a SaveLaunchLocaleTask
   */
  public SaveLaunchLocaleTask()
  {
  }

  public void perform()
  {
    if (Def.DEBUG) System.out.println("in SaveLaunchLocaleTask");
    WizardState state = getWizardState();
    String dispatch = ProfileConstants.dispatchTable;
    File dispatchTable = new File(dispatch);

    if (dispatchTable.exists()) {
      /*
       *  Only need to save file if launcher will run
       */
      Vector selectedLocales = 
           (Vector)state.getData(InstallConstants.selectedLocales);
      String localedir = ProfileConstants.miniRootLocDir;
      /*
       * If miniroot locale wasn't selected, then run launcher in 
       * selected system locale instead (i.e., don't do anything)
       */
      int selLocaleSize = 0;
      if (selectedLocales != null) selLocaleSize = selectedLocales.size();
      if (selLocaleSize != 0) {
        Locale minirootLocale = 
                       (Locale)state.getData(InstallConstants.systemLocale);
        if (minirootLocale != null) {
          String minirootLoc = SoftwareLocalePanel.localeName(minirootLocale);

          if (Def.DEBUG)
             System.out.println("SystemLocaleTask minirootLocale="+minirootLoc);

          /*
           * if miniroot locale was installed, the locale dir and locale_map
           * should now be installed in /a/usr/lib/locale
           */
          File LocMap = new File(localedir + File.separator + minirootLoc
                                     + File.separator + "locale_map");
          if (LocMap.exists()) {
            if (Def.DEBUG) {
              System.out.println("SystemLocaleTask: LocMap EXISTS: " 
                                                    + LocMap.getPath() );
              System.out.println("SystemLocaleTask: /etc/default/init.save");
            }
            String cmd = 
                 "/usr/bin/cp /a/etc/default/init /a/etc/default/init.save";
            if (Def.DEBUG)
                 System.out.println("==>SystemLocaleTask cmd: " + cmd );
            RunCmd r = new RunCmd(cmd, false);
            String output =  r.getAllOutput();
            if (Def.DEBUG) 
                System.out.println("SystemLocaleTask: output =" + output);
        
            cmd = "/usr/bin/cp /etc/default/init /a/etc/default/init";
            r = new RunCmd(cmd, false);
            output =  r.getAllOutput();
            if (Def.DEBUG) 
                System.out.println("SystemLocaleTask: output =" + output);
          } else {
            if (Def.DEBUG)
              System.out.println("SystemLocaleTask: LocMap doesn't exist: " 
                                   + LocMap.getPath() );

          }
        } else {
          if (Def.DEBUG)
                System.out.println("SystemLocaleTask: no minirootLocale");

        }
      } 
    }


  }
      

  /**
   * Add the runtime class requirements to the specified vector.
   * @param resourceVector The vector containing all the required resources
   *      for this wizard.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    super.addRuntimeResources(resourceVector);
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.tasks.SaveLaunchLocaleTask"});
  }
}
