/**
 * Compile a coffee script file.
 * @function lib.compile.compileCoffeeScriptFile
 * @see {@link http://coffeescript.org/ | coffee script}
 * @param {string} src - Source script file.
 * @param {string} dest - Destination js file.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var fs = require('fs'),
    path = require('path'),
    file = require('../file'),
    compileCoffeeScriptString = require('./compile_coffee_script_string'),
    mkdirP = file.mkdirP,
    writeReadonlyFile = file.writeReadonlyFile;

module.exports = function (src, dest, callback) {
    var coffeeScript = require('coffee-script');
    fs.readFile(src, 'utf-8', function (err, content) {
        if (err) {
            callback(err);
            return;
        }
        compileCoffeeScriptString(content, function (err, compiled) {
            if (err) {
                callback(err);
                return;
            }
            mkdirP(path.dirname(dest), function () {
                writeReadonlyFile(dest, compiled, function (err) {
                    callback(err);
                });
            });
        });
    });
};