/**
 * Change permissions
 * @function ci.worker.changeMode
 * @example
 *
 *      changeMode({
 *          mode:'444',
 *          pattern:'doc/*.html'
 *      }, function(err){
 *          console.log('done!');
 *      });
 *
 * @param {object} config - Configuration object
 * @param {string} config.mode - File mode to set.
 * @param {string} config.pattern - File path patterns.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var fs = require('fs'),
    debug = require('./_debug'),
    expandGlob = require('../../lib/file/expand_glob');
exports = module.exports = function (config, callback) {
    var async = require('async'),
        mode = config.mode;
    expandGlob(config.pattern, function (err, filenames) {
        if (err) {
            callback(err);
            return;
        }
        async.parallel(
            filenames.map(function (filename) {
                return function (callback) {
                    fs.chmod(filename, mode, function (err) {
                        if (!err) {
                            debug.didChmod(mode, filename);
                        }
                        callback(err);
                    });
                };
            }),
            callback
        );
    });
};