(function(exports){
	'use strict'
	
	
	var flash_str;
	
	
	function stdout(cb, text)
	{
		text=text.replace(/\r/g, '\n');
		var nl;
		while ( (nl=text.indexOf('\n')) >= 0 )
		{
			cb(flash_str + text.substring(0, nl));
			flash_str="";
			text=text.substring(nl+1);
		}
		flash_str=text;
	}
	
	
	var flash_ok;
	
	var stdout_line = function(text)
	{
		if (text.length == 0) return;
		
		print("stdout: '"+text+"'\n");
		if (text.match(/Flash written and verified/))
			flash_ok=true;
		
		var p = /^\s*([0-9]+)\/([0-9]+) pages written/.exec(text);
		if (p != null)
			ui.progress(100 * p[1] / p[2]);
	};
	
	
	exports.write = function(filename, reset, addr) {
		if (! addr) addr="0x08000000";
		try
		{
			flash_ok=false;
			flash_str="";
			var args=[ "write", filename, addr ];
			if (reset) args.unshift("--reset");
			var rv=system.exec("flashers/stn-flash.exe", args, stdout.bind(null, stdout_line), stdout.bind(null, stdout_line));
			if (rv != 0) throw new Error("errorcode="+rv);
			if (! flash_ok) throw new Error("ok-pattern not found");
		} catch (e)
		{
			print("stn-flash error: "+e+"\n");
			return false;
		}
		
		return true;
	};
	
	exports.read = function(filename, addr, size, reset) {
		try
		{
			var args=[ "read", filename, addr, size ];
			if (reset) args.unshift("--reset");
			var rv=system.exec("flashers/stn-flash.exe", args, print, print);
			if (rv != 0) throw new Error("errorcode="+rv);
		} catch (e)
		{
			print("stn-flash error: "+e+"\n");
			return false;
		}
		
		return true;
	};
	
	
	exports.erase = function(reset) {
		try
		{
			var args=[ "erase" ];
			if (reset) args.unshift("--reset");
			var rv=system.exec("flashers/stn-flash.exe", args, print, print);
			if (rv != 0) throw new Error("errorcode="+rv);
		} catch (e)
		{
			print("stn-flash error: "+e+"\n");
			return false;
		}
		
		return true;
	};
	
})(typeof exports === 'undefined' ? this['_stnflash_js']={} : exports);
