      SUBROUTINE WRITELOCKFILE(icounter,maxprobe,
     * probelockrec,ip,lockstat,il,wholockrec,iw,
     * timerec,it,whatrec,iw2,ilockfile)

C     WRITELOCKFILE WRITES OUT MERGELOCK FILE

      character*80 usrrec,filename,chead1,chead2
      character*100 usrrecout
      character*(*) probelockrec(maxprobe)
      character*(*) lockstat(maxprobe)
      character*(*) wholockrec(maxprobe)
      character*(*) timerec(maxprobe)
      character*(*) whatrec(maxprobe)

      if ( ilockfile .ne. 6 ) then
       call extraname('sys.inf'//CHAR(0),'mergeLOCK.log'//CHAR(0),
     * filename)
       open(ilockfile,file=filename,status='unknown')
      endif

c     write header lines

      call clearstring(chead1,80)
      chead1='     PROBE            STATUS     USER             TIME'
      call writecharline(chead1,80,ilockfile)

      call clearstring(chead2,80) 
      chead2='-------'//
     * '-------------------------------------------------------'
      call writecharline(chead2,80,ilockfile)

c     write out probes 

      do 50 nn=1,icounter

       call makemergeline(usrrec,probelockrec(nn),ip,
     *  lockstat(nn),il,wholockrec(nn),iw,timerec(nn),it,
     *  whatrec(nn),iw2)
        
       call clearstring(usrrecout,100)
       if ( ilockfile .eq. 6 ) then
        usrrecout='##> '//usrrec
        call insertnumsp('#',nn,usrrecout,5)
       else
        usrrecout=usrrec
       endif
       call writecharline(usrrecout,80,ilockfile)
   
50    continue

      if ( ilockfile .ne. 6 ) close(ilockfile)

      return
      end 

      SUBROUTINE WRITECHARLINE(string,maxlength,ifile)

      character*5 cform1
      character*(*) string
      call findlastchar(nsp,string,maxlength)
      cform1='(aXX)'
      call insertnum('X',nsp,cform1,5)
      write(ifile,cform1) string

      return
      end
