
// This file is put in the Public Domain, originally published
// at <http://p.outlyer.net./javacode/>
// $Id: RhinoRun.java 1365 2008-06-27 19:03:31Z  $

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import javax.script.ScriptEngine;
import javax.script.ScriptEngineManager;
import javax.script.ScriptException;

/**
 * This is a wrapper around the rhino script engine, it will load a file
 * and try to eval() its contents.
 *
 * Usage: RhinoRun [inputfile.js] [inputfile2.js]
 */
public class RhinoRun {
    public static void main(String...args) {
        if (args.length == 0) {
            System.err.printf("Usage: java [...] %s [file1.js [file2.js [...]]]\n",
                              RhinoRun.class.getName());
            System.exit(1);
        }

        final ScriptEngine rhino = new ScriptEngineManager().getEngineByName("rhino");

        for (final String path : args) {
            try {
                final File file = new File(path);
                if (!file.isFile() || !file.canRead()) {
                    throw new FileNotFoundException("Failed to access " + path);
                }
                rhino.eval(new BufferedReader(new FileReader(file)));
            }
            catch (final FileNotFoundException e) {
                System.err.println("Error: " + e.getMessage());
                if (System.getProperty("debug", "0").equals("1")) {
                    e.printStackTrace(System.err);
                }
            }
            catch (final ScriptException e) {
                System.err.println("Error: " + e.getMessage());
                if (System.getProperty("debug", "0").equals("1")) {
                    e.printStackTrace(System.err);
                }
            }
        }
    }
}

// vim:set ts=4 et ai: //
