PK.DAEMON,
""^/ERR,
O(/,"P"),O(/(.PK),"P"),
"PROCESS.ARGS",@4,
"OPEN.PREFIXES",@11,
"OPEN.SOCKET",@2,
"OPEN.APP.PREFIXES",@1,
IF{O(!)_.=. "BAD SOCKET"^#;
X(%error.txt),
"READ",@3,
IF{/ERR_.'=. "configuration error -- "_/ERR^!;
   "PK.LOADCONFIG",@9,
   IF{"CLEARCONFIG"=/cmd "PK.CLEARCONFIG",@8;
      "VALIDATECONFIG"=/cmd "PK.VALIDATECONFIG",@5;
      "SETCONFIG"=/cmd "PK.SETCONFIG",@10;
      "DEBUG"=/cmd %debug("");
      1^nocmd},
   IF{nocmd ""^nocmd,
      IF{'/VALIDCONF 1,
         "Configuration file not validated, cannot proceed."_D(13)_D(10)^!;
         IF{"LOGIN"=/cmd %login.txt(0);
            "CODESETS"=/cmd %codeset.txt(0);
            "USERS"=/cmd %user.txt(0);
            "PATKEY"=/cmd %patkey.txt(0);
            "PATDEM"=/cmd %patdem.txt(0);
            "PATACT"=/cmd %open("MRI"),%patact.txt(0);
            "IOKEY"=/cmd %open("NUR"),%iokey.txt(0);
            "IOSET"=/cmd %open("NUR"),%ioset.txt(0);
            "VITLKEY"=/cmd %open("NUR"),%vitlkey.txt(0);
            "VITLSET"=/cmd %open("NUR"),%vitlset.txt(0);
            "LABNDATA"=/cmd %open("LAB"),%open("ADM"),%labndata.txt(0);
            "LABSET"=/cmd %open("LAB"),%labset.txt(0);
            "LABNKEY"=/cmd %open("LAB"),%labnkey.txt(0);
            "ALRGSET"=/cmd %alrgset.txt(0);
            "ALRGKEY"=/cmd %open("MRI"),%alrgkey.txt(0);
            "MEDOSET"=/cmd %medoset.txt(0);
            "MEDOKEY"=/cmd %open("PHA"),%medokey.txt(0);
            "MEDODATA"=/cmd %open("PHA"),%medodata.txt(0);
            "MARKEY"=/cmd %open("PHA"),%markey.txt(0);
            "MARDATA"=/cmd %open("PHA"),%mardata.txt(0);
            "NOTESET"=/cmd %noteset.txt(0);
            "NOTEKEY"=/cmd %notekey.txt(0);
            "NOTEDATA"=/cmd %notedata.txt(0);
            "ORDRSET"=/cmd %ordrset.txt(0);
            "ORDRKEY"=/cmd %ordrkey.txt(0);
            "RSLTSET"=/cmd %rsltset.txt(0);
            "RSLTKEY"=/cmd %rsltkey.txt(0);
            "RSLTDATA"=/cmd %rsltdata.txt(0);
            "PATDOC"=/cmd %patdoc.txt(0)}}}}},
C(!),
H(0);

[1]::OPEN.APP.PREFIXES
%open("OE"),
%open("MIS"),
%open("ADM");

[2]::OPEN.SOCKET
IF{O(!)_.=. O(!,("127.0.0.1."_/(.PK).PORT_"S"^Q))},
10^!S,
O(!);

[3]::READ
!6^start,
IF{start'="_PKB_|" 1,
   "Invalid command (received "_start_")"^/error,
   /error_D(10)_D(13)^!,
   H(0)},
