import fs from 'fs'
import path from 'path'
import { spawn } from 'child_process'
import crypto from 'crypto'
import router from 'koa-router'

export default router()
.post('/api/hooks', async(ctx, next) => {
    const { fields, headers } = ctx.request
    const signature = getSecret(JSON.stringify(fields))

    if (signature === headers['x-hub-signature'].substr(5)) {
        console.info('Signature matched, restarting server...')
        pullAndUpdate()
    } else {
        console.warn('Signature mismatch')
    }

    ctx.body = {
        success: true
    }
})

// Checks if we're authorized to restart the server
function getSecret(body) {
    const INFERNOJS_SECRET = fs.readFileSync(path.join(__dirname, 'INFERNOJS_SECRET'))
    const secret = process.env.INFERNOJS_SECRET || INFERNOJS_SECRET
    const hash = crypto.createHmac('sha1', secret).update(body).digest('hex')
    return hash
}

// Pulls master from github while our watcher automatically rebuilds the bundle
function pullAndUpdate() {
    execute('cd', ['/www/infernojs'], () => execute('git' ['pull']))
}

// Run commands
function execute(cmd, args, callback) {
    const spawn = require('child_process').spawn;
    const child = spawn(cmd, args);
    let output = '';

    child.stdout.on('data', function(buffer) {
        output += buffer.toString()
    })
    child.stdout.on('end', function() {
        console.log('Command:', cmd, args.join(' '))
        console.log('Output:', output)
        callback && callback()
    })
}

@@ -202,16 +202,57 @@
 t) =%3E %7B%0A
+    await new Promise((resolve) =%3E %7B%0A    
     cons
@@ -287,16 +287,20 @@
 request%0A
+    
     cons
@@ -352,16 +352,20 @@
 ))%0A%0A    
+ 
+   
 if (sign
@@ -414,32 +414,36 @@
 r(5)) %7B%0A        
+   
+ 
 console.info('Si
@@ -490,16 +490,20 @@
         
+    
 pullAndU
@@ -509,17 +509,156 @@
 Update()
-%0A
+.then(() =%3E %7B%0A                ctx.body = %7B%0A                    success: true%0A                %7D%0A                resolve()%0A            %7D)%0A    
     %7D el
@@ -662,16 +662,20 @@
  else %7B%0A
+    
         
@@ -717,15 +717,16 @@
     
-%7D%0A%0A
+  
     
+  
 ctx.
@@ -734,32 +734,40 @@
 ody = %7B%0A        
+      
+  
 success: true%0A  
@@ -763,18 +763,66 @@
 ss: 
-true%0A    %7D
+false%0A            %7D%0A            resolve()%0A        %7D%0A    %7D)
 %0A%7D)%0A
@@ -1264,16 +1264,23 @@
 ) %7B%0A    
+return 
 execute(
@@ -1303,18 +1303,28 @@
 ernojs'%5D
-, 
+)%0A    .then(
 () =%3E ex
@@ -1413,54 +1413,46 @@
     
-const spawn = require('child_process').spawn;%0A
+return new Promise((resolve) =%3E %7B%0A    
     
@@ -1485,14 +1485,17 @@
 rgs)
-;
 %0A
+  
     
+  
 let 
@@ -1509,15 +1509,18 @@
 = ''
-;
 %0A%0A    
+   
+ 
 chil
@@ -1566,16 +1566,20 @@
         
+    
 output +
@@ -1598,27 +1598,35 @@
 tring()%0A    
+ 
-%7D)%0A
+   %7D)%0A    
     child.st
@@ -1653,32 +1653,36 @@
 ion() %7B%0A        
+    
 console.log('Com
@@ -1710,32 +1710,36 @@
 n(' '))%0A        
+  
+  
 console.log('Out
@@ -1765,29 +1765,31 @@
     
-callback && callback(
+    resolve()%0A        %7D
 )%0A  

Fix deploy hook