/*
    goahead-openssl.me -- OpenSSL Component for GoAhead
 */

Me.load({ 
    targets: {
        openssl: {
            description: 'OpenSSL Support',
            config: function (target) {
                if (me.options.gen) {
                    let result = {
                        /*
                            Static configuration to work for standard installations under /usr/lib and /usr/include
                            and in a custom user directory.
                         */
                        includes: [ me.dir.inc ],
                        libraries: me.platform.os == 'windows' ?
                            ([ 'gdi32', 'libeay32', 'ssleay32' ]) : ([ 'ssl', 'crypto' ]),
                    }
                    if (me.options.gen == 'make') {
                        result.defines = ['ME_COM_OPENSSL_PATH=/usr/lib']
                    } else if (me.options.gen == 'nmake') {
                        result.defines = ['ME_COM_OPENSSL_PATH=']
                    }
                    if (me.options.gen == 'make' || me.options.gen == 'nmake' || me.options.gen == 'xcode') {
                        result.includes.push('$(ME_COM_OPENSSL_PATH)/include')
                        result.libpaths = ['$(ME_COM_OPENSSL_PATH)']
                    }
                    return result
                }
                let lib, libraries, libpaths, imports, includes, path
                let search = getComponentSearch(target, 'openssl')
                if (me.platform.os == 'windows') {
                    search = search.map(function(p:Path) p.join('out32')) +
                             search.map(function(p:Path) p.join('out32dll'))
                    lib = probe('libeay32.lib', {fullpath: true, search: search, nopath: true})
                    path = lib.dirname.parent
                    libpaths =  [ lib.parent ]
                    libraries = [ 'gdi32', 'libeay32', 'ssleay32' ]
                    imports =   [ lib.parent.join('libeay32.lib'), lib.parent.join('ssleay32.lib') ]
                    if (lib.dirname == 'out32dll') {
                        imports += [ lib.parent.join('libeay32.dll'), lib.parent.join('ssleay32.dll') ]
                    }
                    includes = [ probe('openssl/ssl.h', {search: [path.join('inc32')]}) ]

                } else {
                    let opt = {fullpath: true, search: search, nopath: true}
                    let inc, lib
                    if ((lib = probe('libcrypto.' + me.ext.shobj, blend({nothrow: true}, opt))) != null) {
                        let isearch = [ lib.dirname.join('include') ]
                        if (!me.platform.cross) {
                            isearch.push('/usr/include')
                        }
                        inc = probe('openssl/ssl.h', {search: isearch, nothrow: true})
                    }
                    if (!lib || !inc) {
                        lib = probe('libcrypto.' + me.ext.lib, opt)
                        let isearch = [ lib.dirname.join('include') ]
                        if (!me.platform.cross) {
                            isearch.push('/usr/include')
                        }
                        inc = probe('openssl/ssl.h', {search: isearch})
                    }
                    includes = [ inc ]
                    libraries = [ 'ssl', 'crypto' ]
                    libpaths = [ lib.parent ]
                    imports = lib.parent.files('libssl.*' + lib.extension + '*') + 
                              lib.parent.files('libcrypto.*' + lib.extension + '*')
                    path = lib.dirname
                }
                includes.push(me.dir.inc)
                return {
                    location:   path,
                    imports:    imports,
                    includes:   includes,
                    libpaths:   libpaths,
                    libraries,  libraries,
                }
            },
            ifdef:      [ 'ssl' ],
            conflicts:  [ 'est', 'matrixssl', 'nanossl' ],
            depends:    [ 'libgoahead-openssl' ],
        },

        'libgoahead-openssl': {
            description:  'GoAhead OpenSSL Interface',
            type:         'lib',
            static:       true,
            path:         '${BIN}/libgoahead-openssl${ARLIB}',
            ifdef:        [ 'openssl' ],
            sources:      [ '*.c' ],
            postresolve: `
                let target = me.target
                let openssl = me.targets.openssl

                target.includes += openssl.includes
                target.libpaths += openssl.libpaths
                target.libraries += openssl.libraries

                if (me.platform.os == 'macosx') {
                    target.compiler.push('-Wno-deprecated-declarations')
                }
            `
        }
    }
})

