/*
-----BEGIN QCMOD-----
name: hunspell
arg: with-hunspell-inc=[path],Path to Hunspell include files
arg: with-hunspell-lib=[path],Path to Hunspell library files
-----END QCMOD-----
*/

#define QC_HUNSPELL
bool qc_hunspell_have = false;
QStringList qc_hunspell_defs;
QStringList qc_hunspell_incs;
QStringList qc_hunspell_libs;

//----------------------------------------------------------------------------
// qc_hunspell
//----------------------------------------------------------------------------
class qc_hunspell : public ConfObj
{
public:
    qc_hunspell(Conf *c) : ConfObj(c) {}
    QString name() const { return "hunspell"; }
    QString shortname() const { return "hunspell"; }

    // no output
    QString checkString() const { return QString(); }

    bool exec()
    {
    	// on mac, always use built-in spell check
#ifdef Q_OS_MAC
    	return false;
#else
    	qc_hunspell_have = false;
    	qc_hunspell_defs.clear();
    	qc_hunspell_incs.clear();
    	qc_hunspell_libs.clear();

#ifdef Q_OS_WIN
    	QStringList libnames;
# ifdef _MSC_VER
        qc_hunspell_defs += "HUNSPELL_STATIC"; // at least it's static by default when their msvc sln is in use.
#  ifdef QC_BUILDMODE
    	libnames += (qc_buildmode_debug? "libhunspelld" : "libhunspell"); // possibly static
        libnames += (qc_buildmode_debug? "hunspelld" : "hunspell"); // possibly dynamic
#  else
    	libnames << "libhunspell" << "hunspell";
#  endif
# else // mingw?
        libnames << "hunspell-1.3" << "hunspell";
# endif
        QString libs, incs;
        foreach (const QString libname, libnames)
            if(conf->findSimpleLibrary("QC_WITH_HUNSPELL_INC", "QC_WITH_HUNSPELL_LIB", "hunspell/hunspell.hxx", libname, &incs, &libs)) {
                break;
            }

        if(libs.isEmpty()) {
            printf("hunspell library is not found");
            return false;
        }
        conf->addLib(libs);

    	qc_hunspell_defs += "HAVE_HUNSPELL";
        qc_hunspell_libs += libs;
        qc_hunspell_incs += incs + "/hunspell";
    	qc_hunspell_have = true;
#else // Q_OS_WIN. unix below
    	qc_hunspell_have = false;
    	qc_hunspell_defs.clear();
    	qc_hunspell_incs.clear();
    	qc_hunspell_libs.clear();

    	QStringList incs;
    	QString version, libs, other;
    	if(!conf->findPkgConfig("hunspell", VersionMin, "1.3.0", &version, &incs, &libs, &other))
                        return false;

    	qc_hunspell_defs += "HAVE_HUNSPELL";
    	qc_hunspell_incs += incs;
    	qc_hunspell_libs += libs;
    	qc_hunspell_have = true;
#endif // Q_OS_WIN

    	return true;
#endif // Q_OS_MAC
    }
};
