/**
 * Compile a jade file.
 * @function lib.compile.compileJadeFile
 * @see {@link http://jade-lang.com/reference/ | jade}
 * @param {string} src - Source jade file.
 * @param {string} dest - Destination html file.
 * @param {object} data - Render data.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var fs = require('fs'),
    path = require('path'),
    compileJadeString = require('./compile_jade_string'),
    file = require('../file'),
    mkdirP = file.mkdirP,
    writeReadonlyFile = file.writeReadonlyFile;

module.exports = function (src, dest, data, callback) {
    fs.readFile(src, function (err, buffer) {
        if (err) {
            callback(err);
            return;
        }
        data.filename = src;
        compileJadeString(buffer.toString(), data, function (err, rendered) {
            if (err) {
                callback(err);
                return;
            }
            mkdirP(path.dirname(dest), function (err) {
                if (err) {
                    callback(err);
                    return;
                }
                writeReadonlyFile(dest, rendered, function (err) {
                    callback(err);
                });
            });
        });
    });
};