/**!
 * cnpm - origin_npm.js
 *
 * Copyright(c) cnpmjs.org and other contributors.
 * MIT Licensed
 *
 * Authors:
 *  fengmk2 <fengmk2@gmail.com> (http://fengmk2.github.com)
 *  dead_horse <dead_horse@qq.com> (http://deadhorse.me)
 */

'use strict';

/**
 * Module dependencies.
 */

require('colors');
var debug = require('debug')('cnpm');
var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');
var config = require('./config');
var parseArgv = require('./parse_argv');

var program = parseArgv();

var args = program.rawArgs.slice(2);

var CWD = process.cwd();

if (program.userconfig && !fs.existsSync(program.userconfig)) {
  // make sure userconfig exists
  // or it will throw: npm ERR! Error: default value must be string or number
  fs.writeFileSync(program.userconfig, 'email =\n');
}

args.unshift('--registry=' + program.registry);
args.unshift('--cache=' + program.cache);
args.unshift('--disturl=' + program.disturl);
args.unshift('--userconfig=' + program.userconfig);

var nodeModulesDir = path.join(__dirname, 'node_modules', '.bin');
var cmd = 'npm-beta';
if (process.platform === "win32") {
  cmd = 'npm-beta.cmd';
}
cmd = path.join(nodeModulesDir, cmd);

debug('%s %s', cmd, args.join(' '));

var npm  = spawn(cmd, args, {
  env: process.env,
  cwd: CWD,
  stdio: [
    process.stdin,
    process.stdout,
    process.stderr,
  ]
});

npm.on('exit', function (code, signal) {
  process.exit(code);
});

@@ -1208,16 +1208,192 @@
  cmd);%0A%0A
+// if npm-beta not exists, use npm. happen on %60$ cnpm install cnpm%60%0Aif (!fs.existsSync(cmd)) %7B%0A  cmd = 'npm';%0A  if (process.platform === %22win32%22) %7B%0A    cmd = 'npm.cmd';%0A  %7D%0A%7D%0A%0A
 debug('%25

fix cnpm install cnpm fail bug