#include "WebFormat.h"

using namespace magics;

namespace magml
{

pair<string, string> cut(const string& in)
{
	string::const_iterator c = in.begin();
	string var;
	string current;
	bool  param = true;

	while ( c != in.end() )
	{		
		if ( *c == '='  && param )
		{			
			var = current;
			current = "";
			param= false;
		}
		else {
			current.push_back(*c);
		}
		c++;
	}
	return make_pair(var, current);
}

void setVariable(const string& in, map<string, string>& variables)
{
	if(in[0] != '-')
	{
		MagLog::warning() << "argument " << in << " ignored."<<endl;
		return;
	}
	variables.insert(magml::cut(in.substr(1, in.size())));
}

} //end of namespace magml

int main(int argc, char **argv)
{
  try
    {
	if(argc<2)
	{
		cout << " Usage: " << argv[0] << " file.json [-varname=varvalue]"<< endl;
		exit (1);
	}

	map<string, string> variables;

	for( int i = 2; i < argc; i++ )
		magml::setVariable(argv[i], WebInterpretor::parameters());

	//for ( int i = 0; i < 10; i++) 
	{
		try {
			WebInterpretor::json(argv[1]);
		}
		catch (...) {
			cout << argv[0] << " FAILED to dispatch JSON file!"<< endl;
			exit(1);
		}
	}
    }
    catch (MagicsException& e)
    {
		cout << "MagJson: Catch Exception " << e << endl;
		exit(1);
    }
}
