package nxm.sys.test;

import nxm.sys.lib.BaseFile;
import nxm.sys.lib.Privileges;
import nxm.sys.lib.Shell;

/** A class to show basic usage (example) of BaseFile from a standalone Java program.
    This program displays the header info (full path, size (in bytes)) and the first ten bytes in
    a file (text or binary) using the HEX dump capability of the BaseFile.listElements(..) method.
    <br>
    Since NeXtMidas 2.9.0, if NMROOT is not defined via -DNMROOT=&lt;path_to_nmroot&gt; it will be
    automatically detected based on where nxm.sys.lib.Shell is loaded from (which could be
    from &lt;path_to_nmroot&gt; or from &lt;path_to&gt;nxm-sys.jar&gt;.
    <pre>Usage:
      java -cp &lt;path_to_nmroot or nxm-sys.jar&gt; nxm.sys.test.TestBaseFile &lt;filename&gt; [# bytes to display; DEF=10]

    Example with $NMROOT environment defined:
      java -cp $NMROOT nxm.sys.test.TestBaseFile $NMROOT/nxm/sys/dat/test1000.prm

    Example with NeXtMidas's SYS jar (NMROOT will be detected inside the nxm-sys.jar file):
      java -cp $NMROOT/jars/nxm-sys.jar nxm.sys.test.TestBaseFile $NMROOT/nxm/sys/dat/test1000.prm
    </pre>
   @author Neon Ngo
   @since NeXtMidas 2.8.2
*/
public class TestBaseFile {

  /** This constructor was automatically provided by Java,
   *  but it is unneeded as all the methods are static methods.
   *  @deprecated now that Javadocs are required, it is temporarily
   *              being explicitely provided but deprecated
   */
  @Deprecated
  public TestBaseFile () {}

  /** main method to run stand alone.
      @param args command line arguments
   */
  public static void main (String[] args) {
    String filename;
    if (args.length < 1) {
      System.out.println("Usage: java -cp=$NMROOT "+TestBaseFile.class.getName()+" <filename> [num bytes to display; DEF=10]");
      return;
    } else {
      filename = args[0];
      System.out.println("--- Test input filename = '" + filename+"'");
    }

    // Set this privilege FIRST, if you need the following ENVs: HOME, USER, OSNAME; AUX.HOME, AUX.CWD
    Shell.setPriv(Privileges.PRIV_QUERYOS);

    BaseFile bf = new BaseFile(null, filename);           // create BaseFile instance
    bf.open();                                            // open file (before doing any with it)

    System.out.println("--- File size = "+bf.getSize());  // get file size

    System.out.println("--- Headers ---");
    System.out.println(bf.listHeader());                  // list file headers

    int numElements = 10;
    if (args.length > 1) {
      numElements = Integer.parseInt(args[1]);
    }
    System.out.println("--- Data elements (first "+numElements+") ---");
    System.out.println(bf.listElements(0, numElements, null, BaseFile.RADIX_10)); // list first <numElements> in file

    bf.close();                                           // close (cleanup)
  }
}
