      subroutine BLDHLP(OFFSET)
      include 'SIZE.INC'
      include 'UNITS.INC'
      include 'HELPER.INC'
      real MAS
      integer NOHELP,I,N,IN,IT,JEND,NENT,MAPLO,IMLO,MAPHI,L,M,NK,KNT
      integer RECDAT(FILDAT),HRECS,OFFSET,KLEN,NRECS,J,IM,K
      integer LRAIL
      integer INIT,LENGTH,IBUFF(VARIEC),map(VARIEC)
      integer HELEN(MAXHLP),MINHLP(MAXHLP),START,STOPIT,TYPE
      character CBUFF(VARCEC)*1, INPUT*80, BLANK*1, CCHAR*1,
     &PATTRN*(COMCHR), HELPS(MAXINI)*1
      data BLANK /' '/
      NOHELP = 0
      INIT = 0
      open (unit=RWLUN(MACHNO),FILE='HELPFIL.DAT',status='OLD')
      if (.not.(TOPOPN(MACHNO))) rewind RWLUN(MACHNO)
100   continue
      read (RWLUN(MACHNO),5000,end=120) INPUT
5000  format (A80)
      if (INPUT(1:1).ne.'$') go to 100
      if (INPUT(2:4).eq.'END') go to 120
      START = 5
      call SKAN (INPUT,START,STOPIT,TYPE,BLANK,1)
      if (TYPE.eq.100) go to 250
      LENGTH = STOPIT-START
      NOHELP = NOHELP+1
      if (NOHELP.gt.MAXHLP) go to 260
      HELEN(NOHELP) = LENGTH
      do 110 I = 1, LENGTH
         INIT = INIT+1
         if (INIT.gt.MAXINI) go to 270
         HELPS(INIT) = INPUT(I+4:I+4)
110   continue
      go to 100
120   continue
      call MNLN (NOHELP,HELEN,MINHLP,HELPS,MAXINI)
      write (PRINTR(MACHNO),5010)
