package com.sun.solarisinstall.tasks;

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


/* WSRDataExtractTask
 *
 * Look for Web Start Ready classes in the specified directory.
 *
 * If found, extract their WSR files and store them in the specified directory
 *
 * Set Boolean(true) status if WSR files successfully extracted, 
 *     Boolean(false) otherwise
 */

public class WSRDataExtractTask extends Task implements Serializable
{
  /*
   * This happens at build time. 
   */
  public WSRDataExtractTask()
  {
  }

  public void perform()
  {
    WizardState state = getSequence().getState();
    if (Def.DEBUG) System.out.println ("Performing WSRDataExtractTask");

    /*
     * Initialize found status to false
     * Get source and destination directories 
     */

    state.setData(ProfileConstants.WSRFilesExist, Boolean.FALSE);

    String classDir = (String) state.getData(ProfileConstants.wsrClassDir);
    if (Def.DEBUG) System.out.println ("classDir = " + classDir); 

    String expandDir = (String) state.getData(ProfileConstants.wsrExpandDir);
    if (Def.DEBUG) System.out.println ("expandDir = " + expandDir); 

    /*
     * Validate Source and Destination directories 
     */

    if (classDir == null || expandDir == null) return;

    classDir = classDir.trim();
    expandDir = expandDir.trim();

    File src = new File(classDir);

    if (!src.exists() || !src.isDirectory()) {
       if (Def.DEBUG) System.out.println ("directory " + src + "not found"); 
       return;
    }

    File dest = new File(expandDir);

    if (!dest.exists()) {
      dest.mkdirs();
    }

    /*
     * Look for *.class files
     */

    String[] files = src.list(); 
    Vector classFiles = new Vector();

    for (int i=0; i < files.length; i++) {
	String fileName = files[i].trim();

	if (fileName.endsWith(".class")) { 
	   classFiles.addElement(fileName);  
	}
    }

    if (classFiles.size() == 0) {
       if (Def.DEBUG) System.out.println ("no class files found"); 
       return;
    }

   /*
    * Look for Web Start Ready wizards 
    */

    Vector wsrClassFiles = new Vector();

    for (int i=0; i < classFiles.size(); i++) {
      if (Def.DEBUG) {
	System.out.println("scanning: " + classFiles.elementAt(i));
      }

      String cmd = "/usr/bin/java car -eWSRData " + 
		    classDir + File.separator + classFiles.elementAt(i);

      RunCmd r = new RunCmd(cmd, true);
      int result = r.getResult();
      String output = r.getAllOutput();
      if (Def.DEBUG) {
	System.out.println("car result = " + result);
	System.out.println("car output = " + output);
      }
      if (result == 0) {
        wsrClassFiles.addElement(classFiles.elementAt(i));
      }
    }

    if (wsrClassFiles.size() == 0) {
       if (Def.DEBUG) System.out.println ("no wsr class files found"); 
       return;
    }

    /*
     * Extact the WSR data
     */

    for (int i=0; i < wsrClassFiles.size(); i++) {
      String wsrClassFile = (String) wsrClassFiles.elementAt(i);

      int classIndex = wsrClassFile.lastIndexOf(".class"); 
      String className = wsrClassFile.substring(0,classIndex);

      String cmd = "/usr/bin/java -classpath " + classDir + " " + className + 
		" -get wsr," + expandDir + File.separator + className +
		ProfileConstants.wsrDir;

      if (Def.DEBUG) {
	System.out.println("wsr class: " + wsrClassFile); 
	System.out.println("expanding wsr: " + cmd); 
      }

      RunCmd r = new RunCmd(cmd, true);
      int result = r.getResult();
      String output = r.getAllOutput();
      if (Def.DEBUG) {
	System.out.println("expand result = " + result);
	System.out.println("car output = " + output);
      }

      if (result == 0) {
        state.setData(ProfileConstants.WSRFilesExist, Boolean.TRUE);
      }
    }
  }


  /**
   * Add the runtime class requirements to the specified vector.
   */
  public void addRuntimeResources(Vector resourceVector)
  {
    resourceVector.addElement(new String[] {null, "com.sun.solarisinstall.tasks.WSRDataExtractTask"});
  }



}
