      SUBROUTINE LOCKSTATUS(iaccesstype,ilockin,iown,
     * iprobe,istatus,ilockup,iverb)

C      LOCKSTATUS RETURNS THE PRESENT LOCK STATUS FOR THE
C      GIVEN PROBE

c     .  iaccesstype -  0 = read-only program
c     .                      [no lock required, works on all but EXCL-LOCK]
c     .                        Ex: proflook, overlook, overlap, yeargraph
c
c     .                 1 = low-risk read/write program
c     .                      [READ-ONLY lock required]
c     .                        Ex: cruiseinfo, samecheck, oversort
c
c     .                 2 = high-risk read/write program
c     .                      [EXCL-LOCK required]
c     .                        Ex: headsort, paramsort, addcode, changesubset
c     .                 5 = force exclusive lock
c    
c     .                 8 = DANGEROUS:  Do not run on "main probe"
c     .                    [don't allow, with any lock]
c    
c     istatus:
c     .
c        -1  - local probe, no accession permission required
c     .  "0" = read-only access okay / don't own READ-ONLY lock, if present
c     .  "1" = read-write (low risk) access okay (READ-ONLY)
c     .  "2" = read-write (high risk) access okay (EXCL-LOCK)
c     .  "9" = ACCESS DENIED
c
c     ilockup:
c     . -2 (input value) - automatically set to upgrade to necessary lock
c     . -1 (input value) - prompt user for upgrade
c     .  0 - no lock upgrade
c
      istatus=-1
      ilock=ilockin
      if ( ilock .eq. 3 ) then
       ilock=1
      elseif ( ilock .eq. 4 ) then
       ilock=2
      endif

c     DANGEROUS LOCK CHECK
      if (iaccesstype .eq. 8 .and. iprobe .gt. 0 ) then
       if ( iverb .eq. 1 ) then
        write(6,*)' '
        write(6,*)' WARNING!  THIS PROGRAM SHOULD NOT BE RUN'
        write(6,*)'         ON THE MAIN DATABASES!  '
        write(6,*)' '
       endif
       istatus = 9

c     Lock release

      elseif ( iaccesstype .eq. -1 .and. iprobe .gt. 0 ) then

       if ( iown .eq. 1 ) then
        istatus=ilockin
       else
        istatus=9
       endif

c---------------------------------------------------
c     - Request READ ONLY ACCESS (iaccesstype = 0)
c
c           NO LOCK:  allow read-only access (istatus = 0)
c READ-ONLY (owned):  allow read-only access (istatus = 0)
c           (OTHER):  allow read-only access (istatus = 0)
c EXCL-LOCK (owned):  allow read-only access (istatus = 0)
c           (OTHER):  -> NO ACCESS ALLOWED   (istatus = 9)
c    
c---------------------------------------------------

      elseif ( iaccesstype .eq. 0 .and. iprobe .gt. 0 ) then

       if ( iverb .eq. 1 ) write(6,*) 'access type: read-only'
       if ( ilock .lt. 2 .or. iown .eq. 1 ) then
        istatus=0
       else
        istatus=9
        if ( iverb .eq. 1 ) write(6,*)
     * '        comment:  Probe is EXCL-LOCKed by another user!'
       endif

c------------------------------------------------------------
c     - Request READ-WRITE (minor) ACCESS (iaccesstype = 1)
c
c           NO LOCK:  allow read-only access   (istatus = 0)
c READ-ONLY (owned):  allow minor write access (istatus = 1)
c           (OTHER):  allow read-only access   (istatus = 0)
c EXCL-LOCK (owned):  allow minor write access (istatus = 1)
c           (OTHER):  -> NO ACCESS ALLOWED     (istatus = 9)
c
c------------------------------------------------------------

      elseif ( iaccesstype .eq. 1 .and. iprobe .gt. 0 ) then

       if ( iverb .eq. 1 ) write(6,*) 'access type: read-write (minor)'
       if ( ilock .gt. 0 .and. iown .eq. 1 ) then
        istatus=1
       elseif ( ilock .eq. 1 ) then
        istatus=9
        if ( iverb .eq. 1 ) write(6,*)
     * '        comment:  Probe is READ-ONLY locked by another user!'
       elseif ( ilock .gt. 1 ) then
        istatus = 9
        if ( iverb .eq. 1 ) write(6,*)
     * '        comment:  Probe is EXCL-LOCKed by another user!'
       elseif ( ilock .eq. 0 ) then
        istatus = 9
        if ( ilockup .eq. -2 ) then
         istatus=1
        elseif ( ilockup .eq. -1 ) then
         call upgradelockprompt(1,istatus)
        elseif ( iverb .eq. 1 ) then
         write(6,*)
     * '        comment:  Probe unlocked.  Lock probe to write!'
        endif
       endif

c------------------------------------------------------------
c     - Request READ-WRITE (EXCL) ACCESS (iaccesstype = 2)
c
c           NO LOCK:  -> NO ACCESS ALLOWED     (istatus = 9)
c READ-ONLY (owned):  -> NO ACCESS ALLOWED     (istatus = 9)
c           (OTHER):  -> NO ACCESS ALLOWED     (istatus = 9)
c EXCL-LOCK (owned):  allow EXCL write access  (istatus = 2)
c           (OTHER):  -> NO ACCESS ALLOWED     (istatus = 9)
c
c------------------------------------------------------------

      elseif ( (iaccesstype .eq. 2 .or. iaccesstype .eq. 5 )
     *  .and. iprobe .gt. 0 ) then
       
       if ( iverb .eq. 1 ) write(6,*) 'access type: read-write (EXCL)'

       if ( iown .eq. 1 ) then

        if ( ilock .eq. 1 .or.
     *   ( ilock .eq. 0 .and. iaccesstype .eq. 5) ) then
         istatus=9
         if ( ilockup .eq. -2 ) then
          istatus=2
         elseif ( ilockup .eq. -1 ) then
          call upgradelockprompt(2,istatus)
         elseif ( iverb .eq. 1 ) then
          write(6,*)
     * '        comment:  Upgrade to "EXCL-LOCK" for write access.'
         endif
        elseif ( ilock .eq. 0 .and. iaccesstype .ne. 5 ) then
         if ( ilockup .lt. 0 ) then
          write(6,*) 'Cannot upgrade from no lock to EXCL-LOCK'
          write(6,*) 'You must run mergelock to do this'
         elseif ( iverb .eq. 1 ) then
          write(6,*)
     * '       comment:  Upgrade to "EXCL-LOCK" for write access.'
         endif
        else
         istatus = 2
        endif

       elseif (ilock .eq. 0) then

        istatus = 9
        if ( ilockup .eq. -2 .and. iaccesstype .eq. 5) then
         istatus=2
        elseif ( ilockup .eq. -1 .and. iaccesstype .eq. 5) then
         call upgradelockprompt(2,istatus)
        elseif (  iverb .eq. 1 ) then
         if ( ilockup .lt. 0 ) then
          write(6,*) 'Cannot upgrade from no lock to EXCL-LOCK'
          write(6,*) 'You must run mergelock to do this'
         elseif ( iverb .eq. 1 ) then
          write(6,*)
     *  '     comment:  Probe unlocked.  Excl-Lock probe to continue!'
         endif
        else
         istatus = 9
         if ( iverb .eq. 1 ) write(6,*)
     *  '        comment:  Probe is locked by another user!'
        endif

       endif
      endif

      if ( iverb .eq. 1 ) then
       if (istatus .eq. -1) then
        write(6,*)' granted access:  full (non-lockable)'
       elseif (istatus .eq. 0) then
        write(6,*)' granted access:  read-only'
       elseif (istatus .eq. 1) then
        write(6,*)' granted access:  minor read-write'
       elseif (istatus .eq. 2) then
        write(6,*)' granted access:  EXCL read-write'
       elseif (istatus .eq. 9) then
        write(6,*)' granted access:  --> DENIED <--'
       endif
      endif

      return
      end
