PK.SEARCH
O($S,$),
O(%S,%),
IF{O(#)_"."="." 1^remote,
/ARGV[1]^A,
/ARGV[2]^B,
/ARGV[3]^C,
/ARGV[4]^D,
/ARGV[5]^E,
/ARGV[6]^F},
IF{(A=0)!(A=1) @AUTO;
@SEARCH.ALL.SEGS(A,B,C,"","","Y")},
C($U),
C(%U),
END;

AUTO
@OUT("PK.DATABASES"),
@OUT(""),
@FIND.PKDIR,
IF{B B^m1^m2;
"TEST.5.4.MIS"^m1^m2},
$DIR(m1),
@SEARCH.CUR.FOR.DB($MAC,m1,".MIS.dic","misdir","misdict"),
@SEARCH.ALL.SEGS(m1,m2,".ADM.data","admdir","admdata","Y"),
IF{'remote O(/,"P")},
Q("labdir","labdata",/CONF[.uselab])^/s[".LAB.data"],
Q("","labdict",/CONF[.uselab])^/s[".LAB.dic"],
Q("phadir","phadata",/CONF[.usepha])^/s[".PHA.data"],
Q("","phadict",/CONF[.usepha])^/s[".PHA.dic"],
Q("oedir","oedata",/CONF[.useoe])^/s[".OE.data"],
Q("","oedict",/CONF[.useoe])^/s[".OE.dic"],
Q("nurdir","nurdata",/CONF[.usenur])^/s[".NUR.data"],
Q("","nurdict",/CONF[.usenur])^/s[".NUR.dic"],
Q("mridir","mridata",/CONF[.usemri])^/s[".MRI.data"],
Q("","mridict",/CONF[.usemri])^/s[".MRI.dic"],
Q("schdir","schdata",/CONF[.usesch])^/s[".SCH.data"],
Q("","schdict",/CONF[.usesch])^/s[".SCH.dic"],
""^w,
DO{>/s[w]^w @SEARCH.ALL.SEGS(m1,m2,w,/s[w]|0,/s[w]|1,/s[w]|2)},
IF{D D^m1^m2;
"RAD"^m1,"TEST5.4"^m2},
@SEARCH.ALL.SEGS(m1,m2,"rad.data","raddir","raddata",/CONF[.userad]),
@SEARCH.ALL.SEGS(m1,m2,"rad.data","","raddict",/CONF[.userad]),
@SEARCH.ALL.SEGS(m1,m2,"rad.rpt","","radrpt",/CONF[.userad]),
IF{E E^m1^m2;
"LAB.OV.FHC.TEST"^m1^m2},
@SEARCH.ALL.SEGS(m1,m2,"ilm.reqs.specs","mocklabdir","mocklabdata",/CONF[.usemocklab]),
@SEARCH.ALL.SEGS(m1,m2,"ilm.data","","mocklabindx",/CONF[.usemocklab]),
"mocklabdict":14L_" $.data[&.CDFN]"^x,
@OUT(x),
IF{C C^m1^m2;
"MSM"^m1^m2},
@SEARCH.ALL.SEGS(m1,m2,"or.data","msmdir","msmdata",/CONF[.usemsm]),
IF{F F^m1^m2;
"PCI"^m1,"TEST5.4"^m2},
@SEARCH.ALL.SEGS(m1,m2,"pci.data","pcidir","pcidata",/CONF[.usepci]),
@SEARCH.ALL.SEGS(m1,m2,"pci.file.results","","pciindx",/CONF[.usepci])

SEARCH.ALL.SEGS
@1^sas.match1,
@2^sas.match2,
@3^sas.db,
@4^sas.dirmac,
@5^sas.dbmac,
@6^sas.use,
IF{'sas.use IF{sas.dirmac sas.dirmac:14L_" N"^x,@OUT(x)},
(sas.dbmac):14L_" N"^x,@OUT(x);""^sas.seg,
DO{+$MAC[sas.seg]^sas.seg 1,
@SEARCH.SEG(sas.seg,sas.match1,sas.match2,sas.db,sas.dirmac,sas.dbmac)}}

FIND.PKDIR
O(&,$.LIMBS),
""^file,
DO{+&[file]^file IF{&[file]=% 1,
"pkdir":14L_" $SEG("_D(34)_$MAC_D(34)_","_D(34)_file_D(34)_")"^x,
@OUT(x)}}

SEARCH.SEG
@1^seg,
@2^match1,
@3^match2,
@4^db,
@5^dirmac,
@6^dbmac,
$SEG(seg,".TREE"),
O(&,$.LIMBS),
""^file,
DO{+&[file]^file L(file)^len,
IF{'match1 @OUT(seg_" "_file);
IF{L(file,match1)'=len IF{'match2 @OUT(seg_" "_file);
IF{L(file,match2)'=len IF{'db @OUT(seg_" "_file);
IF{(match1'=match2)!(match1=match2=file) $DIR(file),
@SEARCH.CUR.FOR.DB(seg,file,db,dirmac,dbmac)}}}}}}}

SEARCH.CUR.FOR.DB
@1^s.seg,
@2^s.file,
@3^s.db^s.dbc,
@4^s.dirmac,
@5^s.dbmac,
""^fnd,
DO{(>%[s.dbc]^s.dbc),(s.dbc#0S)=s.db 1^fnd,
IF{s.dirmac s.dirmac:14L_" $SEG("_D(34)_s.seg_D(34)_","_D(34)_s.file_D(34)_")"^x,
@OUT(x)},
(s.dbmac):15L_" %"_(s.dbc#0S)_"["_D(34)_(s.dbc#1S)_D(34)_","_D(34)_(s.dbc#2S)_D(34)_"]"^x,
@OUT(x)},
IF{'fnd IF{%[s.db] 1,
IF{s.dirmac s.dirmac:14L_" $SEG("_D(34)_s.seg_D(34)_","_D(34)_s.file_D(34)_")"^x,
@OUT(x)},
(s.dbmac):15L_" %"_(s.db)^x,
@OUT(x)}}

OUT
IF{remote @1_D(13)_D(10)^!;
IF{'li 0^li},
""^lf,
IF{li=20 1^lf,D(13)_D(10)_"-- More --"^#,#S,D(13)_"          "_D(13)^#,0^li},
li+1^li,
IF{'lf D(13)_D(10)^#},
@1^#}
