package nxm.sys.intr;

import nxm.sys.lib.FileName;
import nxm.sys.lib.FileName.FNCase;
import nxm.sys.lib.Intrinsic;

/** Utility for generating file names in a system-independent manner.

    @author  Jeff Schoen / J. Czechowski IV
 */
public class Fname extends Intrinsic {

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

  @Override
  public int process () {
    if (MA.getState("/PARSE")) {
      String   label = MA.getCS("LABEL");
      String   area  = MA.getU("AREA");
      String   name  = MA.getU("NAME");
      String   ext   = MA.getU("EXT");
      FileName fn    = new FileName(label);
      MR.put(area,fn.getPath());
      MR.put(name,fn.getRoot());
      MR.put(ext,fn.getExt());
    }
    else {
      FNCase   fnCase = MA.getSelection("/FNCASE", FileName.getDefaultFNCase());
      String   label  = MA.getU("LABEL");
      String   opt    = MA.getS("OPT");
      String   area   = MA.getS("AREA");
      String   name   = MA.getCS("NAME");
      String   ext    = MA.getCS("EXT");
      FileName fname  = FileName.getFileName(opt,area,name,ext,fnCase);

      if (label.isEmpty()) {
        M.println("Filename := "+fname);  // since 3.7.1 using M.println instead of M.terminal.writeln to allow logging - NXM-1813
      }
      else {
        MR.put(label, fname.toString());
      }
    }
    return FINISH;
  }
}