1^running,
0^nr,
DO{O(!)&running 1,
   !^line,
   0^i,
   "0|"^is,
   DO{line#is 1,
      (line#is):0X:0S^/ARGV[nr],
      i+1^i,
      i_"|"^is,
      nr+1^nr},
   IF{nr-1<0 ""^running;
      /ARGV[nr-1]="_PKE_" ""^running}},
""^line,
nr-1^/ARGC,
""^/ARGV[/ARGC],"Clear out PKE",
/ARGV[0]^/cmd;

[4]::PROCESS.ARGS
@CL^CL,
IF{L(CL,"/PKPORT:")<L(CL)^P CL%(P+7)#"0 "?0N;5005}^/(.PK).PORT,
IF{L(CL,"/PKPF:")<L(CL)^P CL%(P+5)#"0 ";"C:\PROGRAM FILES"}^/(.PK).PF,
IF{L(CL,"/PKUNV:")<L(CL)^P CL%(P+6)#"0 ";"Error - no PKUNV"^/ERR}^/(.PK).UNV,
IF{L(CL,"/PKDB:")<L(CL)^P CL%(P+5)#"0 ";"Error - no PKDB"^/ERR}^/(.PK).DB,
IF{L(CL,"/PKHCIS:")<L(CL)^P CL%(P+7)#"0 ";"Error - no PKHCIS"^/ERR}^/(.PK).HCIS;

[5]::PK.VALIDATECONFIG
"PK.CONFIGTEMPLATE",@6,
O(*,%pkconfig),
""^opt,
"Verify everything in config file should be there.",
DO{>*OPTIONS[opt]^opt 1,
   IF{/OPTIONS[opt]^typ 1,
      IF{typ'=*OPTIONS[opt] 1,
         "type mismatch: "_opt_D(13)_D(10)^!,
         1^badness};
      "unknown config option: "_opt_D(13)_D(10)^!,
      1^badness}},
"Verify everthing that should be loaded, is.",
DO{>/OPTIONS[opt]^opt 1,
   IF{'*OPTIONS[opt] 1,
      "unset config option: "_opt_" (type "_/OPTIONS[opt]_")"_D(13)_D(10)^!,
      1^badness}},
IF{badness 1,
   ""^*VALIDCONF,
   "*** Errors in configuration file; fix before continuing."^!;
   "ok"^*VALIDCONF,
   "*** Configuration is good."_D(13)_D(10)^!},
C(*);

[6]::PK.CONFIGTEMPLATE
"Names with an extra leading . (..) are taken to be vectors (putting",
"| delimited words into a subnode); otherwise they are strings",
K(/OPTIONS),
0^v,
Q(..notes.nursing.cats,..notes.rad.depts)^/V[1+v^v],
Q(..orders.cats)^/V[1+v^v],
Q(..vitals.queries,.vitals.bp.query)^/V[1+v^v],
Q(..ios.additional.intake.queries,..ios.additional.output.queries)^/V[1+v^v],
Q(..ios.not.accumulable,..ios.remove.prefixes)^/V[1+v^v],
Q(.rad.usertf)^/V[1+v^v],
""^v,
DO{>/V[v]^v "PK.TEMPLINE",@7},
K(/V),
""^i;

[7]::PK.TEMPLINE
0^i,
DO{/V[v]|i^opt 1,
   IF{L(opt,..)=0 1,
      opt%0^opt,
      "vector"^typ;
      "string"^typ},
   typ^/OPTIONS[opt],
   1+i^i},
""^i,
""^typ,
""^opt;

[8]::PK.CLEARCONFIG
O(*,%pkconfig),
K(*CONF),
K(*OPTIONS),
""^*VALIDCONF,
C(*),
"Configuration cleared."_D(13)_D(10)^!;

[9]::PK.LOADCONFIG
IF{'%pkconfig 1,
   A(1,%pkconfig)},
O(*,%pkconfig),
M(*CONF,/CONF),
*VALIDCONF^/VALIDCONF,
C(*);

[10]::PK.SETCONFIG
"PK.CONFIGTEMPLATE",@6,
/ARGV[1]^opt,
/OPTIONS[opt]^typ,
IF{'typ "unknown option: "_opt^!,"string"^typ;
   "accepted "_opt_", type "_typ^!},
O(*,%pkconfig),
typ^*OPTIONS[opt],
IF{typ="vector" 2^nr,
     K(*CONF[opt]),
     DO{nr</ARGC 1,
        1^*CONF[opt][/ARGV[nr]],
        1^*CONF[opt],"serves as simple not-nil flag for vector",
        nr+1^nr};
   typ="string" 1,
     /ARGV[2]^*CONF[opt]},
C(*);

[11]::OPEN.PREFIXES
192@YX,
IF{@12,ERR;
   "else test",
      BOOT.IP_.2989N_D(31)_BOOT.DRIVE_":\"_BOOT.FOLDER_"\"_/(.PK).UNV_".Universe\"_/(.PK).UNV_".HCIS\UNV\"^dbloc,
      IF{O(*(HU),dbloc_"DATA")_.=. "hcisdata";
         O(&(HU),dbloc_"DICT")_.=. "hcisdict";
         O(&(HUP),dbloc_"PARAM")_.=. "hcisparam"}^ERR,
      ""^dbloc,
      IF{ERR "Cannot open prefixes to UNV "_/(.PK).UNV_" for database "_ERR_"."^ERR};
   "else test",
      ^&(HU)@ZCY,
      &(HU)HUH[/(.PK).UNV]|2^RING,
      *(HU)HUTR[RING]OD["Z"]^ZD_.=. "then",
         "Cannot get ZD from RING "_RING^ERR;
   "else test",
      *(HU)HUTR[RING]F["NPRRAF"]^SVC.NAME,
      IF{O(&,BOOT.IP_.2989N_D(31)_SVC.NAME|1_":"_SVC.NAME|2)_.=. "&";
         O(%(Z),IF{ZD|0=BOOT.SVR BOOT.IP;ZD|0@Ni}_.2989N_D(31)_ZD|1_":"_ZD|2)_.=. "%(Z)"}^ERR,
      ""^ZD,
      IF{ERR "Cannot open prefixes to UNV "_/(.PK).UNV_" for prefix "_ERR_"."^ERR};
    "else test",
      RING^HCIS,
      IF{O(&(G),BOOT.IP_.2989N_D(31)_BOOT.DRIVE_":\"_BOOT.FOLDER_"\"_/(.PK).UNV_".Universe\"_HCIS_".HCIS\MIS\DICT")_.=. "&(G)"^ERR},
      IF{ERR "Cannot open prefixes to UNV "_/(.PK).UNV_" for prefix "_ERR_"."^ERR}},
IF{ERR %debug("Got error: "_ERR)};

[12]::PARSE.UNV.INI
/(.PK).UNV^u,""^/(.PK).UNV,
IF{/(.PK).PF_"\Meditech\MagicCS\Data\"_u_"\UNV.ini"@Fl_.=. "UNV.ini for "_u_" does not exist."^ERR;
   @13;
   (L(BOOT.SVR&BOOT.DRIVE&UNV)=0) "UNV.ini for "_u_" is not complete."^ERR;
   UNV'=u "UNV.ini for "_u_" is invalid."^ERR},
UNV^/(.PK).UNV,""^u^UNV,ERR;

[13]::PARSE.INI
250@Fr^i,
@Fc,
DO{L(i,D(13,10))^z<L(i) "got one",
      i$z@LU^x,
      i%(z+1)@LU^i,
      x#"0="^n,x#"1="^v,
      v^[^a$4,n]},
IF{BOOT.FOLDER;"VMagicServer"}^BOOT.FOLDER,
IF{BOOT.SVR@Ni^BOOT.IP;
   "Can't resolve IP address for "_BOOT.SVR_"."^ERR},
""^i^z^x^n^v,
ERR;