5010  format (///16X,'+******************+'/16X,'|      EXAMS       |'/ 
     &16X,'|  BLDHLP Utility  |'/16X,'|    02-Nov-88     |'/16X,'+******
     &************+')
      write (PRINTR(MACHNO),5020) VARCEC,NOHELP
5020  format (//' Building the random access HELP file from a'/' sequent
     &ial input file.  Each of the input records is 80'/' characters lon
     &g, and each of the output records has'/' a record length of',I5,' 
     &CHARACTER values.'//' Number of options: ',I5//)
      N = 0
      IN = OFFSET
      IT = IN+1
      call RECPAK (IN,HELEN,NOHELP)
      write (PRINTR(MACHNO),5030)
5030  format (//' Lengths of the names of the options are stored')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5040) IT,IN
5040     format (' in records ',I5,' through ',I5,'.')
      else
         write (PRINTR(MACHNO),5050) IT
5050     format (' in record ',I5,'.')
      endif
      do 130 I = 1, NOHELP
         N = N+HELEN(I)
130   continue
      IT = IN+1
      call RECPAK (IN,MINHLP,NOHELP)
      write (PRINTR(MACHNO),5060)
5060  format (//' Lengths required for unique identification are')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5040) IT,IN
      else
         write (PRINTR(MACHNO),5050) IN
      endif
      IT = IN+1
      call PKCREC (IN,HELPS,N)
      write (PRINTR(MACHNO),5070)
5070  format (//' HELP text names are stored in')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5040) IT,IN
      else
         write (PRINTR(MACHNO),5050) IN
      endif
      write (PRINTR(MACHNO),5080) N
5080  format (//' Total number of characters in the HELP text'/' option 
     &names: ',I5,'.'//)
      JEND = IN-OFFSET
      NENT = VARIEC/3
      I = NOHELP/NENT
      if (I*NENT.lt.NOHELP) I = I+1
      MAPLO = IN+1
      IMLO = IN+1
      MAPHI = IN+I
      write (PRINTR(MACHNO),5090)
5090  format (//' Pointers to the option descriptions are stored')
      if (MAPLO.ne.MAPHI) then
         write (PRINTR(MACHNO),5040) MAPLO,MAPHI
      else
         write (PRINTR(MACHNO),5050) MAPLO
      endif
      IN = MAPHI
      rewind RWLUN(MACHNO)
      read (RWLUN(MACHNO),5000,end=120) INPUT
      write (PRINTR(MACHNO),5100)
5100  format (//,'1ELEMENT NUMBER -  NAME  - RECORD NO. - OFFSET -',' NO
     &. OF CHARS.'/' ==============   ======   ==========   ======  ',' 
     &=============',/)
      L = 0
      M = 0
      MAS = 0.0
      NK = 0
      KNT = 0
      IM = 1
      map(IM) = IN+1
      map(IM+1) = 1
140   continue
      read (RWLUN(MACHNO),5000,end=180) INPUT
      if (INPUT(1:1).eq.'$') go to 160
      K = LRAIL(INPUT,80)+1
      do 150 I = 1, K
         L = L+1
         M = M+1
         MAS = MAS+1.
         if (L.gt.VARCEC) then
            IN = IN+1
            write (RANUNT(MACHNO),rec=IN) CBUFF
            L = 1
         endif
         CCHAR = INPUT(I:I)
         if (I.eq.K) CCHAR = '$'
         CBUFF(L) = CCHAR
150   continue
      go to 140
160   continue
      map(IM+2) = M
      M = 0
      PATTRN = ' '
      KNT = KNT+1
      KLEN = HELEN(KNT)
      do 170 I = 1, KLEN
         NK = NK+1
         if (I.gt.COMCHR) go to 170
         PATTRN(I:I) = HELPS(NK)
170   continue
      write (PRINTR(MACHNO),5110) KNT,PATTRN,map(IM),map(IM+1),map(IM+2)
5110  format (7X,I3,8X,A,6X,I4,7X,I4,10X,I4)
      IM = IM+3
      if (IM+2.le.VARIEC) then
         map(IM) = IN+1
         map(IM+1) = L+1
      else
         write (RANUNT(MACHNO),rec=MAPLO) map
         MAPLO = MAPLO+1
         IM = 1
         map(1) = IN+1
         map(2) = L+1
      endif
      go to 140
180   continue
      IN = IN+1
      write (RANUNT(MACHNO),rec=IN) CBUFF
      map(IM+1) = M
      write (RANUNT(MACHNO),rec=MAPLO) map
      IT = MAPHI+1
      write (PRINTR(MACHNO),5120)
5120  format (//' The HELP keyword text is stored in')
      if (IT.ne.IN) then
         write (PRINTR(MACHNO),5040) IT,IN
      else
         write (PRINTR(MACHNO),5050) IN
      endif
      write (PRINTR(MACHNO),5130) MAS
5130  format (//' The total number of characters stored in the'/' HELP k
     &eyword text records is: ',F8.0)
      NRECS = IN-OFFSET
      write (PRINTR(MACHNO),5140) NRECS
5140  format (//' Total records for the HELP database: ',I5,'.')
      HRECS = FILDAT/VARIEC
      if (FILDAT-VARIEC*HRECS.ne.0) HRECS = HRECS+1
      M = 0
      do 200 I = 1, HRECS
         read (RANUNT(MACHNO),rec=I) IBUFF
         do 190 L = 1, VARIEC
            M = M+1
            if (M.gt.FILDAT) go to 210
            RECDAT(M) = IBUFF(L)
190      continue
200   continue
210   continue
      RECDAT(1) = OFFSET+1
      RECDAT(2) = JEND
      RECDAT(3) = IMLO
      RECDAT(4) = NRECS-JEND
      do 240 I = 1, HRECS
         J = (I-1)*VARIEC
         K = J+VARIEC
         J = J+1
         M = 0
         do 220 L = J, K
            M = M+1
            if (L.gt.FILDAT) go to 230
            IBUFF(M) = RECDAT(L)
220      continue
230      continue
         write (RANUNT(MACHNO),rec=I) IBUFF
240   continue
      OFFSET = IN
      close (unit=RWLUN(MACHNO))
      return
250   continue
      write (PRINTR(MACHNO),5150)
5150  format (/' Blank keyword encountered.'/' The HELP FILE facility ca
     &nnot be created or used.'/' See the MAINTAINER or DEVELOPER.')
      NOHELP = 0
      go to 120
260   continue
      write (PRINTR(MACHNO),5160) MAXHLP
5160  format (/'The maximum number of keywords has been exceeded:'/1X,I3
     &   ,' keywords have been encountered.')
      go to 120
270   continue
      write (PRINTR(MACHNO),5170) MAXINI
5170  format (/' The maximum number (',I4,') of elements'/' in the keywo
     &rd table has been exceeded.')
      go to 120
      end
