'use strict';

var node_path   = require('path');
var option_cwd  = require('../util/cwd');
var fs = require('fs');

exports.shorthands = {
  c: 'cwd',
};

exports.options = {
  // cortex build abc
  cwd: option_cwd,

  ranges: {
    type: Boolean,
    info: 'check ranges and build local files with range pathes.',
    default: true
  },

  preinstall: {
    enumerable: false,
    type: Boolean,
    default: false
  },

  prebuild: {
    enumerable: false,
    type: Boolean,
    default: true
  },

  dest: {
    type: node_path,

    // default to be profile.built_root
    info: 'the destination directory to be built into',
    setter: function (dest) {
      var done = this.async();
      if (!dest) {
        dest = node_path.join(this.get('cwd'), 'neurons');

        function cb (err) {
          done(err || null, dest);
        }

        // legacy
        // If `neurons` exists and is not a directory, try to unlink it.
        fs.lstat(dest, function (err, stat) {
          if (err) {
            if (err.code === 'ENOENT') {
              return cb();
            }
            return cb(err);
          }

          if (stat.isDirectory()) {
            return cb();
          }

          fs.unlink(dest, cb);
        });
        return;
      }

      done(null, dest);
    }
  },

  link: {
    enumerable: false,
    type: Boolean,
    info: 'link the neurons/ folder to built root.',
    default: true
  }
};


exports.info = 'Build module wrapper, and publish to cortex server root.';

exports.usage = [
  '{{name}} build [options]'
];

@@ -681,24 +681,141 @@
 is.async();%0A
+%0A      function cb (err) %7B%0A        // '/abc/' -%3E '/abc'%0A        done(err %7C%7C null, node_path.resolve(dest));%0A      %7D%0A%0A
       if (!d
@@ -884,83 +884,8 @@
 ');%0A
-%0A        function cb (err) %7B%0A          done(err %7C%7C null, dest);%0A        %7D%0A%0A
     
@@ -1313,23 +1313,11 @@
     
-done(null, dest
+cb(
 );%0A 

resolve options.dest; #397