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.*;



/** MountCDTask: A task to mount a CD 
*
* @version @(#)MountCDTask.java	1.4 99/09/19 14:37:53  */

public class MountCDTask extends Task implements Serializable
{
  protected static boolean usingVold = false;
  protected int result = 0;
  protected String CDmnt = null;
  protected String CDdev = null;
  protected String CDdeviceRaw = null;
  protected String netMnt = null;

  /*
   * This happens at build time. 
   */
  public MountCDTask()
  {
  }

  /** 
   *  Mount the CD. To validate the CD, extend MountCDTask and
   *  in the perform method do a super.perform(), then code to
   *  validate. The mount status can be used to determine
   *  the actual result of the mount command.
   */
  public void perform()
  {
    WizardState state = getSequence().getState();

    /*
     * Initialize mount status to false
     */
    state.setData(ProfileConstants.CDmountOK, Boolean.FALSE);

    Boolean vold = (Boolean) state.getData(ProfileConstants.usingVold);
    CDmnt = ProfileConstants.CDmount; // set to /cdrom

    if ((vold != null) && vold.booleanValue()) usingVold = true;

    String output = null;
    String cmd = null;

    if (usingVold) {
      /*
       * vold mounts on /cdrom 
       */
      CDmnt = "/cdrom"; 

      /*
       * Wait a few seconds to allow vold to see CD 
       */
      try {
        Thread.sleep(5000);
      } catch (InterruptedException ex) {
        System.out.println("MountCDTask: mount: sleep: "+ex);
      }

      /*
       * Do ls to see if cdrom is mounted 
       */
      cmd = "/usr/bin/ls /cdrom";
      if (Def.DEBUG) System.out.println ("==>MountCDTask cmd: " + cmd ); 
      int maxAttempts = 4;
      int i = 0;

      for (i=0; i < maxAttempts; i++) {
        RunCmd r = new RunCmd(cmd, true);
        result = r.getResult();
        output =  r.getAllOutput();
        if (Def.DEBUG) System.out.println("MountCDTask: output =" + output);
        if ( r.getOutput().compareTo("") != 0 ) break;
        /*
         * Sleep for a while
         */
        try {
          if (Def.DEBUG)System.out.println("MountCDTask: mount sleep 2 secs");
          Thread.sleep(2000);
        } catch (InterruptedException ex) {
          System.out.println("MountCDTask: mount: sleep: "+ex);
        }
      }
      if ( i < maxAttempts ) {
        result = 0;
      } else {
        result = 99;
      }
    } else {
      /*
       * not using vold, prepare to mount cdrom
       */
      File CDmntFile = new File (CDmnt);
      if (CDmntFile.exists()) {
        if ( CDmntFile.isFile()) {
          System.out.println("MountCDTask: " + CDmnt +
                         " is a file. Unable to mount CD.");
          return;
        }
        String umntCmd = "/sbin/umount " + CDmnt;
        result=0;
        RunCmd r = new RunCmd(umntCmd, true);
        result = r.getResult();
        if (Def.DEBUG) {
          System.out.println("==>MountCDTask:" + umntCmd + " return=" + result);
        }
      } else {
        CDmntFile.mkdir();
      }

      /*
       * Run script to manually mount
       */
      CDdev = (String) state.getData(ProfileConstants.CDdevice);
      CDdeviceRaw= (String) state.getData(ProfileConstants.CDdeviceRaw);

      if ( netMnt == null ) {
         /*
          * Command to mount a physical CD 
          */
         cmd = "/sbin/mount -F hsfs -m -o ro " + CDdev + " " + CDmnt;
      }
      else {
         /* 
          * Command to mount from a network image 
          */
         cmd = "/sbin/mount " + netMnt + " " + CDmnt;
      }

      if (Def.DEBUG) System.out.println ("==>MountCDTask cmd: " + cmd );
      output = null;
      result = 0;
      int maxAttempts = 1;
      int i = 0;

      for (i=0; i < maxAttempts; i++) {
        RunCmd r = new RunCmd(cmd, true);
        result = r.getResult();
        output =  r.getAllOutput();
        if (Def.DEBUG) System.out.println("MountCDTask: output =" + output);
        if (result == 0) break;

          /*
           * Sleep for a while
           */
        try {
          Thread.sleep(5000);
        } catch (InterruptedException ex) {
          System.out.println("MountCDTask: mount: sleep: "+ex);
        }
      }

    }

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

  }


  public static void UmountEject(String CDmntpt, String CDraw)
  {
    if (usingVold) {
      AutoEjectCD();
    } else {
      UnmountCD(CDmntpt);
      EjectCD(CDraw);
    }
  }


  /*
   * unmounts cdrom, assumes no vold 
   */
  public static void UnmountCD(String CDmntpt)
  {
    /*
     * Unmount the CD
     */
    String cmd = "/usr/sbin/umount " + CDmntpt;
    if (Def.DEBUG) System.out.println ("\nMountCDTask UnmountCD cmd: " + cmd );
    int result=0;
    RunCmd r = new RunCmd(cmd, true);
    result = r.getResult();
    String output =  r.getAllOutput();
    if (Def.DEBUG) {
      System.out.println("MountCDTask: Unmount output =" + output);
      System.out.println("MountCDTask: Unmount Return code =" + result);
    }
  }


  /*
   * ejects cdrom, assumes vold is running
   */
  public static void AutoEjectCD()
  {
    /*
     * Eject the CD
     */
    String cmd = "/usr/bin/eject cdrom" ;
    if (Def.DEBUG) System.out.println ("\n==>MountCDTask EjectCD cmd: " + cmd );
    int result=0;
    RunCmd r = new RunCmd(cmd, true);
    result = r.getResult();
    String output =  r.getAllOutput();
    if (Def.DEBUG) {
      System.out.println("MountCDTask: Eject output =" + output);
      System.out.println("MountCDTask: Eject Return code =" + result);
    }

  }


  /*
   * ejects cdrom, assumes no vold 
   */
  public static void EjectCD(String CDraw)
  {
    if (usingVold) {
      AutoEjectCD();
    } else {

      /*
       * Eject the CD
       */
      String cmd = "/usr/bin/eject " + CDraw;
      if (Def.DEBUG) System.out.println("\n==>MountCDTask EjectCD cmd: " + cmd);
      int result=0;
      RunCmd r = new RunCmd(cmd, true);
      result = r.getResult();
      String output =  r.getAllOutput();
      if (Def.DEBUG) {
        System.out.println("MountCDTask: Eject output =" + output);
        System.out.println("MountCDTask: Eject Return code =" + result);
      }
    }
  }


  public static void setVold(boolean usingvold)
  {
    if (usingvold) usingVold = true;     
  }


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



}
