      SUBROUTINE PLW.INIT(FLG)
*PIC-LAN
*
*ALL
*PC
*N
*
*
*
*
******************************************************************************
******************************************************************************
***                                                                        ***
*** (C) Copyright 1990-2005 Doug Dumitru, All Rights Reserved.             ***
***                                                                        ***
*** This program is licensed under the terms of the GNU General Public     ***
*** License, version 2.0 with attached notices.                            ***
***                                                                        ***
*** The full text can be found in the LICENSE.TXT file in the              ***
*** PLIP.BP database file (the /usr/qmweb/PLIP.BP/PLIP.BP directory).      ***
***                                                                        ***
******************************************************************************
******************************************************************************
*
      INCLUDE SYS.TYPE.INCLUDE
*
      INCLUDE PL.COMMON
      INCLUDE PL.COMMON.DEFS
      INCLUDE PL.DEFS
      INCLUDE PLIP.COMMON
      INCLUDE PLW.COMMON
*
*$*   CALL PLIP.INIT.SUB
      CALL PLW.INIT.FILEOPENS(FLG)
*
      EQUATE FINGER.PORT         TO 79
      EQUATE HTTP.PORT           TO 80
*
      EQUATE SPINNER TO '|/-\'
*
      CRLF = CR : LF
      NUL80 = STR(CHAR(0),80)
*
      PL$CFG = 'CONFIG'
      PL$RESTART.FLG = NO
*
      IF PL$OFFLINE THEN
         PL$V.PORT.NO = 999999
      END ELSE
         PL_PRINT 'PicLan-IP THREAD process starting on port ' : PORT.NO
*
         IF PL$V.PORT.NO = '' THEN
            PL$V.PORT.NO = SENT<1,3>
            IF NOT(PL$V.PORT.NO MATCHES '1N0N') THEN
               PL$V.PORT.NO = ''
               READ D FROM PL$PROC.FD , 'CFG*PORTS' THEN
                  LOCATE(PORT.NO,D,1;L) THEN
                     PL$V.PORT.NO = D<2,L>
                  END
               END
            END
         END
*
         BEGIN CASE
            CASE SENT<1,2> = 'START'
            CASE SENT<1,2> = 'RESTART'
               PL$RESTART.FLG = YES
            CASE SENT<1,2> = ''
               IF PL$V.PORT.NO = '' THEN PL$V.PORT.NO = 0
            CASE YES
               PL_PRINT 'Error starting thread on port ' : PORT.NO : ' invalid process type flag ' : SENT<1,2> : '.'
               STOP
         END CASE
*
         PL_PR 'PicLan-IP THREAD ' : PL$V.PORT.NO
         IF PL$RESTART.FLG THEN
            PL_PR ' (restart)'
         END
         IF NOT(PL$V.PORT.NO) THEN PL_PR ' (master)'
         PL_PRINT ''
      END
*
      READU DUMMY FROM PL$LOCK.FD , 'V.PORT.NO*' : PL$V.PORT.NO LOCKED
         PL_PRINT 'THREAD process number is already in use.'
         STOP
      END ELSE NULL
      WRITE PORT.NO ON PL$STATE.FD , 'A.PORT.NO*' : PL$V.PORT.NO
*
      PR$NOW = PL_NOW()
      PL_PRSTATE "INIT"
*
      CALL PLW.INIT.SUB(FLG)
*
      WRITEV PL$CFG ON PL$PROC.FD , 'CFG*' : PL$V.PORT.NO , 1
      WRITEV PORT.NO ON PL$PROC.FD , 'CFG*' : PL$V.PORT.NO , 2
*
      IF NOT(PL$RESTART.FLG) AND NOT(PL$V.PORT.NO) THEN
         SELECT PL$CONN.FD
         EOF = NO
         LOOP
            READNEXT ID ELSE EOF = YES
         UNTIL EOF DO
            IF ID MATCHES '4N' THEN
               PL_READ_STATE THEN
                  IF ST$CFG = PL$CFG THEN
                     DELETE PL$STATE.FD , ID
                     DELETE PL$STATE.FD , 'I*' : FIELD(ID,'*',2)
                     DELETE PL$STATE.FD , 'O*' : FIELD(ID,'*',2)
                  END
               END
            END
         REPEAT
*
         SELECT PL$DATA.FD
         EOF = NO
         LOOP
            READNEXT ID ELSE EOF = YES
         UNTIL EOF DO
            IF FIELD(ID,'*',1) = PL$CFG THEN
               DELETE PL$DATA.FD , ID
            END
         REPEAT
      END
*
      MAT ST$ARRAY = ''
*
      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 CALL PLXX.STOP(201,DATA$FNAME)
*
      RETURN
   END
