var express = require("./node_modules/express");
var app = express();
var exec = require("child_process").exec;
var os = require('os');
var serverName = os.hostname();

if (serverName == 'edu2')
  {
  var birtns = "../birtns/o(../birtns)";
  var vartns = "../vartns/o(../vartns)";
  var gtm_dist = "/usr/local/lib/yottadb/r204";
  var gtmgbldir = "../g/mumps.gld";
  var docs_base = "/var/www/html/vista/docs";
  process.env.gtm_dist = gtm_dist;
  process.env.gtmroutines = birtns + " " + vartns + " " + gtm_dist + "/libyottadbutil.so";
  process.env.gtmgbldir = gtmgbldir;
  }
else
  {
  var birtns = "../birtns/o(../birtns)";
  var vartns = "../vartns/o(../vartns)";
  var gtm_dist = "/usr/lib/fis-gtm/V71010";
  var gtmgbldir = "../g/mumps.gld";
  var docs_base = "/var/www/html/vista/docs";
  process.env.gtm_dist = gtm_dist;
  process.env.gtmroutines = birtns + " " + vartns + " " + gtm_dist;
  process.env.gtmgbldir = gtmgbldir;
  }

var server = app.listen(8090,
      function(){ console.log(Date() + ": Server has started."); });

app.use(express.text());

//---------------------- VistA Dictionary Viewer ------------------------------

app.get('/dict/list',
      function (requestresponse)
        { gtm(requestresponse"list^dictionary"); });

app.get('/dict/stan/:ien',
      function (requestresponse)
        { gtm(requestresponse"stan^dictionary " + request.params.ien); });

app.get('/dict/glob/:ien',
      function (requestresponse)
        { gtm(requestresponse"glob^dictionary " + request.params.ien); });

app.get('/dict/def/:ien/:filter',
      function (requestresponse)
        { gtm(requestresponse"def^dictionary "
             + request.params.ien + "^" + request.params.filter); });

//---------------------- FileMan API Tester -----------------------------------

app.post('/fmapil/do',
      function (requestresponse)
        { gtm(requestresponse"en^fmapil " + request.body); });

app.post('/fmapir/do',
      function (requestresponse)
        { gtm(requestresponse"en^fmapir " + request.body); });

//---------------------- HL7 to FHIR Converter --------------------------------

app.post('/HL7FHIRAI/do',
      function (requestresponse)
        { gtm(requestresponse"en^HL7FHIRAI " + request.body); });

//---------------------- ipl --------------------------------------------------

app.get('/patch/ipl',
      function (requestresponse)
        { gtm(requestresponse"view^ipl"); });

app.get('/patch/delimited/:ien',
      function (requestresponse)
        { gtm(requestresponse"delimited^ipl " + request.params.ien); });

//---------------------- Monitor ----------------------------------------------

app.get('/mon/pm2list',
      function (requestresponse)
        { cmd(requestresponse"pm2 list"); });

app.get('/mon/psefcnt',
        function (requestresponse)
        { cmd(requestresponse"ps -ef | wc -l"); });

app.get('/mon/psefgrep',
        function (requestresponse)
        { cmd(requestresponse"ps -ef | grep \'nodejs\\\|pm2\\\|mumps\' | grep -ve \'crhvist\'"); });

app.get('/mon/hitcounts',
      function (requestresponse)
        { gtm(requestresponse"hitcounts^settings"); });

app.get('/mon/hitslast',
      function (requestresponse)
        { gtm(requestresponse"hitslast^settings"); });

app.get('/mon/hitsview',
      function (requestresponse)
        { gtm(requestresponse"hitsview^settings"); });

//---------------------- rpc --------------------------------------------------

app.get('/rpc/list',
      function (requestresponse)
        { gtm(requestresponse"list^rpcw"); });

app.get('/rpc/main/:ien',
      function (requestresponse)
        { gtm(requestresponse"main^rpcw " + request.params.ien); });

//---------------------- surgery ----------------------------------------------

app.get('/surg/list',
      function (requestresponse)
        { gtm(requestresponse"list^surgery"); });

app.get('/surg/patient/:search',
      function (requestresponse)
        { gtm(requestresponse"patient^surgery "
             + request.params.search); });

app.get('/surg/provider/:search',
      function (requestresponse)
        { gtm(requestresponse"provider^surgery "
             + request.params.search); });

app.get('/surg/procedure/:search',
      function (requestresponse)
        { gtm(requestresponse"procedure^surgery "
             + request.params.search); });

app.post('/surg/do',
      function (requestresponse)
        { gtm(requestresponse"do^surgery " + request.body); });

//---------------------- volp -------------------------------------------------

app.get('/vopl/list',
      function (requestresponse)
        { gtm(requestresponse"list^vopl"); });

app.get('/vopl/main/:ien',
      function (requestresponse)
        { gtm(requestresponse"main^vopl " + request.params.ien); });

//---------------------- wbrtn ------------------------------------------------

app.get('/wbrtn/list/:alpha',
      function (requestresponse)
        { gtm(requestresponse"list^wbrtn " + request.params.alpha); });

app.get('/wbrtn/rtn/:rtn',
      function (requestresponse)
        { gtm(requestresponse"rtn^wbrtn " + request.params.rtn); });

//---------------------- Functions --------------------------------------------

// Function to make a command line request.
function cmd(request,response,cmdLine)
  { exec(cmdLine, {maxBuffer1024 * 3000},
      function(errorstdoutstderr)
        { response.writeHead(200, {"Content-Type""text/html"});
          response.write(stdout); response.end(); });}

// Function to run a gtm routine.
function gtm(request,response,cmdLine)
  { exec(gtm_dist + "/mumps -run " + cmdLine, {maxBuffer1024 * 3000},
      function(errorstdoutstderr)
        { response.writeHead(200, {"Content-Type""text/html"});
          response.write(stdout); response.end(); });}