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(8090, function(){ console.log(Date() + ": Server has started."); });
app.use(express.text());
//------------------------------- Documentation -------------------------------
app.get('/logit/:logitvar', function (request, response) { maingtm(request, response, "logit^settings " + request.params.logitvar); });
//-------------------------- VistA Dictionary Viewer --------------------------
app.get('/dict/list', function (request, response) { maingtm(request, response, "list^dictionary"); });
app.get('/dict/stan/:ien', function (request, response) { maingtm(request, response, "stan^dictionary " + request.params.ien); });
app.get('/dict/glob/:ien', function (request, response) { maingtm(request, response, "glob^dictionary " + request.params.ien); });
app.get('/dict/def/:ien/:filter', function (request, response) { maingtm(request, response, "def^dictionary " + request.params.ien + "^" + request.params.filter); });
//---------------------------- FileMan API Tester -----------------------------
app.post('/fmapi/do', function (request, response) { maingtm(request, response, "en^fmapil " + request.body); });
//------------------------------------ ipl ------------------------------------
app.get('/patch/ipl', function (request, response) { maingtm(request, response, "view^ipl"); });
app.get('/patch/delimited/:ien', function (request, response) { maingtm(request, response, "delimited^ipl " + request.params.ien); });
//--------------------------------- Monitor -----------------------------------
app.get('/mon/pm2list', function (request, response) { main(request, response, "pm2 list"); });
app.get('/mon/lastcnt', function (request, response) { maingtm(request, response, "last^datecnt"); });
//----------------------------------- rpc ------------------------------------
app.get('/rpc/list', function (request, response) { maingtm(request, response, "list^rpcw"); });
app.get('/rpc/main/:ien', function (request, response) { maingtm(request, response, "main^rpcw " + request.params.ien); });
//--------------------------------- surgery -----------------------------------
app.get('/surg/list', function (request, response) { maingtm(request, response, "list^surgery"); });
app.get('/surg/patient/:search', function (request, response) { maingtm(request, response, "patient^surgery " + request.params.search); });
app.get('/surg/provider/:search', function (request, response) { maingtm(request, response, "provider^surgery " + request.params.search); });
app.get('/surg/procedure/:search', function (request, response) { maingtm(request, response, "procedure^surgery " + request.params.search); });
app.post('/surg/do', function (request, response) { maingtm(request, response, "do^surgery " + request.body); });
//----------------------------------- volp ------------------------------------
app.get('/vopl/list', function (request, response) { maingtm(request, response, "list^vopl"); });
app.get('/vopl/main/:ien', function (request, response) { maingtm(request, response, "main^vopl " + request.params.ien); });
//----------------------------------- zzbi ------------------------------------
app.get('/zzbi', function (request, response) { maingtm(request, response, "^zzbi"); });
//----------------------------------- Functions ------------------------------------
// main command function.
function main(request,response,cmdLine)
{ exec(cmdLine, {maxBuffer: 1024 * 3000}, function(error, stdout, stderr)
{ 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); }