// Generated by CoffeeScript 1.6.3
/*
  HTTPster
  Copyright(c) 2010 Simeon Bateman <simeon@simb.net>
  MIT Licensed
*/


(function() {
  var cors, env, exec, express, fav, fs, pack, path, port, program, serveIndex, startDefaultServer, useCompress, useCors, usePushstate, _ref, _ref1, _ref2, _ref3, _ref4;

  fs = require('fs');

  program = require('commander');

  exec = require('child_process').exec;

  express = require('express');

  serveIndex = require('serve-index');

  cors = require('cors');

  path = "./";

  port = void 0;

  fav = require('./fav');

  pack = require('../package.json');

  env = require('node-env-file');

  program.version(pack.version).option('-p, --port <port>', 'Port for server to run on - defaults to 3333').option('-d, --dir [path]', 'Server directory - defaults to ./').option('-z, --compress', 'Add support for compression').option('-s, --pushstate', 'Add support for HTML5 pushstate').option('-e, --env', 'Add support for setting environmental variables from .env file').option('-b, --basic_auth', 'Add support for basic auth security. Uses environmental variables HTTPSTER_AUTH_USER and HTTPSTER_AUTH_PASS to authenticate').option('-c, --cors', 'Add cors support').parse(process.argv);

  if (program.env) {
    env(fs.realpathSync(path) + "/.env");
  }

  port = (_ref = program.port) != null ? _ref : 3333;

  path = (_ref1 = program.dir) != null ? _ref1 : fs.realpathSync(path);

  useCompress = (_ref2 = program.compress) != null ? _ref2 : false;

  useCors = (_ref3 = program.cors) != null ? _ref3 : false;

  usePushstate = (_ref4 = program.pushstate) != null ? _ref4 : false;

  if (program.basic_auth) {
    if (process.env.HTTPSTER_AUTH_USER == null) {
      throw 'HTTPSTER Basic Authentication Enabled but no HTTPSTER_AUTH_USER environmental variable was found';
    }
    if (process.env.HTTPSTER_AUTH_PASS == null) {
      throw 'HTTPSTER Basic Authentication Enabled but no HTTPSTER_AUTH_PASS environmental variable was found';
    }
  }

  startDefaultServer = function(port, path) {
    var app;
    app = express();
    if (useCompress === true) {
      app.use(express.compress());
    }
    app.use(fav(path));
    if (program.basic_auth) {
      app.use(express.basicAuth(process.env.HTTPSTER_AUTH_USER, process.env.HTTPSTER_AUTH_PASS));
    }
    if (useCors) {
      app.use(cors());
    }
    app.use(express["static"](path));
    app.use(serveIndex(path));
    app.use(express.logger({
      format: "dev"
    }));
    app.use(express.errorHandler({
      dumpExceptions: true,
      showStack: true
    }));
    if (usePushstate === true) {
      app.all("/*", function(req, res) {
        res.sendfile("index.html", {
          root: path
        });
      });
    }
    app.configure('production', function() {
      return app.use(express.staticCache());
    });
    app.listen(parseInt(port, 10));
    return app;
  };

  console.log("Starting HTTPster v%s on port %j from %s", pack.version, port, path);

  startDefaultServer(port, path);

}).call(this);
