/* -*- C++ -*-

Copyright (C) 2000 Francesco Bresciani <fbresciani@users.sourceforge.net>
This file is part of Kemistry

Kemistry 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, or (at your option)
any later version.

Kemistry 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 Kemistry; see the file COPYING.  If not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

*/


#include <kapp.h>
#include <kaboutdata.h>
#include <kcmdlineargs.h>
#include <kglobal.h>
#include <klocale.h>

#include <qmap.h>
#include <qtextstream.h>

#include <kmolcalcparser.h>
#include <kdebug.h>

#include "kmolcalc.h"
#include "config.h"

#define APP_VERSION "0.5.1"

static KCmdLineOptions options[] =
{
	{"raw", I18N_NOOP("Raw output"), 0},
	{"+[string]", I18N_NOOP("Formula to be parsed"), 0},
	{0, 0, 0}
};


int main(int argc, char **argv)
{
	QString version = APP_VERSION;
	version += QString(" (Kemistry %1)").arg(VERSION);
	KAboutData about("kmolcalc", "KMolCalc", version,
		I18N_NOOP("Molecular weight calculator"), KAboutData::License_GPL,
		"(C) 2000-2002 Francesco Bresciani",
		I18N_NOOP("This program is part of Kemistry, a collection\nof chemistry"
		" applications for the K Desktop Environment"),
		"http://kemistry.sourceforge.net/kmolcalc.php",
		"fbresciani@users.sourceforge.net");
	about.addAuthor("Francesco Bresciani", I18N_NOOP("Author and maintainer"),
		"fbresciani@users.sourceforge.net");

	about.addAuthor("Tomislav Gountchev",
		I18N_NOOP("Tomislav is the author of KMol.\n"
		"KMolCalc is simply a port of KMol to KDE 3,"
        " with some enhancements."));

	about.setTranslator(I18N_NOOP("_:NAME OF TRANSLATORS\nYour names"),
	I18N_NOOP("_:EMAIL OF TRANSLATORS\nYour emails"));

	KCmdLineArgs::init(argc, argv, &about);
	KCmdLineArgs::addCmdLineOptions(options);

	KApplication app;

	KCmdLineArgs* args = KCmdLineArgs::parsedArgs();


  // No command line args, show the GUI
	if (!args->count())
	{
		KMolCalcWin *kmolcalc = new KMolCalcWin();
            kmolcalc->show();
            return app.exec();
	}

	QTextOStream output(stdout);
	QTextOStream error(stderr);

	bool raw = args->isSet("raw");

	// create a new Molecular Mass parser;
	KMolCalcParser* parser = new KMolCalcParser();
	QMap<QString, QString> errorsMap;

	if (!raw)
		output<<i18n("Results:")<<endl;

	for (int i = 0; i<args->count(); i++)
	{
		parser->parseFormula(args->arg(i));
                QString errormsg = parser->errorMessage();
		if ( errormsg == QString::null )
		{
			double mass = parser->computeMass();
            if (raw)
				output<<mass<<endl;
			else
				output<<args->arg(i)<<"-----> "<<mass<<" g/mol"<<endl;
		}
		else
    	{
      	errorsMap[args->arg(i)] = errormsg;
    	}
	}
	if (!errorsMap.isEmpty())
	{
		QMap<QString, QString>::Iterator it;
		for (it = errorsMap.begin(); it != errorsMap.end(); ++it)
			{
				error<<i18n("Error while parsing formula %1: %2").arg(it.key()).
				arg(it.data())<<endl;
			}
	}

	args->clear();

	return 0;
}

