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

#define QC_ASPELL
bool qc_aspell_have = false;
QStringList qc_aspell_defs;
QStringList qc_aspell_incs;
QStringList qc_aspell_libs;

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

    // 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_aspell_have = false;
    	qc_aspell_defs.clear();
    	qc_aspell_incs.clear();
    	qc_aspell_libs.clear();

    	QString s;

#ifdef Q_OS_WIN
    	s = conf->getenv("QC_WITH_ASPELL_INC");
    	if(!s.isEmpty()) {
    		if(!conf->checkHeader(s, "aspell.h")) {
    			conf->debug("Aspell includes not found!");
    			return false;
    		}
    		qc_aspell_incs += s;
    	}
    	else
    		return false;

    	QString a_lib = conf->getenv("QC_WITH_ASPELL_LIB");
    	if(a_lib.isEmpty())
    		return false;

    	QStringList libnames;
    	libnames += "aspell-15";
    	libnames += "aspell";

    	bool success;
    	QString libname_success;
    	foreach(const QString &libname, libnames) {
    		conf->debug(QString("Trying %1").arg(libname));
    		if(conf->checkLibrary(a_lib, libname)) {
    			success = true;
    			libname_success = libname;
    			break;
    		}
    	}

    	if(!success)
    		return false;

    	qc_aspell_defs += "HAVE_ASPELL";
    	qc_aspell_libs += QString("-L") + a_lib;
    	qc_aspell_libs += QString("-l") + libname_success;
    	qc_aspell_have = true;
#else
    	s = conf->getenv("QC_WITH_ASPELL_INC");
    	if(!s.isEmpty()) {
    		if(!conf->checkHeader(s, "aspell.h")) {
    			conf->debug("Aspell includes not found!");
    			return false;
    		}
    		qc_aspell_incs += s;
    	}
    	else {
    		QStringList sl;
    		sl += "/usr/include";
    		sl += "/usr/local/include";
    		sl += "/sw/include";
    		if(!conf->findHeader("aspell.h", sl, &s)) {
    			conf->debug("Aspell includes not found!");
    			return false;
    		}
    		qc_aspell_incs += s;
    	}

    	s = conf->getenv("QC_WITH_ASPELL_LIB");
    	if(!s.isEmpty()) {
    		if(!conf->checkLibrary(s, "aspell")) {
    			conf->debug("Aspell libraries not found!");
    			return false;
    		}
    		qc_aspell_libs += QString("-L") + s;
    	}
    	else {
    		if(!conf->findLibrary("aspell", &s)) {
    			conf->debug("Aspell libraries not found!");
    			return false;
    		}
    		if (!s.isEmpty())
    			qc_aspell_libs += QString("-L") + s;
    	}

    	qc_aspell_defs += "HAVE_ASPELL";
    	qc_aspell_libs += "-laspell";
    	qc_aspell_have = true;
#endif

    	return true;
#endif
    }
};
