// Sunflow start script, exits with 1 on errors

// Virtual memory of the JVM.
// Check Sun's docs for values above 4GB.
var mem="1G";

// Set to true to show the java console
var showConsole = false;

var sh = new ActiveXObject("WScript.Shell");
var env = sh.Environment("PROCESS");
var fso = WScript.CreateObject("Scripting.FileSystemObject");

/**
* Reads a registry entry.
*
* @param sh An Instance of WScript.Shell.
* @param key The name of the key including the complete path.
* @return The string representation of the registry value or null if it does not exist.
*/
function readReg(sh, key) {
	var res;
	try {
		res = sh.RegRead(key);
	} catch (e) {
		// Key nonexistant
		res = null;
	}
	
	return res;
}

// Is the server VM present?
var serverVM = true;

// Name of the Java executable, without path
var javaExec = showConsole ? "java" : "javaw";

// Is the path already complete (including the exec)?
var pathComplete = false;

// Path of java directory
var javaPath = null;
// If you want to override automatic detection (old batfile behaviour), uncomment the following line (remove the slashes).
// javaPath = "YOURJAVADIR";

// JAVA_HOME environment variable
if (javaPath == null) {
	var javaHome = env("JAVA_HOME");
	
	if ((javaHome != null) && (javaHome != "")) {
		if (fso.FolderExists(javaHome)) {
			javaPath = javaHome;
		}
	}
}

// Probe for JDK
if (javaPath == null) {
	var javaVers = readReg(sh, "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\CurrentVersion");
	
	if (javaVers != null) {
		javaPath = readReg(sh, "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Development Kit\\" + javaVers + "\\JavaHome");
		
		if (javaPath != null) {
			serverVM = true;
		}
	}
}

// JRE
if (javaPath == null) {
	var javaVers = readReg(sh, "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\CurrentVersion");

	if (javaVers != null) {
		javaPath = readReg(sh, "HKEY_LOCAL_MACHINE\\SOFTWARE\\JavaSoft\\Java Runtime Environment\\" + javaVers + "\\JavaHome");
		
		if (javaPath != null) {
			serverVM = false;
		}
	}
}

// Java interpreter already in path? (JRE does that by copying the files to C:\Windows\System32)
if (javaPath == null) {
	try {
		var tph = sh.exec("cmd /c " + javaExec + " -server -version > \\.\nul 2> \\.\nul");
		
		// Wait for termination
		while (tph.Status == 0) {
			WScript.sleep(50);
		}
		
		javaPath = javaExec;
		pathComplete = true;
		// Likely server VM not found
		if (tph.ExitCode != 0) {
			serverVM = false;
		}
	} catch (e) {
		; // Java not found
	}
}

// Guess
if (javaPath == null) {
	var guessedPath = "\\java";
	
	if (fso.FolderExists(guessedPath)) {
		javaPath = guessedPath;
		
		// Assume server VM
		serverVM = true;
	}
}

if (javaPath == null) {
	WScript.echo("No Java Installation could be found. Please download and install the JDK from http://java.sun.com/javase/downloads/ .");
	WScript.quit(1);
}

var options = " -Xmx" + mem + (serverVM ? " -server" : "") + " -jar sunflow.jar";

var arguments = "";
for (var i = 0;i < WScript.Arguments.Length;i++) {
	var arg = WScript.Arguments.Item(i);
	
	arguments += " \"" + arg + "\"";
}

var cmd = javaPath + (pathComplete ? "" : ("\\bin\\" + javaExec)) + options + arguments;

var ph = sh.Exec(cmd);
while(! ph.StdErr.AtEndOfStream) {
	WScript.echo(ph.StdErr.ReadLine());
}
