var express = require('express');

var _ = require('./util');
var app = express();

var builder = require('./server-builder');

var db = require('./server-db');

app.configure(function(){
  app.use(express.bodyParser());
  app.use(express.cookieParser());
});

function errorCallback(res) {
  return function(err) {
    res.send(500, err);
  }
}

function sendResponse(res, promise) {
  promise.then(function(a) {
    res.send(a);
  }, errorCallback(res));
}


app.get('/', function(req, res) {
  res.send('hello world');
});

app.post('/builds', function(req, res) {
  var d = req.body;
  var buildId;
  db.builds.add('building', 'Starting Build', d.repoUrl, d.deployKey, d.refspec).then(function(build) {
    buildId = build.id;
    res.send(build);
    builder.build(build).then(function() {
      db.builds.setStatus('passed', 'Build Passed').then(function() {
        console.log('saved success status');
      }, function() {
        console.log('error saving success status');
      });
    }, function(err) {
      db.builds.setStatus('failed', err).then(function() {
        console.log('saved err status ', err);
      }, function() {
        console.log('error saving err status');
      });
    }, function(status) {
      db.builds.setStatus(buildId, status.status, status.message).then(function() {
        console.log('saved status ', status.message);
      }, function() {
        console.log('error saving status');
      });
    });
  }, errorCallback(res));
});

app.get('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.get(req.params.id));
});

app.del('/builds/:id', function(req, res) {
  sendResponse(res, db.builds.remove(req.params.id));
});

app.get('/builds', function(req, res) {
  sendResponse(res, db.builds.list());
});

app.get('/instances', function(req, res) {
  sendResponse(res, db.instances.list());
});

var httpsKey = _.fs.readFileSync('/root/server.key');
var httpsCert = _.fs.readFileSync('/root/server.crt');

var httpsOpts = {
  key: httpsKey,
  cert: httpsCert,
  ca: [ httpsCert ],
  rejectUnauthorized: true
};

db.start().then(function(){

  _.https.createServer(httpsOpts, app).listen(8888, function(err) {

    if(err) return console.log('Error! ', err);

    console.log('Server started on 8888');

  });

}, function(err){
  console.log('Error starting db:', err);
});
@@ -581,16 +581,106 @@
 q.body;%0A
+  var hasErrorHappened = false;%0A  var hasPassHappened = false;%0A  var isNotifying = false;%0A
   var bu
@@ -874,24 +874,86 @@
 unction() %7B%0A
+      hasPassHappened = true;%0A      function reportPass() %7B%0A  
       db.bui
@@ -1010,32 +1010,34 @@
 ion() %7B%0A        
+ 
+ 
 console.log('sav
@@ -1055,32 +1055,34 @@
 status');%0A      
+  
 %7D, function() %7B%0A
@@ -1081,32 +1081,34 @@
 ion() %7B%0A        
+ 
+ 
 console.log('err
@@ -1137,32 +1137,34 @@
 us');%0A      
+  
 %7D);%0A    
 %7D, function(
@@ -1155,27 +1155,183 @@
     
-%7D, function(err) %7B%0A
+  %7D);%0A      if (isNotifying) afterNotify = reportPass;%0A      else reportPass();%0A    %7D, function(err) %7B%0A      hasErrorHappened = true;%0A      function reportFailure() %7B      %0A  
     
@@ -1385,32 +1385,34 @@
 ion() %7B%0A        
+ 
+ 
 console.log('sav
@@ -1432,32 +1432,34 @@
  ', err);%0A      
+  
 %7D, function() %7B%0A
@@ -1458,32 +1458,34 @@
 ion() %7B%0A        
+ 
+ 
 console.log('err
@@ -1514,24 +1514,26 @@
 ;%0A      
+  
 %7D);%0A    
 %7D, funct
@@ -1528,30 +1528,202 @@
     
-%7D, function(status) %7B%0A
+  %7D%0A      if (isNotifying) afterNotify = reportFailure;%0A      else reportFailure();%0A    %7D, function(status) %7B%0A      isNotifying = true;%0A      if(!hasErrorHappened && !hasPassHappened) %7B      %0A  
     
@@ -1794,32 +1794,106 @@
 en(function() %7B%0A
+          isNotifying = false;%0A          if(afterNotify) afterNotify();%0A  
         console.
@@ -1932,24 +1932,26 @@
 age);%0A      
+  
 %7D, function(
@@ -1946,32 +1946,106 @@
 %7D, function() %7B%0A
+          isNotifying = false;%0A          if(afterNotify) afterNotify();%0A  
         console.
@@ -2074,28 +2074,38 @@
 us');%0A      
+  
 %7D);%0A
+      %7D%0A
     %7D);%0A  %7D,

fix race condition with notification