      SUBROUTINE PLW.READ.CONFIG(FLG)
*PIC-LAN
*
*ALL
*PC
*N
*
*
*
*
      INCLUDE SYS.TYPE.INCLUDE
*
      INCLUDE PL.COMMON
      INCLUDE PL.COMMON.DEFS
      INCLUDE PL.DEFS
      INCLUDE PLIP.COMMON
      INCLUDE PLW.COMMON
*
      CALL PLW.INIT.SUB(FLG)
*
      PL_PRINT ''
*
      PL_LOCK 20
*
      PL_PRINT 'Initializing HTTP   directory configuration ...'
*
      IF NOT(PL$V.PORT.NO) THEN
         READV PL$LISTEN.LST FROM PL$PROC.FD , 'CFG*' : PL$V.PORT.NO , 4 ELSE PL$LISTEN.LST = ''
      END
*
      X = PL$V.PORT.NO : '*'
      XL = LEN(X)
      SELECT PL$DATA.FD
      EOF = NO
      LOOP
         READNEXT ID ELSE EOF = YES
      UNTIL EOF DO
         IF ID[1,XL] = X THEN
            DELETE PL$DATA.FD , ID
         END
      REPEAT
*
      MAT CFG$ARRAY = ''
*
      PATHS = ''
      HTTP.IP.LST = ''
      RPC.IP.LST = ''
      I1 = DCOUNT(PLW$DIR,VM)
      FOR I = 1 TO I1
         C    = PLW$DIR<1,I,1>
         C.TP = PLW$DIR<1,I,2>
*
         HTTP.ADDR = FIELD(FIELD(C,' ',1),':',1)
         HTTP.PORT = FIELD(FIELD(C,' ',1),':',2)
         HTTP.PATH = FIELD(C,' ',2)
*
         IF HTTP.ADDR = '*' THEN HTTP.ADDR = PLIP$MYIPS<1>
*
         PATH = 'HTTP://' : HTTP.ADDR : ':' : HTTP.PORT : HTTP.PATH
         LOCATE(PATH,PATHS;LOC) THEN
            PL_PRINT 'Duplicate WEB directory ' : PATH : ' found.  Not used!'
         END ELSE
            C1 = FIELD(FIELD(C,'(',1),'[',1)
            CFG$OPT<-1> = '.' : FIELD(FIELD(C,'(',2),')',1)
            CFG.NAME = FIELD(FIELD(C,'[',2),']',1)
            CFG.IP = HTTP.ADDR : ':' : HTTP.PORT
            CFG$PATH<-1> = HTTP.PATH
            CFG$TP<-1>   = FIELD(C,' ',3)
            CFG$PARM<-1> = OCONV(C,'G3 999')
*
            CFG$IP<-1> = CFG.IP
            IF CFG.NAME <> '' THEN
               CFG$NAME<-1> = CFG.NAME
            END ELSE
               CFG$NAME<-1> = CFG.IP
            END
            BEGIN CASE
               CASE C.TP = 'HTTP'
                  LOCATE(CFG.IP,RPC.IP.LST;LOC) THEN
                     RPC.IP.LST = DELETE(RPC.IP.LST,LOC,0,0)
                  END
                  LOCATE(CFG.IP,HTTP.IP.LST;LOC) ELSE
                     HTTP.IP.LST<-1> = CFG.IP
                  END
               CASE C.TP = 'RPC'
                  LOCATE(CFG.IP,HTTP.IP.LST;LOC) ELSE
                     LOCATE(CFG.IP,RPC.IP.LST;LOC) ELSE
                        RPC.IP.LST<-1> = CFG.IP
                     END
                  END
            END CASE
         END
      NEXT I
*
      IP.LIST = ''
      DSG.LIST = ''
      DSG.PLCBS = ''
      PL$FILE.LIST = ''
      PL$DSG.HANDLES = ''
*
      N.CFG = DCOUNT(CFG$IP,AM)
      FOR I = 1 TO N.CFG
         MAT ST$ARRAY = ''
         IP.ADDR = FIELD(CFG$IP<I>,':',1)
         IF IP.ADDR = '*' THEN IP.ADDR = PLIP$MYIPS<1>
         IP.ADDR = PL_HEXTOIP(PL_IPTOHEX(IP.ADDR))
         IP.PORT = INT(FIELD(CFG$IP<I>,':',2)) + 0
         LOCATE(IP.ADDR:':':IP.PORT,IP.LIST;LOC) ELSE
            IP.LIST<-1> = IP.ADDR : ':' : IP.PORT
         END
*
         MAT DATA$ARRAY = ''
         DATA$PREFIX = CFG$NAME<I>
         OPEN.ERR = NO
