/*! @file

    @id $Id$

    Build für Windoof:
    <code>QMAKESPEC=$(pwd)/mkspecs/mingw-g++ qmake-qt4 && make release</code>
*/
//       1         2         3         4         5         6         7         8
// 45678901234567890123456789012345678901234567890123456789012345678901234567890

#include <tokentool.hxx>

#include <QApplication>
#include <QTranslator>
#include <QTextCodec>
#include <QLocale>
#include <QProcess>
#include <QFileInfo>
#include <iostream>

int main(int argv, char** argc) try {
  //............................................................................
  QStringList l(QProcess::systemEnvironment());
  QMap<QString, QString> env;
  for (QStringList::iterator it(l.begin()); it!=l.end(); ++it) {
    QStringList v(it->split('='));
    QString key(*v.begin());
    QString value((v.pop_front(), v.join("=")));
    env.insert(key, value);
  }
  if (env.contains("LANGUAGE")) QLocale::setDefault(env["LANGUAGE"]);
  //............................................................................
  QApplication app(argv, argc);
  app.setWindowIcon(QIcon(":/icons/tokentool"));
  app.setApplicationName(QObject::trUtf8("tokentool",
                                         "application name"));
  app.setApplicationVersion(VERSION);
  QTranslator qtTranslator;
  qtTranslator.load(":/language/qt_" + QLocale::system().name());
  app.installTranslator(&qtTranslator);
  QTranslator appTranslator;
  appTranslator.load(":/language/tokentool_"+ QLocale::system().name());
  app.installTranslator(&appTranslator);
  //............................................................................
  QStringList args(app.arguments());
  for (QStringList::iterator it(args.begin()); ++it!=args.end();)
    if (*it=="-h" || *it=="--help" || *it=="-help" || *it=="/?") {
      std::cout<<QObject::trUtf8
        ("Usage: %1 [OPTIONS...]\n"
         "Options:\n"
         " -h, --help        show this help text\n"
         "Environment:\n"
         "  LANGUAGE              \"de\", \"en\", ...          (actual: %2)\n")
        .arg(QFileInfo(argc[0]).fileName())
        .arg(env["LANGUAGE"])
        .toStdString()
               <<std::endl;
      return 0;
    } else {
      std::cout<<QObject::trUtf8("Too few arguments.\nTry: %1 --help")
        .arg(QFileInfo(argc[0]).fileName()).toStdString()<<std::endl;
      return 1;
    }
  //............................................................................
  tokentool win;
  win.show();
  return app.exec();
 } catch (...) {
  // unexpected exception - just terminate
  return 1;
 }
