var express = require("./node_modules/express");
var app = express();
var exec = require("child_process").exec;

const birtns = "../birtns/o(../birtns)";
const vartns = "../vartns/o(../vartns)";
const gtm_dist = "/usr/local/lib/yottadb/r138";
const gtmgbldir = "../g/mumps.gld";
const 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;

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

app.use(express.text());

//------------------------------- Documentation -------------------------------
app.get('/logit/:logitvar'function (requestresponse) { maingtm(requestresponse"logit^settings " + request.params.logitvar); });

//-------------------------- VistA Dictionary Viewer --------------------------
app.get('/dict/list'function (requestresponse) { maingtm(requestresponse"list^dictionary"); });
app.get('/dict/stan/:ien'function (requestresponse) { maingtm(requestresponse"stan^dictionary " + request.params.ien); });
app.get('/dict/glob/:ien'function (requestresponse) { maingtm(requestresponse"glob^dictionary " + request.params.ien); });
app.get('/dict/def/:ien/:filter'function (requestresponse) { maingtm(requestresponse"def^dictionary " + request.params.ien + "^" + request.params.filter); });

//---------------------------- FileMan API Tester -----------------------------
app.post('/fmapi/do'function (requestresponse) { maingtm(requestresponse"en^fmapil " + request.body); });

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

//--------------------------------- Monitor -----------------------------------
app.get('/mon/pm2list'function (requestresponse) { main(requestresponse"pm2 list"); });
app.get('/mon/lastcnt'function (requestresponse) { maingtm(requestresponse"last^datecnt"); });

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

//--------------------------------- surgery -----------------------------------
app.get('/surg/list'function (requestresponse) { maingtm(requestresponse"list^surgery"); });
app.get('/surg/patient/:search'function (requestresponse) { maingtm(requestresponse"patient^surgery " + request.params.search); });
app.get('/surg/provider/:search'function (requestresponse) { maingtm(requestresponse"provider^surgery " + request.params.search); });
app.get('/surg/procedure/:search'function (requestresponse) { maingtm(requestresponse"procedure^surgery " + request.params.search); });
app.post('/surg/do'function (requestresponse) { maingtm(requestresponse"do^surgery " + request.body); });

//----------------------------------- volp ------------------------------------
app.get('/vopl/list'function (requestresponse) { maingtm(requestresponse"list^vopl"); });
app.get('/vopl/main/:ien'function (requestresponse) { maingtm(requestresponse"main^vopl " + request.params.ien); });

//----------------------------------- zzbi ------------------------------------
app.get('/zzbi'function (requestresponse) { maingtm(requestresponse"^zzbi"); });

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

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

// maingtm command function.
function maingtm(request,response,cmdLine)
  { main(request,response,gtm_dist + "/mumps -run " + cmdLine); }