*
         PL_PR ( '  ' : IP.ADDR : ':' : IP.PORT : CFG$PATH<I> ) 'L#40' : ' '
         BEGIN CASE
            CASE CFG$TP<I> = 'FILE'
               DATA$FNAME = FIELD(CFG$PARM<I>,' ',1)
               IF DATA$FNAME = 'PLIP.RPC,PAGES' THEN
                  PL_PRINT 'RPC Server'
               END ELSE
                  PL_PRINT ( 'Local file ' : DATA$FNAME )[1,38]
               END
               GOSUB 90000
               IF NOT(OPEN.ERR) THEN
                  DATA$TP = 'FILE'
                  DATA$FNUM = LOC
                  DATA$OPT = CFG$OPT<I>
               END ELSE
                  PL_PRINT '*** Error opening file ' : DATA$FNAME : ' ***'
                  MAT DATA$ARRAY = ''
               END
            CASE CFG$TP<I> = 'DSG'
               DSG.NAME = FIELD(CFG$PARM<I>,' ',1)
               DATA$DSGPATH = FIELD(CFG$PARM<I>,' ',2)
               DATA$OPT = CFG$OPT<I>
               PL_PRINT ( 'DSG ' : DSG.NAME : ' ' : DATA$DSGPATH )[1,38]
               LOCATE(DSG.NAME,DSG.LIST;LOC) THEN
                  DATA$DSGPLCB = DSG.PLCBS<LOC>
               END ELSE
                  CALL PLSUB.DSG('OPEN CONNECTION',DSG.HANDLE,DSG.NAME,'','','','','',ERR)
                  IF ERR = '' THEN
                     DSG.LIST<-1> = DSG.NAME
                     PL$DSG.HANDLES<-1> = DSG.HANDLE
                     DATA$DSGPLCB = DCOUNT(PL$DSG.HANDLES,AM)
                     DSG.PLCBS<-1> = DATA$DSGPLCB
                  END ELSE
                     PL_PRINT '*** Error opening connection to DSG ' : DSG.NAME : ' ***'
                     OPEN.ERR = YES
                  END
               END
               IF NOT(OPEN.ERR) THEN
                  DATA$TP = 'DSG'
                  DATA$FNUM = LOC
               END ELSE
                  MAT DATA$ARRAY = ''
               END
            CASE CFG$TP<I> = 'HOST'
               DATA$HOSTPATH = FIELD(CFG$PARM<I>,' ',1)
               DATA$OPT = CFG$OPT<I>
               PL_PRINT ( 'HOST ' : DATA$HOSTPATH )[1,38]
               DATA$TP = 'HOST'
            CASE CFG$TP<I> = 'DSG-RAW'
               DSG.NAME = FIELD(CFG$PARM<I>,' ',1)
               DATA$DSGPATH = FIELD(CFG$PARM<I>,' ',2)
               DATA$OPT = CFG$OPT<I>
               PL_PRINT ( 'DSG-RAW ' : DSG.NAME : ' ' : DATA$DSGPATH )[1,38]
               LOCATE(DSG.NAME,DSG.LIST;LOC) THEN
                  DATA$DSGPLCB = DSG.PLCBS<LOC>
               END ELSE
                  CALL PLSUB.DSG('OPEN CONNECTION',DSG.HANDLE,DSG.NAME,'','','','','',ERR)
                  IF ERR = '' THEN
                     DSG.LIST<-1> = DSG.NAME
                     PL$DSG.HANDLES<-1> = DSG.HANDLE
                     DATA$DSGPLCB = DCOUNT(PL$DSG.HANDLES,AM)
                     DSG.PLCBS<-1> = DATA$DSGPLCB
                  END ELSE
                     PL_PRINT '*** Error opening connection to DSG ' : DSG.NAME : ' ***'
                     OPEN.ERR = YES
                  END
               END
               IF NOT(OPEN.ERR) THEN
                  DATA$TP = 'DSG-RAW'
                  DATA$FNUM = LOC
               END ELSE
                  MAT DATA$ARRAY = ''
               END
            CASE CFG$TP<I> = 'HOST-RAW'
               DATA$HOSTPATH = FIELD(CFG$PARM<I>,' ',1)
               DATA$OPT = CFG$OPT<I>
               PL_PRINT ( 'HOST-RAW ' : DATA$HOSTPATH )[1,38]
               DATA$TP = 'HOST-RAW'
            CASE YES
               PL_PRINT 'Unknown CFG directory type ' : CFG$TP<I>
               STOP
         END CASE
         IF DATA$TP <> '' THEN
            MATWRITE DATA$ARRAY ON PL$DATA.FD , PL$V.PORT.NO : '*' : IP.ADDR : ':' : IP.PORT : CFG$PATH<I>
         END
      NEXT I
      PL_PRINT ''
