var st = require('st')
  , http = require('http')
  , path = require('path')
  , url = require('url')
  , js = require('atomify-js')
  , css = require('atomify-css')
  , open = require('open')
  , through = require('through')
  , tinylr = require('tiny-lr-fork')
  , gaze = require('gaze')
  , colors = require('colors')
  , server
  , baseUrl

module.exports = function (args) {

  console.log('')

  var mount = st(args.server.st || { path: process.cwd(), cache: false })
    , parsedUrl = url.parse(args.server.url || '')
    , port = args.server.port || parsedUrl.port || 1337
    , launch = args.server.open
    , path = args.server.path || '/default'
    , lr = args.server.lr

  args.server.url = args.server.url || 'http://localhost:' + port + path
  baseUrl = parsedUrl.protocol + '//' + parsedUrl.hostname

  if (lr) {
    lr = typeof lr === 'boolean' ? {} : lr
    lr.patterns = lr.patterns || lr._ || []
    lr.port = lr.port || 35729
    startLiveReloadServer(lr)
    console.log(('Live reload server listening on port ' + lr.port).grey)
  }

  if (path.charAt(0) !== '/') path = '/' + path
  if (!args.js) args.js = {}
  if (!args.js.alias) args.js.alias = '/' + args.js.entry
  if (!args.css) args.css = {}
  if (!args.css.alias) args.css.alias = '/' + args.css.entry

  http.createServer(function (req, res) {

    switch (req.url.split('?')[0]) {
      case args.js.alias || args.js.entry:
        js(args.js, responder('javascript', res))
        break

      case args.css.alias || args.css.entry:
        css(args.css, responder('css', res))
        break

      case '/default':
        serveDefaultPage(res, args, lr)
        break

      default:
        if (lr && req.url.substr(-5) === '.html') res.filter = injectLiveReloadScript(lr)
        mount(req, res)
        break
    }

  }).listen(port)

  console.log(('Atomify server listening on port ' + port).grey)
  console.log('')

  if (launch) open(args.server.url)
}

function responder (type, res) {
  return function (err, src) {
    if (err) console.log(err);

    if (!res.headersSent) res.setHeader('Content-Type', 'text/' + type)
    res.end(src)
  }
}

function startLiveReloadServer (lr) {
  server = tinylr()
  server.listen(lr.port)

  var patterns = lr.patterns.length ? lr.patterns : ['*.html', '*.js', '*.css']

  gaze(patterns, function() {
    this.on('changed', function (filepath) {
      if (!lr.quiet) console.log(path.relative(process.cwd(), filepath), 'changed'.grey);
      server.changed({ body: { files: filepath } })
    })
  })
}

function injectLiveReloadScript (lr) {
  var buffer = ''
    , tag = '<script src="' + baseUrl + ':' + lr.port + '/livereload.js?snipver=1"></script>\n'

  return through(function (chunk) {
    buffer += chunk.toString()
  }, function () {
    this.queue(buffer.replace('</body>', tag + '</body>'))
    this.queue(null)
  })
}

function serveDefaultPage (res, args, lr) {
  var src = '<html><head>'
  src += '<meta name="viewport" content="initial-scale=1,width=device-width,user-scalable=0,minimal-ui">'
  src += '<title>generated by atomify</title>'
  src += '<link rel="stylesheet" href="CSS">'
  src += '</head><body>'
  src += '<script src="JS"></script>'
  src += '</body></html>'

  src = src.replace('JS', args.js.alias)
  src = src.replace('CSS', args.css.alias)

  res.setHeader('Content-Type', 'text/html')

  var thru = lr ? injectLiveReloadScript(lr) : through()
  thru.pipe(res)
  thru.write(src)
  thru.end('\n')
}

@@ -389,24 +389,96 @@
 le.log('')%0A%0A
+  args.server.url = args.server.url %7C%7C 'http://localhost:1337/default'%0A%0A
   var mount 
@@ -586,14 +586,8 @@
 .url
- %7C%7C ''
 )%0A  
@@ -635,28 +635,18 @@
 port
- %7C%7C 1337
 %0A    , 
-launc
+pat
 h = 
@@ -661,56 +661,30 @@
 ver.
-open%0A    , path = args.server.path %7C%7C '/default'
+path %7C%7C parsedUrl.path
 %0A   
@@ -710,81 +710,40 @@
 .lr%0A
-%0A  args.server.url = args.server.url %7C%7C 'http://localhost:' + port + path
+    , launch = args.server.open%0A
 %0A  b
@@ -1909,31 +1909,43 @@
 h) open(
-args.server.url
+baseUrl + ':' + port + path
 )%0A%7D%0A%0Afun

Fix url parsing and construction