/**
 * Compile a less file.
 * @function lib.compile.compileLessFile
 * @see {@link http://www.lesscss.org/ | less }
 * @param {string} src - Source less file.
 * @param {string} dest - Destination html file.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */

var fs = require('fs'),
    path = require('path'),
    file = require('./../file/index'),
    compileLessString = require('./compile_less_string'),
    mkdirP = file.mkdirP,
    writeReadonlyFile = file.writeReadonlyFile;

module.exports = function (src, dest, callback) {
    var less = require('less');
    fs.readFile(src, 'utf-8', function (err, content) {
        if (err) {
            callback(err);
            return;
        }
        try {
            compileLessString(content, {
                    paths: [path.dirname(src)],
                    filename: src
                },
                function (err, compiled) {
                    if (err) {
                        callback(err);
                        return;
                    }
                    mkdirP(path.dirname(dest), function (err) {
                        if (err) {
                            callback(err);
                            return;
                        }
                        writeReadonlyFile(dest, compiled, function (err) {
                            callback(err);
                        });
                    });
                });

        } catch (e) {
            callback(e);
        }
    });
};