/**
 * Command line implementation for CSSComb
 *
 * Usage example:
 * ./node_modules/.bin/csscomb [options] file1 [dir1 [fileN [dirN]]]
 */
var fs = require('fs');
var path = require('path');
var program = require('commander');
var vow = require('vow');
var Comb = require('./csscomb');

program
    .version(require('../package.json').version)
    .usage('[options] <file ...>')
    .option('-v, --verbose', 'verbose mode')
    .option('-c, --config [path]', 'configuration file path')
    .option('-d, --detect', 'detect mode (would return detected options)')
    .option('-l, --lint', 'in case some fixes needed returns an error')
    .parse(process.argv);

if (!program.args.length) {
    console.log('No input paths specified');
    program.help();
}

/**
 * Look for a config file: recursively from current (process) directory
 * up to $HOME dir
 * @param {String} configPath
 * @returns {String}
 */
function getConfigPath(configPath) {
    var HOME = process.env.HOME || process.env.HOMEPATH || process.env.USERPROFILE;
    // Since `process.cwd()` can be absolutely anything, build default path
    // relative to current directory:
    var defaultConfigPath = __dirname + '/../config/csscomb.json';

    configPath = configPath || process.cwd() + '/.csscomb.json';

    // If we've finally found a config, return its path:
    if (fs.existsSync(configPath)) return configPath;

    // If we are in HOME dir already and yet no config file, return a default
    // one from our package:
    if (path.dirname(configPath) === HOME) return defaultConfigPath;

    // If there is no config in this directory, go one level up and look for
    // a config there:
    configPath = path.dirname(path.dirname(configPath)) + '/.csscomb.json';
    return getConfigPath(configPath);
}

var configPath = program.config || getConfigPath();
var comb = new Comb();

if (program.detect) {
    console.log(JSON.stringify(comb.detectInFile(program.args[0]), false, 4));
    process.exit(0);
}

if (fs.existsSync(configPath)) {
    var config;
    if (configPath.match(/\.css$/)) {
        config = comb.detectInFile(configPath);
    } else {
        config = require(configPath);
    }

    if (config.template) {
        if (fs.existsSync(config.template)) {
            var templateConfig = comb.detectInFile(config.template);
            for (var attrname in templateConfig) {
                if (!config[attrname]) {
                    config[attrname] = templateConfig[attrname];
                }
            }
        } else {
            console.log('Template configuration file ' + config.template + ' was not found.');
            process.exit(1);
        }
    }

    console.time('spent');

    config.verbose = program.verbose === true || config.verbose;
    config.lint = program.lint;

    comb.configure(config);

    vow.all(program.args.map(comb.processPath.bind(comb)))
    .then(function() {
        if (config.verbose) {
            console.log('');
            console.log(comb.processed + ' file' + (comb.processed === 1 ? '' : 's') + ' processed');
            console.log(comb.changed + ' file' + (comb.changed === 1 ? '' : 's') + ' fixed');
            console.timeEnd('spent');
        }
        if (config.lint && comb.tbchanged) {
            process.exit(1);
        }
    })
    .fail(function(e) {
        console.log('stack: ', e.stack);
        process.exit(1);
    });
} else {
    console.log('Configuration file ' + configPath + ' was not found.');
    process.exit(1);
}

@@ -1491,51 +1491,373 @@
 kage
-:%0A    if (path.dirname(configPath) === HOME
+.%0A    // If project is located not under HOME, compare to root instead.%0A    // Since there appears to be no good way to get root path in%0A    // Windows, assume that if current dir has no parent dir, we're in%0A    // root.%0A    var dirname = path.dirname(configPath);%0A    var parentDirname = path.dirname(dirname);%0A    if (dirname === HOME %7C%7C dirname === parentDirname
 ) re
@@ -2004,44 +2004,19 @@
 = pa
-th.dirname(path.dirname(configPath))
+rentDirname
  + '

Stop searching config if we reach root directory