/**
 * Create a cleaned directory. If already exists, regenerate it. If it's parents were missing, create them.
 * @function lib.file.prepareCleanDir
 * @param {string} dirname - Directory name to prepare.
 * @param {function} callback - Callback when done.
 * @author Taka Okunishi
 *
 */
var fs = require('fs'),
    rmdirRecursive = require('./rmdir_recursive'),
    mkdirP = require('./mkdir_p');

module.exports = function (dirname, callback) {
    if (!callback) callback = new Function;
    fs.exists(dirname, function (exists) {
        if (exists) {
            rmdirRecursive(dirname, function () {
                mkdirP(dirname, callback);
            });
        } else {
            mkdirP(dirname, callback);
        }
    });
};