text: /**
   * licensify:
   *   Browserify plugin to prepend license header to your bundle
   *
   * https://github.com/twada/licensify
   *
   * Copyright (c) 2014-2015 Takuto Wada
   * Licensed under the MIT license.
   *   http://twada.mit-license.org/2014-2015
   */
  'use strict';
  
  var typeName = require('type-name');
  var through = require('through2');
  var props = [
      'license',
      'licenses',
      'author',
      'maintainers',
      'contributors',
      'homepage',
      'version'
  ];
  
  function extractLicense (pkg, summary) {
      switch (typeName(pkg.license)) {
      case 'string':
          summary.license = pkg.license;
          break;
      case 'Object':
          summary.license = pkg.license.type;
          break;
      }
  }
  
  function extractLicenses (pkg, summary) {
      if (typeName(pkg.licenses) === 'Array') {
          summary.licenses = pkg.licenses.map(function (license) {
              return license.type;
          }).join(', ');
      }
  }
  
  function displayName (person) {
      switch (typeName(person)) {
      case 'string':
          return person;
      case 'Object':
          if (person.email) {
              return person.name + ' <' + person.email + '>';
          }
          return person.name;
      }
      return 'NO AUTHOR!';
  }
  
  function extractAuthor (pkg, summary) {
      if (pkg.author) {
          summary.author = displayName(pkg.author);
      }
  }
  
  function extractMaintainers (pkg, summary) {
      if (typeName(pkg.maintainers) === 'Array') {
          summary.maintainers = pkg.maintainers.map(function (person) {
              return displayName(person);
          }).join(', ');
      }
  }
  
  function extractContributors (pkg, summary) {
      if (typeName(pkg.contributors) === 'Array') {
          summary.contributors = pkg.contributors.map(function (person) {
              return displayName(person);
          }).join(', ');
      }
  }
  
  function extractHomepage (pkg, summary) {
      if (pkg.homepage) {
          summary.homepage = pkg.homepage;
      }
  }
  
  function extractVersion (pkg, summary) {
      if (pkg.version) {
          summary.version = pkg.version;
      }
  }
  
  function extract (pkg) {
      var summary = {};
      summary.name = pkg.name;
      extractLicense(pkg, summary);
      extractLicenses(pkg, summary);
      extractAuthor(pkg, summary);
      extractMaintainers(pkg, summary);
      extractContributors(pkg, summary);
      extractHomepage(pkg, summary);
      extractVersion(pkg, summary);
      return summary;
  }
  
  function createEachHeader(pkg) {
      var header = '';
      header += ' * ' + pkg.name + ':\n';
      props.forEach(function (prop) {
          if (pkg[prop]) {
              header += ' *   ' + prop + ': ' + pkg[prop] + '\n';
          }
      });
      header += ' * \n';
      return header;
  }
  
  function createLicenseHeader (mainPkg, licenses) {
      var header = '';
      header += '/**\n';
      header += ' * Modules in this bundle\n';
      header += ' * @license\n';
      header += ' * \n';
      header += createEachHeader(mainPkg);
      var keys = Object.keys(licenses);
      keys.sort();
      keys.forEach(function (key) {
          header += createEachHeader(licenses[key]);
      });
      header += ' * This header is generated by licensify (https://github.com/twada/licensify)\n';
      header += ' */\n';
      return header;
  }
  
  module.exports = function licensify (b, opts) {
      var licenses = {};
      var scannedPkg = [];
      var mainPkg;
  
      function alreadyScanned (pkg) {
          return scannedPkg.some(function (scanned) {
              return pkg.name === scanned;
          });
      }
  
      b.on('package', function (pkg) {
          if (alreadyScanned(pkg)) {
              return;
          }
  
          if (!opts.scanBrowser || !pkg.browser || typeName(pkg.browser) === 'string') {
              if (!mainPkg) {
                  mainPkg = extract(pkg);
              } else {
                  licenses[pkg.name] = extract(pkg);
              }
              scannedPkg.push(pkg.name);
              return;
          }
  
          // If this pkg has "browser" field, licensify scans the field and extracts informations.
          // see https://github.com/substack/browserify-handbook#browser-field
          Object.keys(pkg.browser).forEach(function (key) {
              var subPkgPath = pkg.__dirname + '/node_modules/' + key + '/package.json';
              try {
                  var subPkg = require(subPkgPath);
              } catch (e) {
                  return;
              }
              licenses[subPkg.name] = extract(subPkg);
              scannedPkg.push(subPkg.name);
          });
  
          if (!mainPkg) {
              mainPkg = extract(pkg);
          } else {
              licenses[pkg.name] = extract(pkg);
          }
          scannedPkg.push(pkg.name);
      });
  
      b.on('bundle', function () {
          var first = true;
          b.pipeline.get('wrap').push(through.obj(function (buf, enc, next) {
              if (first) {
                  this.push(new Buffer(createLicenseHeader(mainPkg, licenses)));
                  first = false;
              }
              this.push(buf);
              next();
          }));
      });
  };
  