var dispatch = require('dispatch');
var path = require('path');
var fs = require('fs');
var _ = require('underscore');

var plugins = require('../plugins');

/* jshint unused:false*/

module.exports = function (config) {
  var names = plugins.getPluginNames(config);

  var paths = names.map(function (name) {
    return path.resolve(
      config.plugins[name].path, 'hoodie.' + name + '.js'
    );
  });

  var bufferToString = function (buf) {
    return buf.toString();
  };

  var readString = _.compose(bufferToString, function (filename) {
    return fs.readFileSync(filename);
  });

  var extensions = paths.filter(fs.existsSync).map(readString);
  var hoodie_dir = path.resolve(__dirname, '../../node_modules/hoodie/dist');
  var base = readString(hoodie_dir + '/hoodie.js');
  var hoodiejs = base + extensions.join('\n');

  return dispatch({
    '/_api/_files/hoodie.js': function (req, res, next) {
      res.writeHead(200, {'Content-Type': 'application/javascript'});
      res.end(hoodiejs);
    }
  });

};

var dispatch = require('dispatch');
var path = require('path');
var fs = require('fs');
var _ = require('underscore');

var plugins = require('../plugins');

/* jshint unused:false*/

module.exports = function (config) {
  var names = plugins.getPluginNames(config);

  var paths = names.map(function (name) {
    return path.resolve(
      config.plugins[name].path, 'hoodie.' + name + '.js'
    );
  });

  var bufferToString = function (buf) {
    return buf.toString();
  };

  var readString = _.compose(bufferToString, function (filename) {
    return fs.readFileSync(filename);
  });

  var extensions = paths.filter(fs.existsSync).map(readString);
  var hoodie_dir = path.resolve(__dirname, '../../node_modules/hoodie/dist');

  // if the user's project has defined a hoodie.js dependency,
  // then favour that over our own dependency.
  if(config.app.dependencies.hoodie) {
    hoodie_dir = path.resolve(config.project_dir, 'node_modules/hoodie/dist');
    console.log('loading alternative hoodie.js from %s', hoodie_dir);
  }

  var base = readString(hoodie_dir + '/hoodie.js');
  var hoodiejs = base + extensions.join('\n');

  return dispatch({
    '/_api/_files/hoodie.js': function (req, res, next) {
      res.writeHead(200, {'Content-Type': 'application/javascript'});
      res.end(hoodiejs);
    }
  });

};

Allow apps to specify an alternative hoodie.js dependency
Allow apps to specify an alternative hoodie.js dependency

If you project needs a hoodie.js that is different from the default, you
can now specify an alternate dependency source for `hoodie`.

e.g.:

```
{
  "name": "foo",
  "version": "1.0.1",
  "type": "app",
  "dependencies": {
    "hoodie-server": "~0.8.12",
    "hoodie-plugin-appconfig": "~0.1.0",
    "hoodie-plugin-email": "~0.1.1",
    "hoodie-plugin-users": "0.0.9",
    "hoodie": "git://github.com/hoodiehq/hoodie.js.git"
  },
  "subdomain": "hoodie-foo",
  "domains": [
    "admin.hoodie-foo.jit.su",
    "couch.hoodie-foo.jit.su"
  ],
  "scripts": {
    "start": "node node_modules/hoodie-server/bin/start"
  },
  "hoodie": {
    "plugins": [
      "hoodie-plugin-appconfig",
      "hoodie-plugin-email",
      "hoodie-plugin-users"
    ]
  }
}
```

Closes #164.

* * *

This commit was sponsored by The Hoodie Firm.
You can hire The Hoodie Firm:
http://go.hood.ie/thehoodiefirm
