var acetate = require('./lib/site');
var browserSync = require('browser-sync');
var _ = require('lodash');
var chokidar = require('chokidar');
var path = require('path');

module.exports = function (options) {
  options = _.defaults(options, {
    mode: 'build',
    config: 'acetate.conf.js',
    src: 'src',
    dest: 'build',
    root: process.cwd(),
    log: 'info',
    host: 'localhost',
    port: 8000,
    server: {},
    args: {}
  });

  var site = acetate(options);
  var index;
  var server;
  var fileWatcher;
  var configWatcher;

  function action () {
    if (options.mode === 'server') {
      reload();
    }

    if (options.mode === 'watch') {
      site.build();
    }
  }

  function buildIndex (callback) {
    site.debug('server', 'rebuilding page index');
    site.runExtensions(function () {
      index = _.indexBy(site.pages, function (page) {
        return (page.url[0] !== '/') ? '/' + page.url : page.url;
      });

      if (callback) {
        callback();
      }
    });
  }

  function pageBuilder (request, response, next) {
    if (index[request.url] && index[request.url].dirty) {
      site.verbose('server', 'request recived for %s', request.url);
      index[request.url].build(function () {
        next();
      });
    } else {
      next();
    }
  }

  function startServer () {
    site.info('server', 'starting server');

    buildIndex(function () {
      server = browserSync.create();

      server.emitter.on('init', function () {
        site.emit('server:ready');
      });

      var serverOptions = _.clone(site.options.server);

      delete serverOptions.proxy;
      serverOptions.logLevel = (options.log === 'debug' || options.log === 'silent') ? options.log : 'info';
      serverOptions.server = path.join(site.root, site.dest);
      serverOptions.files = path.join(site.root, site.dest, '**/*');
      serverOptions.port = serverOptions.port || options.port;
      serverOptions.host = serverOptions.host || options.host;
      serverOptions.open = serverOptions.open || options.open;
      serverOptions.middleware = serverOptions.middleware || [];
      serverOptions.middleware.push(pageBuilder);

      server.init(serverOptions);

      site.emit('server:start');
    });
  }

  function startConfigWatcher () {
    configWatcher = chokidar.watch(path.join(process.cwd(), options.config), {
      ignoreInitial: true
    }).on('change', function () {
      site.success('watcher', 'config file changed, restarting Acetate');
      var newSite = acetate(options);
      _.extend(site, {
        reload: reload,
        cleanup: cleanup
      });
      site.emit('restart', newSite);
      site = newSite;
      site.once('load', function () {
        action();
      });
    });
  }

  function startFileWatcher () {
    fileWatcher = chokidar.watch(site.sources, {
      ignoreInitial: true
    });

    fileWatcher.on('change', changed);
    fileWatcher.on('add', added);
    fileWatcher.on('unlink', deleted);
    fileWatcher.on('ready', function () {
      site.emit('watcher:ready');
    });

    site.emit('watcher:start');
  }

  function reload () {
    buildIndex(function () {
      site.info('server', 'refreshing pages');
      server.reload(_(site.pages).collect('dirty').collect('url').value());
    });
  }

  function changed (filepath) {
    site.info('watcher', '%s changed', filepath.replace(process.cwd() + path.sep, ''));
    invalidateNunjucksCache(filepath);
    removeOldPage(filepath);
    loadNewPage(filepath);
  }

  function deleted (filepath) {
    site.info('watcher', '%s deleted', filepath.replace(process.cwd() + path.sep, ''));
    invalidateNunjucksCache(filepath);
    removeOldPage(filepath);
  }

  function added (filepath) {
    site.info('watcher', '%s added', filepath.replace(process.cwd() + path.sep, ''));
    loadNewPage(filepath);
  }

  function removeOldPage (filepath) {
    var page = _.remove(site.pages, {fullpath: filepath})[0];

    if (page) {
      site.verbose('watcher', 'removing %s', page.dest);
      page.clean();
    }
  }

  function loadNewPage (filepath) {
    var relativepath = filepath.replace(process.cwd() + path.sep, '');
    if (path.basename(filepath)[0] === '_') {
      invalidateNunjucksCache(filepath);
      _.each(site.pages, function (page) {
        page.dirty = true;
      });
      action();
    } else {
      site.loadPage(filepath, function (error, page) {
        if (error) {
          site('watcher', 'error loading %s - %s', error);
          return;
        }

        site.info('watcher', 'rebuilding %s', relativepath);
        site.pages.push(page);
        action();
      });
    }
  }

  function invalidateNunjucksCache (filepath) {
    var name = filepath.replace(path.join(site.root, site.src) + path.sep, '').replace(path.extname(filepath), '');
    site.debug('nunjucks', 'clearing %s from the template cache', name);
    site.nunjucks.loaders[0].emit('update', name);
  }

  function cleanup () {
    if (fileWatcher) {
      fileWatcher.close();
    }

    if (configWatcher) {
      configWatcher.close();
    }

    if (server) {
      server.exit();
    }

    site.emit('cleanup');
  }

  if (options.mode === 'server' || options.mode === 'watch') {
    process.on('SIGINT', cleanup);
  }

  site.once('load', function () {
    if (options.mode === 'build') {
      site.build();
    }

    if (options.mode === 'server') {
      startFileWatcher();
      startConfigWatcher();
      startServer();
    }

    if (options.mode === 'watch') {
      site.build(function () {
        startFileWatcher();
        startConfigWatcher();
      });
    }
  });

  _.extend(site, {
    reload: reload,
    cleanup: cleanup
  });

  return site;
};

@@ -163,16 +163,42 @@
 'path');
+%0Avar url = require('url');
 %0A%0Amodule
@@ -1090,154 +1090,404 @@
     
-if (index%5Brequest.url%5D && index%5Brequest.url%5D.dirty) %7B%0A      site.verbose('server', 'request recived for %25s', request.url);%0A  
+site.verbose('server', 'request recived for %25s', request.url);%0A%0A    if(request.method !==  'GET' ) %7B%0A      next();%0A      return;%0A    %7D%0A%0A    var pathname = url.parse(request.url).pathname;%0A    var ext = path.extname(pathname);%0A%0A    if (!ext && pathname%5Bpathname.length - 1%5D !== '/') %7B%0A      pathname = pathname + '/';%0A    %7D%0A%0A
     
+if (
 index%5B
-request.url
+pathname%5D && index%5Bpathname%5D.dirty) %7B%0A      index%5Bpathname
 %5D.bu

fix serving pages with no trailing slash