/*
 * Run_Script.bsh - a BeanShell macro script for the
 * jEdit text editor - Runs script using interpreter based upon
 * buffer's editing mode (by default, determined using file extension).
 *
 * Copyright (C) 2001 John Gellene
 * jgellene@nyc.rr.com
 * http://community.jedit.org
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with the jEdit program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 *
 * $Id: Run_Script.bsh 3912 2001-11-24 03:56:51Z jgellene $
 */


void execScript(interpreter, command)
{
	params = Macros.input(view,
		"Specify parameters for running script under " + interpreter);
	if(params == null)
	{
		Macros.message(view, "Script execution was cancelled.");
		return;
	}
	runInSystemShell(view, command + params);
}

void runScript()
{
	if(buffer.isNewFile())
		buffer.saveAs(view, true);
	else
		buffer.save(view, buffer.getPath());
	mode = buffer.getMode().getName();
	path = buffer.getPath() + " ";
	os = System.getProperty("os.name");
	if(os.indexOf("Windows") != -1)
		path = "\"" + path + "\"";
	if(mode.equals("beanshell")) {
		source(path);
	}
	else if(mode.equals("awk")) {
		execScript("awk", "awk -f " + path);
	}
	else if(mode.equals("batch")) {
		if(os.indexOf("Windows 9") == -1
			&& os.indexOf("Windows M") == -1)
		{
			execScript("shell", "cmd /C " + path);
		}
		else execScript("shell", "command /C " + path);
	}
	else if(mode.equals("javascript")) {
		execScript("Windows Script Host", "wscript " + path);
	}
	else if(mode.equals("jmk")) {
		execScript("jmk", "java -jar jmk.jar -f" + path);
	}
	else if(mode.equals("makefile")) {
		if(os.indexOf("Windows") == -1) {
			execScript("make", "make -f " + path);
		}
		else {
			execScript("nmake", "nmake -f " + path);
		}
	}
	else if(mode.equals("netrexx"))  {
		execScript("NetRexx", "NetRexxC -exec " + path);
	}
	else if(mode.equals("perl"))	{
		execScript("perl", "perl " + path);
	}
	else if(mode.equals("python")) {
		execScript("python", "python " + path);
	}
	else if(mode.equals("ruby")) {
		execScript("ruby", "ruby " + path);
	}
	else if(mode.equals("scheme")) {
		execScript("scheme", "scheme -load " + path);
	}
	else if(mode.equals("shellscript"))  {
		execScript("shell", "bash " + path);
	}
	else if(mode.equals("tcl")) {
		execScript("tcl", "tcl " + path);
	}
	else if(mode.equals("vbscript"))  {
		execScript("Windows Script Host", "wscript " + path);
	}
	else {
		Macros.error(view,
			"The current file does not appear to be a script.");
	}
}

runScript();


/*

Macro index data (in DocBook format)

  <listitem>
    <para><filename>Run_Script.bsh</filename></para>
    <abstract><para>
      Runs script using interpreter based upon buffer's editing mode
	  (by default, determined using file extension).
    </para></abstract>
  </listitem>

*/

// end Run_Script.bsh
