// Generated by CoffeeScript 1.6.3
(function() {
  var crypto, favicon, fs, md5;

  fs = require("fs");

  crypto = require("crypto");

  module.exports = favicon = function(path, options) {
    var icon, maxAge;
    options = options || {};
    maxAge = options.maxAge || 86400000;
    icon = void 0;
    return favicon = function(req, res, next) {
      if ("/favicon.ico" === req.url) {
        if (icon) {
          res.writeHead(200, icon.headers);
          return res.end(icon.body);
        } else {
          if (!fs.existsSync(path + "/favicon.ico")) {
            path = __dirname + "/../bin";
          }
          return fs.readFile(path + "/favicon.ico", function(err, buf) {
            if (err) {
              return next(err);
            }
            icon = {
              headers: {
                "Content-Type": "image/x-icon",
                "Content-Length": buf.length,
                ETag: "\"" + md5(buf) + "\"",
                "Cache-Control": "public, max-age=" + (maxAge / 1000)
              },
              body: buf
            };
            res.writeHead(200, icon.headers);
            return res.end(icon.body);
          });
        }
      } else {
        return next();
      }
    };
  };

  md5 = function(str, encoding) {
    return crypto.createHash("md5").update(str).digest(encoding || "hex");
  };

}).call(this);
