var _ = require('lodash'),
  express = require('express'),
  consolidate = require('consolidate'),
  fs = require('fs'),
  utils = require('./utils');

var app = express();

function findHTML(file, path, cb) {
  file = file || 'index';
  fs.exists(path + file + '.html', function (exists) {
    if (!exists && (/index$/).test(file)) {
      cb('File not found: ' + path + file);
      return;
    }

    if (!exists) {
      findHTML(file + '/index', path, cb);
      return;
    }

    cb(null, file + '.html');
  });
}

exports.run = function (argv) {
  var path = argv._[0],
    engine = argv.e,
    encoding = argv.encoding,
    port = argv.p,
    __swig;

  app.use(express.logger('dev'));
  app.use(app.router);

  if (!consolidate[engine]) {
    utils.error('Template engine', engine, 'does not exist');
    process.exit(1);
  }

  if (engine === 'swig') {
    // Swig requires some extra setup
    __swig = require('swig');
    __swig.init({
      root: path,
      allowErrors: true,
      cache: false
    });
  }

  app.engine('.html', consolidate[engine]);
  app.set('view engine', 'html');
  app.set('views', path);

  app.get('/*', function (req, res) {
    var view = _.first(req.params).replace(/\/$/, '');

    findHTML(view, path, function (err, tpl) {
      if (err) {
        utils.error('View not found for', view);
        return;
      }

      var dataFile = tpl.replace(/\.html$/, 'json');

      fs.readFile(dataFile, encoding, function (err, jsonData) {
        var data = {};

        if (err) {
          utils.warn('No data for file', tpl);
        } else {
          try {
            data = _.extend(data, JSON.parse(jsonData));
          } catch (e) {
            utils.warn('Data not proper JSON', dataFile);
          }
        }

        res.render(tpl, data);
      });

    });
  });

  app.listen(port);
  utils.log('Server listening on port', port);
};

@@ -484,16 +484,14 @@
     
-cb(null,
+file =
  fil
@@ -497,24 +497,110 @@
 le + '.html'
+;%0A%0A    fs.stat(path + file, function (err, stats) %7B%0A      cb(null, file, stats);%0A    %7D
 );%0A  %7D);%0A%7D%0A%0A
@@ -1344,16 +1344,22 @@
 err, tpl
+, stat
 ) %7B%0A    
@@ -1490,16 +1490,17 @@
 tml$/, '
+.
 json');%0A
@@ -1518,16 +1518,23 @@
 eadFile(
+path + 
 dataFile
@@ -1723,20 +1723,20 @@
 .extend(
-d
+st
 at
-a
 , JSON.p

fix path and add stat for server viewing