/*
-----BEGIN QCMOD-----
name: libidn
arg: with-idn-inc=[path],Path to libidn include files
arg: with-idn-lib=[path],Path to libidn library or framework files
-----END QCMOD-----
*/

/*
Warning: libidn is somewhat deprecated in favor of libidn2, but we can't just
drop it since IDNA2008 doesn't include XMPP profiles and has nothing from
stringprep. This stuff was moved to RFC8264 and RFC8265 and its usage was
described in RFC7622. There are no known C/C++ implementations of these specs
at the moment of this writing. A partial and not sufficient implementation can
be found at https://gitlab.com/gnutls/gnutls/blob/master/lib/str-unicode.c. It
implements OpaqueString Profile but doesn't implement UsernameCaseMapped
profile required by RFC7622. Note it may be also desired to implement RFC8266.

Some implementations in other languages:
Go: https://godoc.org/golang.org/x/text/secure/precis
Python: https://pypi.org/project/precis-i18n/
Perl: https://metacpan.org/release/Unicode-Precis
PHP: https://github.com/tom--/precis

We can try to use https://github.com/lpereira/gomoku to convert from Go.
*/

//----------------------------------------------------------------------------
// qc_idn
//----------------------------------------------------------------------------
class qc_idn : public ConfObj
{
public:
    qc_idn(Conf *c) : ConfObj(c) {}
    QString name() const { return "LibIDN"; }
    QString shortname() const { return "libidn"; }
    bool exec()
    {
        QString idn_incdir, idn_libdir, idn_prefixdir;
    	idn_incdir = conf->getenv("QC_WITH_IDN_INC");
    	idn_libdir = conf->getenv("QC_WITH_IDN_LIB");
        idn_prefixdir = conf->getenv("PREFIX");

        if (!idn_incdir.isEmpty() || !idn_libdir.isEmpty() || !idn_prefixdir.isEmpty()) { // prefer this if given
    		if ((!idn_incdir.isEmpty() && conf->checkHeader(idn_incdir, "stringprep.h")) ||
    		    (idn_incdir.isEmpty() && conf->findHeader("stringprep.h", QStringList(), &idn_incdir))) {
    			conf->addIncludePath(idn_incdir);
    		} else {
    			printf("Headers not found!\n");
    			return false;
    		}

#ifdef Q_OS_WIN
            conf->addDefine("LIBIDN_STATIC"); // it's need only for iris anyway
            QString staticLibName = qc_buildmode_debug? "libidnd" : "libidn";
            QString dynamicLibName = qc_buildmode_debug? "idnd" : "idn";
            QStringList libNames = QStringList() << staticLibName << dynamicLibName;
#else
            QStringList libNames = QStringList() << "idn";
#endif
            bool libFound = false;
            foreach (const QString &libName, libNames) {
                if((!idn_libdir.isEmpty() && conf->checkLibrary(idn_libdir, libName)) ||
                   (idn_libdir.isEmpty() && conf->findLibrary(libName, &idn_libdir))) {
                    conf->addLib(idn_libdir.isEmpty()? QString("-l") + libName : QString("-L%1 -l%2").arg(idn_libdir, libName));
                    libFound = true;
                    break;
                }
            }
            if (!libFound) {
                printf("Libraries not found!\n");
            }
            return libFound;
    	}

    	QStringList incs;
    	QString version, libs, other;
    	if(conf->findPkgConfig("libidn", VersionAny, QString::null, &version, &incs, &libs, &other))
    	{
    		for(int n = 0; n < incs.count(); ++n)
    			conf->addIncludePath(incs[n]);
    		if(!libs.isEmpty())
    			conf->addLib(libs);
    		return true;
    	}

    	return false;
    }
};