*
      IF NOT(PL$V.PORT.NO) AND NOT(PL$OFFLINE) THEN
         OLD.LISTENS = PL$LISTEN.LST
         PL$LISTEN.LST = ''
         N.LISTENS = DCOUNT(IP.LIST,AM)
         FOR I = 1 TO N.LISTENS
            LOCATE(IP.LIST<I>,OLD.LISTENS,1,1;LOC) THEN
               PL$LISTEN.LST<1,1,-1> = IP.LIST<I>
               PL$LISTEN.LST<1,2,-1> = OLD.LISTENS<1,2,LOC>
               OLD.LISTENS = DELETE(OLD.LISTENS,1,1,LOC)
               OLD.LISTENS = DELETE(OLD.LISTENS,1,2,LOC)
            END ELSE
               IP.ADDR = FIELD(IP.LIST<I>,':',1)
               IP.PORT = FIELD(IP.LIST<I>,':',2)
               LOCATE(IP.LIST<I>,RPC.IP.LST;DUMMY) THEN
                  PL_PR '  Posting RPC    listen on ' : ( IP.ADDR : ':' : IP.PORT ) 'L#21' : ' '
                  CALL PLW.POST.LISTEN('UNL',IP.ADDR,IP.PORT,RESULT)
               END ELSE
                  PL_PR '  Posting HTTP   listen on ' : ( IP.ADDR : ':' : IP.PORT ) 'L#21' : ' '
                  CALL PLW.POST.LISTEN('WEB',IP.ADDR,IP.PORT,RESULT)
               END
               IF RESULT <> '' THEN
                  PL_PRINT RESULT
               END ELSE
                  PL$LISTEN.LST<1,1,-1> = IP.LIST<I>
                  PL$LISTEN.LST<1,2,-1> = RES
*
                  ST$PLCB = RES
                  ST$TP = 1
                  ST$IP = IP.ADDR : ':' : IP.PORT
                  ST$CFG = PL$CFG
                  PL_TCP_SET_Q ST$PLCB , A.READ ELSE NULL
                  PL_PRINT 'PLCB = ' : ST$PLCB
                  MATWRITE ST$ARRAY ON PL$CONN.FD , ST$PLCB 'R%8'
                  MAT ST$ARRAY = ''
               END
            END
         NEXT I
         PL_PRINT ''
*
         N.LISTENS = DCOUNT(OLD.LISTENS<1,1>,SVM)
         FOR I = 1 TO N.LISTENS
            CALL PLW.DELETE.LISTEN(FIELD(OLD.LISTENS<1,1,I>,':',1),FIELD(OLD.LISTENS<1,1,I>,':',2),'')
         NEXT I
         WRITEV PL$LISTEN.LST ON PL$PROC.FD , 'CFG*' : PL$V.PORT.NO , 4
      END
*
      IF PLW$EXT <> '' THEN
         PL_PRINT 'Calling extension initialization routines ...'
         I1 = DCOUNT(PLW$EXT<1,1>,SVM)
         FOR I = 1 TO I1
            SUB.NAME = PLW$EXT<1,1,I>
            PL_PRINT '   ' : SUB.NAME
            CALL @SUB.NAME('','INIT','','')
         NEXT I
         PL_PRINT ''
      END
*
* Open local HTTP messages file
*
      MAT DATA$ARRAY = ''
      DATA$PREFIX = 'LOCAL'
      DATA$TP = 'FILE'
      DATA$FNAME = 'WWW.CTRL,LOCAL.PAGES'
*
      OPEN.ERR = NO
      GOSUB 90000
      IF OPEN.ERR THEN
         PL_PRINT 'Error opening local WEB pages file WWW.CTRL,LOCAL.PAGES'
         STOP
      END
      DATA$FNUM = LOC
      MATWRITE DATA$ARRAY ON PL$DATA.FD , PL$V.PORT.NO : '*LOCAL'
*
      PL_UNLOCK 20
*
      RETURN
*
90000 REM Open file group
*
      LOCATE(DATA$FNAME,PL$FILE.LIST;LOC) THEN RETURN
*
      PL$FILE.LIST<-1> = DATA$FNAME
      LOCATE(DATA$FNAME,PL$FILE.LIST;LOC) ELSE NULL
      OPEN DATA$FNAME TO F$ARRAY(LOC) ELSE
         OPEN.ERR = YES
         RETURN
      END
*
      RETURN
   END
