-log = require 'winston'
+winston = require 'winston'
 require './projRequire'
 
-# TOGO: Where should this go?
-do (setupGlobals = this) ->
+module.exports.startServer = ->
+  
+  #- setup globals
   GLOBAL._ = require 'lodash'
   _.str = require 'underscore.string'
   _.mixin _.str.exports()
   global.tv4 = require 'tv4' # required for TreemaUtils to work
-
-module.exports.startServer = ->
+  
   
   #- setup logging
-  log.remove(log.transports.Console)
-  log.add(log.transports.Console,
+  winston.remove(winston.transports.Console)
+  winston.add(winston.transports.Console,
     colorize: true,
     timestamp: true
   )
-
+  
   
   #- connect to db
   Grid = require 'gridfs-stream'

  config = projRequire 'server/server-config'

  dbName = config.mongo.db
  address = config.mongo.host + ':' + config.mongo.port
  if config.mongo.username and config.mongo.password
    address = config.mongo.username + ':' + config.mongo.password + '@' + address
  address = "mongodb://#{address}/#{dbName}"
  
  log.info "Connecting to Mongo with connection string #{address}"
  mongoose.connect address
  mongoose.connection.once 'open', -> Grid.gfs = Grid(mongoose.connection.db, mongoose.mongo)
  
  
  #- express creation, config
  express = require 'express'
  app = express()
  app.set('port', config.port)
  app.set('env', if config.isProduction then 'production' else 'development')

  
  #- express middleware

      else if status >= 300 then color = 36
      elapsed = (new Date()) - req._startTime
      elapsedColor = if elapsed < 500 then 90 else 31
      if (status not in [200, 204, 302, 304]) or elapsed > 500
        return "\x1b[90m#{req.method} #{req.originalUrl} \x1b[#{color}m#{res.statusCode} \x1b[#{elapsedColor}m#{elapsed}ms\x1b[0m"
      null
    
    express.logger.format 'prod', productionLogging
    app.use express.logger('prod')
    app.use express.compress()
  else
    app.use express.logger('dev')

  path = require 'path'
  useragent = require 'express-useragent'
  app.use(express.static(path.join(__dirname, '../public')))
  app.use(useragent.express())

  app.use express.favicon()
  app.use express.cookieParser(config.cookie_secret)
  app.use express.bodyParser()
  app.use express.methodOverride()
  app.use express.cookieSession({secret:'2EqPfxTEqUtRXVfZygLR'})


  #- passport middlware
