var http = require('http');
var fs = require('fs');
var path = require('path');
var chalk = require('chalk');
var blacklist = require('../packager/blacklist.js');
var ReactPackager = require('../packager/react-packager');

var OUT_PATH = 'ios/main.jsbundle';
var URL_PATH = '/index.ios.bundle?dev=';

function getBundle(flags) {

  var outPath = flags.out ? flags.out : OUT_PATH;

  var projectRoots = [path.resolve(__dirname, '../../..')];
  if (flags.root) {
    projectRoots.push(path.resolve(flags.root));
  }

  var assetRoots = [path.resolve(__dirname, '../../..')];
  if (flags.assetRoots) {
    assetRoots = assetRoots.concat(flags.assetRoots.split(",").map(function(root) {
      return path.resolve(root);
    }));
  }


  var options = {
    projectRoots: projectRoots,
    transformModulePath: require.resolve('../packager/transformer.js'),
    assetRoots: assetRoots,
    cacheVersion: '2',
    blacklistRE: blacklist('ios'),
  };

  var url = flags.url ? flags.url.replace(/\.js$/i, '.bundle?dev=') : URL_PATH;
  url = url.match(/^\//) ? url : '/' + url;
  url += flags.dev;

  console.log('Building package...');
  ReactPackager.buildPackageFromUrl(options, url)
    .done(function(bundle) {
      console.log('Build complete');
      fs.writeFile(outPath, bundle.getSource({
        inlineSourceMap: false,
        minify: flags.minify
      }), function(err) {
        if (err) {
          console.log(chalk.red('Error saving bundle to disk'));
          throw err;
        } else {
          console.log('Successfully saved bundle to ' + outPath);
        }
      });
    });
}

function showHelp() {
  console.log([
    'Usage: react-native bundle [options]',
    '',
    'Options:',
    '  --dev\t\tsets DEV flag to true',
    '  --minify\tminify js bundle',
    '  --root\t\tadd another root(s) to be used in bundling in this project',
    '  --assetRoots\t\tspecify the root directories of app assets',
    '  --out\t\tspecify the output file',
    '  --url\t\tspecify the bundle file url',
  ].join('\n'));
  process.exit(1);
}

module.exports = {
  init: function(args) {
    var flags = {
      help: args.indexOf('--help') !== -1,
      dev: args.indexOf('--dev') !== -1,
      minify: args.indexOf('--minify') !== -1,
      root: args.indexOf('--root') !== -1 ? args[args.indexOf('--root') + 1] : false,
      assetRoots: args.indexOf('--assetRoots') !== -1 ? args[args.indexOf('--assetRoots') + 1] : false,
      out: args.indexOf('--out') !== -1 ? args[args.indexOf('--out') + 1] : false,
      url: args.indexOf('--url') !== -1 ? args[args.indexOf('--url') + 1] : false,
    }

    if (flags.help) {
      showHelp();
    } else {
      getBundle(flags);
    }
  }
}

@@ -230,16 +230,88 @@
 T_PATH =
+ %7B%0A  android: 'android/app/src/main/assets/index.android.bundle',%0A  ios:
  'ios/ma
@@ -322,16 +322,18 @@
 sbundle'
+%0A%7D
 ;%0Avar UR
@@ -340,16 +340,83 @@
 L_PATH =
+ %7B%0A  android: '/index.android.bundle?platform=android&dev=',%0A  ios:
  '/index
@@ -427,21 +427,36 @@
 .bundle?
+platform=ios&
 dev='
+%0A%7D
 ;%0A%0Afunct
@@ -478,16 +478,73 @@
 lags) %7B%0A
+  var platform = flags.platform ? flags.platform : 'ios';
 %0A  var o
@@ -584,16 +584,26 @@
 OUT_PATH
+%5Bplatform%5D
 ;%0A%0A  var
@@ -1147,21 +1147,24 @@
 acklist(
-'ios'
+platform
 ),%0A  %7D;%0A
@@ -1242,16 +1242,26 @@
 URL_PATH
+%5Bplatform%5D
 ;%0A  url 
@@ -2242,16 +2242,73 @@
 e url',%0A
+    '  --platform%5Ct%5Ctspecify the platform(android/ios)',%0A
   %5D.join
@@ -2613,24 +2613,122 @@
 1%5D : false,%0A
+      platform: args.indexOf('--platform') !== -1 ? args%5Bargs.indexOf('--platform') + 1%5D : false,%0A
       assetR

let 'react-native bundle' support platform option.