'use strict';
var cmd = require("cmd-exec").init();
var async = require("async");
var express = require('express');
var app = express();
var stripAnsi = require('strip-ansi');


app.get('/status', function(req, res) {
  res.type('text/json');
  cmd.exec('arkmanager status')
  .then(function(result){
    var status = stripAnsi(result.message);
    status = status.trim();
    status = status.split('\n');
    status.forEach(function( property, i ){
       property = property.split(':');
       property[0] = property[0].toCamelCase();
       property.forEach(function( item, j ) {
          item = item.trim();
          property[j] = item;
       });
       status[i] = property;
    });
    res.send(JSON.stringify(status));
  })
  .fail(function(err){
    console.log(err.message);
  })
  .done(function(){
    console.log("successfully returned status to a client");
  });
});

app.get('/log', function(req, res) {
  res.type('text/json');
  cmd.exec('cat /var/log/arktools/arkserver.log')
  .then(function(result){
    var logs = stripAnsi(result.message);
    logs = decodeURIComponent(logs);
    logs = logs.replace(/\\/g,"/");
    logs = logs.split('\n');
    res.send(JSON.stringify(logs));
  })
  .fail(function(err){
    console.log(err.message);
  })
  .done(function(){
    console.log("successfully returned log to a client");
  });
});

app.get('/checkupdate', function(req, res) {
  res.type('text/json');
  cmd.exec('arkmanager checkupdate')
  .then(function(result){
    var returnMsg = stripAnsi(result.message);
    returnMsg = decodeURIComponent(returnMsg);
    returnMsg = stripAnsi(returnMsg);
    returnMsg = returnMsg.trim();
    returnMsg = returnMsg.split('\n');
    returnMsg.splice(0,1); //remove the 'querying steam database for latest version...' message
    returnMsg.splice(returnMsg.length -1, 1); //remove the 'your server is up to date!' message
    returnMsg.forEach(function( property, i ){
       property = property.split(':');
       property[0] = property[0].toCamelCase();
       property.forEach(function( item, j ) {
          item = item.trim();
          property[j] = item;
       });
       returnMsg[i] = property;
    });
    res.send(JSON.stringify(returnMsg));
  })
  .fail(function(err){
    console.log(err.message);
  })
  .done(function(){
    console.log("successfully returned checkupdate to a client");
  });
});

String.prototype.toCamelCase = function() {
      return this
          .replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
          .replace(/\s/g, '')
          .replace(/^(.)/, function($1) { return $1.toLowerCase(); });
};

console.log('listening on port ' + (process.env.PORT || 4730));
app.listen(process.env.PORT || 4730);

@@ -1,18 +1,4 @@
-'use strict';%0A
 var 
@@ -201,36 +201,43 @@
 ) %7B%0A  res.type('
-text
+application
 /json');%0A  cmd.e
@@ -279,32 +279,57 @@
 nction(result)%7B%0A
+    var returnData = %5B%5D;%0A
     var status =
@@ -676,28 +676,82 @@
     
-status%5Bi%5D = property
+var o = %7B%7D;%0A       o%5Bproperty%5B0%5D%5D = property%5B1%5D;%0A       returnData.push(o)
 ;%0A  
@@ -773,30 +773,18 @@
 end(
-JSON.stringify(status)
+returnData
 );%0A 
@@ -2184,24 +2184,80 @@
 %0A       %7D);%0A
+       var o = %7B%7D;%0A       o%5Bproperty%5B0%5D%5D = property%5B1%5D;%0A
        retur
@@ -2266,24 +2266,17 @@
 sg%5Bi%5D = 
-property
+o
 ;%0A    %7D)

update /status and /log to return objects instead of arrays