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

   Copyright (C) 2021 LLC "RusBITech-Astra" (https://astralinux.ru)

   libfly-ocr is free software: you can redistribute it and/or modify
   it under the terms of the GNU Lesser 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 Lesser General Public
   License along with libfly-ocr.  If not, see <https://www.gnu.org/licenses/>.
***/

#include <QApplication>
#include <QDir>
#include "fly-ocr.h"

QString FlyOcr::pluginPath()
{
    QByteArray path = qgetenv("FLY_OCR_PLUGINS");
    if (!path.isEmpty())
        return path;
    QString fallbackPath = qApp->applicationDirPath() + "/../lib/fly-ocr/";
#ifdef FLY_OCR_PLUGIN_DIR
    QDir dir(FLY_OCR_PLUGIN_DIR);
#else
    QDir dir(fallbackPath);
#endif
    if (!dir.exists())
        dir = QDir(fallbackPath);
    return dir.canonicalPath();
}

QStringList FlyOcr::findPlugins()
{
    QDir pluginDir(pluginPath());
    QStringList paths;
    for (const QFileInfo& info : pluginDir.entryInfoList({ "*.dll", "*.so" }, QDir::Files))
        paths << info.canonicalFilePath();
    return paths;
}
