var _ = require('lodash')

var Chairo = require ( 'chairo' )
var Hapi = require ( 'hapi' )
var Bell = require('bell')
var Hapi_Cookie = require('hapi-auth-cookie')

exports.init = function (options, done) {
  var server = new Hapi.Server ()
  server.connection()

  server.register([Hapi_Cookie, Bell, {
    register: Chairo,
    options: {
      seneca_plugins: {
        web: false
      },
      webPlugin: require('seneca-web')
    }
  }], function ( err ) {
      var si = server.seneca

      si.use('user')
      si.use(
        require('seneca-auth'),
        _.extend(
          {
            secure: true,
            restrict: '/api',
            server: 'hapi',
            strategies: [
              {
                provider: 'local'
              }
            ]
          }, options || {}))
    done(null, server)
    })
}

exports.checkCookie = function (res) {
  var cookie = res.headers['set-cookie'][0]
  cookie = cookie.match(/(?:[^\x00-\x20\(\)<>@\,;\:\\"\/\[\]\?\=\{\}\x7F]+)\s*=\s*(?:([^\x00-\x20\"\,\;\\\x7F]*))/)[1]
  return cookie
}

@@ -44,19 +44,17 @@
 uire
- ( 
+(
 'chairo'
  )%0Av
@@ -49,17 +49,16 @@
 'chairo'
- 
 )%0Avar Ha
@@ -73,18 +73,15 @@
 uire
- ( 
+(
 'hapi'
- 
 )%0Ava
@@ -224,17 +224,16 @@
 i.Server
- 
 ()%0A  ser
@@ -392,14 +392,8 @@
  web
-Plugin
 : re
@@ -438,19 +438,15 @@
 on (
- 
 err
- 
 ) %7B%0A
-  
     
@@ -469,26 +469,24 @@
 seneca%0A%0A    
-  
 si.use('user
@@ -496,18 +496,16 @@
     
- 
- 
 si.use(%0A
     
@@ -500,18 +500,16 @@
 si.use(%0A
-  
       re
@@ -534,30 +534,8 @@
 '),%0A
-        _.extend(%0A    
     
@@ -546,20 +546,16 @@
         
-    
 secure: 
@@ -568,20 +568,16 @@
         
-  
-  
 restrict
@@ -596,163 +596,9 @@
     
-      server: 'hapi',%0A            strategies: %5B%0A              %7B%0A                provider: 'local'%0A              %7D%0A            %5D%0A          %7D, options %7C%7C %7B%7D)
+%7D
 )%0A  
@@ -618,18 +618,16 @@
 server)%0A
-  
   %7D)%0A%7D%0A%0A

fix for Hapi init