/**
 * Publish html files.
 * @function task.worker.publishHtmlFiles
 * @param {object} config - Work configuration.
 * @param {string} config.srcDir - Source html directory.
 * @param {string} config.ignore - Ignore  html pattern. Glob.
 * @param {string} config.pattern - Source html file pattern. Glob.
 * @param {string} config.destDir - Destination html directory.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var path = require('path'),
    debug = require('./_debug'),
    _convertDirectory = require('./_convert_directory'),
    file = require('../../lib/file'),
    relativeSymlink = file.relativeSymlink;

exports = module.exports = function (config, callback) {
    var ext = config.ext || '.html';
    _convertDirectory({
        srcDir: config.srcDir,
        destDir: config.destDir,
        pattern: config.pattern,
        ignore: config.ignore,
        fileNameConverter: function (filename) {
            return exports._linkName(filename, ext);
        },
        fileContentConverter: function (src, dest, callback) {
            relativeSymlink(src, dest, function (err) {
                if (!err) {
                    debug.didCreateLink(dest, src);
                }
                callback(err);
            });
        }
    }, function (err) {
        callback(err);
    });
};
exports._linkName = function (filename, ext) {
    if (filename == 'index.html') return 'index.html';
    if (filename == 'index.min.html') return 'index.html';
    return path.basename(filename.replace(/\//g, '-'), ext);
};