var _settings = require('./settings');
var path = require('path');

var mapnik;
var _mapnik = './_mapnik.node';


// standard build, pull unversioned _mapnik.node
if ( path.existsSync(path.join(__dirname,_mapnik)) ) {
    mapnik = require(_mapnik);
}
//_mapnik.node in versioned subfolder based on node version
// it was compiled against (only currently used for packaging)
else {
    var version = process.versions.node.split('-')[0];
    _mapnik = './' + version + '/_mapnik.node';
    mapnik = require(_mapnik);
}

function warning(value, what)
{
    var msg = 'Warning: node-mapnik initialization failed\n';
    msg += 'in "' + __dirname + '/settings.js" ';
    msg += what + '\n';
    msg += 'Auto-loading of ';
    if (value == 'fonts')
        msg += 'DejaVu fonts';
    else if (value == 'plugins')
        msg += 'Datasource input plugins';
    msg += ' will not be available.\n';
    msg += 'See the docs/FAQ on ideas for how to fix this issue.';
    console.log(msg);
}

// hack to force libmapnik symbols to be loaded with RTLD_NOW|RTLD_GLOBAL
// so that datasource plugins will have access to symbols since node.cc (process.dlopen)
// uses RTLD_LAZY in its call to dlopen and has no mechanism to set the RTLD_NOW flag.
// Not needed on darwin because mapnik input plugins are directly linked to libmapnik
if (process.platform === 'linux') {
    if (!path.existsSync(path.join(__dirname,_mapnik)))
        console.log(mapnik_node + ' does not exist, loading plugins will fail on linux');
    
    var loaded = mapnik.make_mapnik_symbols_visible(mapnik_node);
    
    if (!loaded)
        console.log('Warning, attempt to pre-load mapnik symbols did not work, see FAQ for potential solutions');
}

if (!_settings.paths.fonts) {
    warning('fonts', 'the fonts path value is missing');
} else if (!path.existsSync(_settings.paths.fonts)) {
    warning('fonts', 'the fonts directory: "' + _settings.paths.fonts + '" does not exist');
} else {
    // TODO - warn if no fonts are successfully registered
    mapnik.register_fonts(_settings.paths.fonts);
}

if (!_settings.paths.input_plugins) {
    warning('plugins', 'the datasource plugins (input) path value is missing');
} else if (!path.existsSync(_settings.paths.input_plugins)) {
    warning('plugins', 'the input_plugins directory: "' + _settings.paths.input_plugins + '" does not exist');
} else {
    mapnik.register_datasources(_settings.paths.input_plugins);
}

// push all C++ symbols into js module
for (var k in mapnik) { exports[k] = mapnik[k]; }

// make settings available
exports['settings'] = _settings;


@@ -1351,37 +1351,39 @@
 ) %7B%0A    
-if (!path.existsSync(
+var mapnik_node_path = 
 path.joi
@@ -1402,16 +1402,59 @@
 _mapnik)
+;%0A    if (!path.existsSync(mapnik_node_path
 ))%0A     
@@ -1606,16 +1606,21 @@
 nik_node
+_path
 );%0A    %0A

fix for global symbol trick on linux - RTLD_NOW