/***
   This file is part of fly-ocr.

   Copyright (C) 2016-2020 Sandro Mani <manisandro@gmail.com>
   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

    Based on gImageReader source code by Sandro Mani under the GNU
    General Public License (GPL) version 3 or later

   Foobar 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 3 of the License, or
   (at your option) any later version.

   Foobar 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 fly-ocr. If not, see <https://www.gnu.org/licenses/>.
***/

#include <QtSingleApplication>
#include <QDir>
#include <QLibraryInfo>
#include <QLocale>
#include <QTextCodec>
#include <QTranslator>
#include <QtDebug>
#include <cstring>
#include "configdialog.h"
#include "flyocrstarter.h"
#include "crashhandler.h"

int main (int argc, char* argv[])
{
    QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
    QtSingleApplication app(argc, argv);

    QApplication::setOrganizationName("rusbitech");
    QApplication::setApplicationName(PACKAGE_NAME);
    QApplication::setApplicationVersion(PACKAGE_VERSION);
    QIcon::setFallbackSearchPaths(QIcon::fallbackSearchPaths() << ":/extra-theme-icons");

    QTranslator translator;
    QString translationsPath = QLibraryInfo::location(QLibraryInfo::TranslationsPath);
    translator.load(QLocale(), QLatin1String("qt"), QLatin1String("_"), translationsPath);
    translator.load(QLocale(), QLatin1String("qtbase"), QLatin1String("_"), translationsPath);
    translator.load(QLocale(), QLatin1String("fly-ocr"), QLatin1String("_"), QLatin1String(":/"));
    app.installTranslator(&translator);
    QIcon icon;
    icon.addFile(":/icons/16x16/apps/fly-ocr.png");
    icon.addFile(":/icons/32x32/apps/fly-ocr.png");
    icon.addFile(":/icons/64x64/apps/fly-ocr.png");
    icon.addFile(":/icons/256x256/apps/fly-ocr.png");
    icon.addFile(":/icons/scalable/apps/fly-ocr.svg");
    app.setWindowIcon(icon);

    const QStringList args = app.arguments();

    if (args.count() >= 3 && args.at(1) == QStringLiteral("crashhandle")) {
        int pid = args.at(2).toInt();
        int tesseractCrash = args.at(3).toInt();;
        QString savefile = args.count() >= 5 ? args.at(4) : QString();
        CrashHandler crashHandler(pid, tesseractCrash, savefile);
        crashHandler.show();
        return app.exec();
    }

    FlyOcrStarter starter(&app);
    if (starter.isFinished())
        return starter.exitCode();

    return app.exec();
}

