import os

settings = os.path.join(os.path.dirname(__file__),'lib','mapnik_settings.js')

# this goes into a mapnik_settings.js file beside the C++ _mapnik.node
settings_template = """
module.exports.paths = {
    'fonts': %s,
    'input_plugins': %s
};
"""

def write_mapnik_settings(fonts='undefined',input_plugins='undefined'):
    global settings_template
    if '__dirname' in fonts or '__dirname' in input_plugins:
        settings_template = "var path = require('path');\n" + settings_template
    open(settings,'w').write(settings_template % (fonts,input_plugins))


if __name__ == '__main__':
    settings_dict = {}
    
    # settings for fonts and input plugins
    if os.environ.has_key('MAPNIK_INPUT_PLUGINS_DIRECTORY'):
        settings_dict['input_plugins'] =  os.environ['MAPNIK_INPUT_PLUGINS_DIRECTORY']
    else:
        settings_dict['input_plugins'] = '\'%s\'' % os.popen("mapnik-config --input-plugins").readline().strip()
    
    if os.environ.has_key('MAPNIK_FONT_DIRECTORY'):
        settings_dict['fonts'] =  os.environ['MAPNIK_FONT_DIRECTORY']
    else:
        settings_dict['fonts'] = '\'%s\'' % os.popen("mapnik-config --fonts").readline().strip()

    write_mapnik_settings(**settings_dict)
@@ -674,416 +674,118 @@
     
-if os.environ.has_key('MAPNIK_INPUT_PLUGINS_DIRECTORY'):%0A        settings_dict%5B'input_plugins'%5D =  os.environ%5B'MAPNIK_INPUT_PLUGINS_DIRECTORY'%5D%0A    else:%0A        settings_dict%5B'input_plugins'%5D = '%5C'%25s%5C'' %25 os.popen(%22mapnik-config --input-plugins%22).readline().strip()%0A    %0A    if os.environ.has_key('MAPNIK_FONT_DIRECTORY'):%0A        settings_dict%5B'fonts'%5D =  os.environ%5B'MAPNIK_FONT_DIRECTORY'%5D%0A    else:%0A    
+settings_dict%5B'input_plugins'%5D = '%5C'%25s%5C'' %25 os.popen(%22mapnik-config --input-plugins%22).readline().strip()%0A    %0A
     

stop reading fonts/input plugins from environ as we now have a working mapnik-config.bat on windows