package nxm.sys.intr;

import nxm.sys.lib.Intrinsic;
import nxm.sys.lib.MidasException;
import nxm.sys.lib.Shell;

/**
  Run a NeXtMidas command in a local sub-process (uses ENV.NMLOCAL).

  @author Jeff Schoen
*/
public class Nm extends Intrinsic {
  private static final boolean FORCE_ERROR = true; // Since NeXtMidas 2.1.1 force this to error out.

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

  public int open() {
    if (FORCE_ERROR) {
      throw new MidasException("NM: Can not run NM from inside the shell."); // Since 2.1.1
    }

    String nm = MR.getString("ENV.NMLOCAL");
    if (nm==null) nm = "DEFAULT";
    String ostype = Shell.ostype;
    String command = MA.getRawArgs();

    if (command.equals("")) M.error("Can not run NM from inside the shell without an argument");

         if (nm.equals("INTERNAL")) nm = "";
    else if (nm.equals("DEFAULT"))  nm = "$$nm ";
    else if (ostype.equals("UNIX")) nm = "$"+nm+"os/unix/nmbat ";
    else if (ostype.equals("VMS"))  nm = "$"+nm+"[os.vms]nmbat.com ";
    else if (ostype.equals("DOS"))  nm = "$"+nm+"os\\dos\\nmbat.bat ";
    else nm = "$$nm ";

    // now execute the command in the proper environment
    Shell.runCommand (M,nm+command);

    return NORMAL;
  }
}
