var less = require('../less.js/lib/less'),
    async = require('../../../deps/async'),
    logger = require('../../../lib/logger'),
    utils = require('../../../lib/utils'),
    spawn = require('child_process').spawn,
    path = require('path');


function compileLess(project_path, filename, settings, callback) {
    logger.info('compiling', utils.relpath(filename, project_path));
    var args = [filename];
    if (settings.less.compress) {
        args.unshift('--compress');
    }
    var lessc = spawn(__dirname + '/../less.js/bin/lessc', args);

    var css = '';
    var err_out = '';
    lessc.stdout.on('data', function (data) {
        css += data;
    });
    lessc.stderr.on('data', function (data) {
        err_out += data;
    });
    lessc.on('exit', function (code) {
        if (code === 0) {
            callback(null, css);
        }
        else {
            callback(new Error(err_out));
        }
    });
};

module.exports = function (root, path, settings, doc, callback) {
    if (!settings.less || !settings.less.compile) {
        return callback(null, doc);
    }
    var paths = settings.less.compile || [];
    if (!Array.isArray(paths)) {
        paths = [paths];
    }
    async.forEach(paths, function (p, cb) {
        var name = p.replace(/\.less$/, '.css');
        var filename = utils.abspath(p, path);
        compileLess(path, filename, settings, function (err, css) {
            if (err) {
                return cb(err);
            }
            doc._attachments[name] = {
                content_type: 'text/css',
                data: new Buffer(css).toString('base64')
            };
            cb();
        });
    },
    function (err) {
        if (settings.less.remove_from_attachments) {
            for (var k in (doc._attachments || {})) {
                if (/\.less$/.test(k)) {
                    delete doc._attachments[k];
                }
            }
        }
        callback(err, doc);
    });
};

@@ -57,24 +57,21 @@
 equire('
-../../..
+kanso
 /deps/as
@@ -95,32 +95,29 @@
  = require('
-../../..
+kanso
 /lib/logger'
@@ -144,16 +144,13 @@
 re('
-../../..
+kanso
 /lib

fix require paths for new less-precompiler