var path = require('path');
var os = require('os');
var util = require('util');
var cp = require('child_process');
var fs = require('fs');
var colors = require('colors/safe');
var fse = require('fs-extra2');
var getPluginPaths = require('../lib/plugins/module-paths').getPaths;

var MAX_RULES_LEN = 1024 * 16;
var CHECK_RUNNING_CMD = process.platform === 'win32' ? 
  'tasklist /fi "PID eq %s" | findstr /i "node.exe"'
  : 'ps -f -p %s | grep "node"';

function getHomedir() {
  //默认设置为`~`，防止Linux在开机启动时Node无法获取homedir
  return (typeof os.homedir == 'function' ? os.homedir() :
    process.env[process.platform == 'win32' ? 'USERPROFILE' : 'HOME']) || '~';
}

function isRunning(pid, callback) {
  pid ? cp.exec(util.format(CHECK_RUNNING_CMD, pid), 
    function (err, stdout, stderr) {
      callback(!err && !!stdout.toString().trim());
    }) : callback(false);
}

function showStartWhistleTips() {
  // TODO: xxx
  console.log(colors.red('请先执行 `w2 start` 启动whistle.'));
}

function handleRules(options, filepath, callback) {
  var getRules = require(filepath);
  if (typeof getRules !== 'function') {
    return callback(getRules);
  }
  getRules(options, callback);
}

function getString(str) {
  return typeof str !== 'string' ? '' : str.trim();
}
   
module.exports = function(filepath, storage) {
  var dataDir = path.resolve(getHomedir(), '.startingAppData');
  var configFile = path.join(dataDir, encodeURIComponent('#' + (storage ? storage + '#' : '')));
  if (!fs.existsSync(configFile)) {
    return showStartWhistleTips();
  }
  var pid, options;
  try {
    var config = fse.readJsonSync(configFile);
    options = config.options;
    pid = options && config.pid;
  } catch(e) {}
  isRunning(pid, function(running) {
    if (!running) {
      return showStartWhistleTips();
    }
    filepath = path.resolve(filepath || '.whistle.js');
    var port = options.port > 0 ? options.port : 8899;
    handleRules({
      pluginPaths: getPluginPaths(),
      port: port
    }, filepath, function(result) {
      if (!result) {
        console.log(colors.red('规则名称及内容不能为空.'));
        return;
      }
      var name = getString(result.name);
      if (!name || name.length > 64) {
        // TODO: xxx
        console.log(colors.red('规则名称不能为空且长度不能超过64个字符.'));
        return;
      }
      var rules = getString(result.rules);
      if (rules.length > MAX_RULES_LEN) {
        // TODO: xxx
        console.log(colors.red('规则内容不能为空且长度不能超过16k.'));
        return;
      }
      console.log(colors.green('规则已成功设置到whistle(127.0.0.1:' + port + ').'));
    });
  });
};
@@ -895,26 +895,18 @@
 ips(
-) %7B%0A  // TODO: xxx
+storage) %7B
 %0A  c
@@ -932,33 +932,101 @@
 ed('
-%E8%AF%B7%E5%85%88%E6%89%A7%E8%A1%8C %60w2 start%60 %E5%90%AF%E5%8A%A8
+Please execute %60w2 start' + (storage ? ' -S ' + storage : '')%0A    + '%60 to start 
 whistle
+ first
 .'))
@@ -1583,24 +1583,31 @@
 WhistleTips(
+storage
 );%0A  %7D%0A  var
@@ -1846,16 +1846,23 @@
 tleTips(
+storage
 );%0A    %7D
@@ -2138,19 +2138,38 @@
 ed('
-%E8%A7%84%E5%88%99%E5%90%8D%E7%A7%B0%E5%8F%8A%E5%86%85%E5%AE%B9%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA
+name and rules cannot be empty
 .'))
@@ -2274,37 +2274,16 @@
 %3E 64) %7B%0A
-        // TODO: xxx%0A
         
@@ -2310,28 +2310,71 @@
 ed('
-%E8%A7%84%E5%88%99%E5%90%8D%E7%A7%B0%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%E4%B8%94%E9%95%BF%E5%BA%A6%E4%B8%8D%E8%83%BD%E8%B6%85%E8%BF%8764%E4%B8%AA%E5%AD%97%E7%AC%A6
+name cannot be empty and the length cannot exceed 64 characters
 .'))
@@ -2488,29 +2488,8 @@
 ) %7B%0A
-        // TODO: xxx%0A
     
@@ -2520,23 +2520,57 @@
 ed('
-%E8%A7%84%E5%88%99%E5%86%85%E5%AE%B9%E4%B8%8D%E8%83%BD%E4%B8%BA%E7%A9%BA%E4%B8%94%E9%95%BF%E5%BA%A6%E4%B8%8D%E8%83%BD%E8%B6%85%E8%BF%87
+rules cannot be empty and the size cannot exceed 
 16k.
@@ -2634,24 +2634,9 @@
 en('
-%E8%A7%84%E5%88%99%E5%B7%B2%E6%88%90%E5%8A%9F%E8%AE%BE%E7%BD%AE%E5%88%B0whistle(
+%5B
 127.
@@ -2653,17 +2653,36 @@
 port + '
-)
+%5D Setting successful
 .'));%0A  

add use cmd