package nxm.sys.intr;

import nxm.sys.lib.BaseFile;
import nxm.sys.lib.FileUtil;
import nxm.sys.lib.IOResource;
import nxm.sys.lib.Intrinsic;

/**
  Finds file and returns its full path name.

  @author Earl Ferrara
*/
public class Find extends Intrinsic {

  /** default constructor */
  public Find () {}

  public int process () {

    String fullName=""; // path, name and extension of file (moved inside process in 3.9.4 for NXM-3619)

    // Init the file class
    BaseFile bf = MA.getFile("FILE");

    // Get result labels
    String fnLabel = MA.getU("FULLNAME",null);

    // Do you want the URL or simply a standard file name?
    boolean getUrl = MA.getState("/URL",false);

    // search for the file
    if (bf.find(IOResource.DIR_INPUT)) {
      fullName = bf.getURL();
      if (!getUrl) fullName = FileUtil.getCanonicalPath(fullName);
/* NTN 2008-11-26 ORIG before using FileUtil.getCanonicalPath(..)

// This is a short-term fix for DR 503635
//
if(getUrl && Shell.ostype.equals("DOS")) {
    fullName = fullName.replace('\\', '/');
}
      if (!getUrl) {
        // jph -- should not have to do this, need another way to
        // find a regular file name
        int iColon = fullName.indexOf(":");
        if (iColon > 0) fullName = fullName.substring(iColon+1);
      }
NTN: END of ORIG
*/
    }

    // report results
    if (fnLabel != null) {
      MR.put(fnLabel, fullName);
      verbose = false;
    }
    verbose = MA.getState("/VERBOSE",verbose);
    if (verbose) M.println(fullName);  // since 3.7.1 using M.println instead of M.terminal.writeln to allow logging - NXM-1813
    return FINISH;
  }

}
