SUBROUTINE ADKEY4(LKYNM,LKTXT, LKYFL,index4,kerr4)
!
      implicit none
!
!     ADKEY adds a key to the catalog.
!
!     Input variables:
!        1. LKYNM   -   The ten character code of the key to be added.
!        2. LKTXT   -   The text descriptor for the key.
!        3. LKYFL   -   A one I*2 flag word for the user.
!
      integer*2 LKYNM(5),LKTXT(*), key_hold(5), lkyfl
      integer*4 index4, kerr4
      character*10                 key_holdc
      Equivalence (key_hold, key_holdc)
!
!     Output variables:
!
!        index4  - The index number of the key record created.
!        kerr4   - Error return flag (0 - normal return,
!                 -1 - Catalog not open,
!                 -2 - No free space - did not add key,
!                 -3 - LKYNM already in catalog.)
!
      INCLUDE 'catcm.i'
!
!     To:
!     NKEY   - The number of the currently active recond.
!     KEY    - The access code of the currently active key.
!     KYREC  - The record number of the key to the currently
!              active key.
!     From:
!     Ikey   - The catalog system status flag.
!
!     From & to:
!     bufi   - The catalog record buffer.
!
      integer*4 ierr4, iirec, jdum, kdum, nempt
      integer*4 i, j, idum
      INTEGER*4 I4P0
      INTEGER*4 I4P1
      integer*4 endian_flip_i4
      DATA I4P0 /0/
      DATA I4P1 /1/
!
!        ierr4   - Erorr flag from subroutine calls
!        NREC   - pointer to the record where the key will be stored. .
!        IIREC  - Pointer to the last record on the chain of keys.
!        ICESS  - Access number of the last key already in the catalog.
!
!     Programmer: Jim Ryan May 15, 1978
!     Modifications:
!     :79.03.01:jwr: To add catbac code and change error stops
!     :81.07.06:M W: To remove fast access scheme and include
!                    added hashfile scheme.
!     :92.05.01:jwr: UNIX & I*4
!     :93.06.23:jwr: Typing and dimensioning change to convert (1)'s
!                    in (*) so -C option of compile will work.
!     :93.06.28:jwr: Real error in copying lkynm to lkey found and fixed.??
!     :93:11:12:kdb: Remove $LONG for compatibility with sun.  Now pass
!                    variable, not constant, to kyinf4, readc, chnon4.
!     :98.03.11:kdb: Rename getep to getep4.
!     :03.08.07:kdb: For Lahey LINUX, add endian flipping for reads/writes
!                    from/to the catalog record variables bufi and bufj.
!                    (Bufc is okay -- it isn't subject to endian conversions.)
!             
!
!
!     Make a string version of lkynm
      Do i = 1,5
        key_hold(i) = lkynm(i)
      enddo
!
!     Set ICNBF to indicate the buffer belongs to the system.
!     Verify that the catalog system is open and see if the
!     key specified by 'LKYNM' is already in the catalog.
!     If it is set kerr4 and get out.
!
      ICNBF = 1
      IF (IKEY.NE.1)  then ! catalog not open.
        kerr4=-1
        RETURN
      endif
!
      CALL KYINF4(LKYNM,I4P0,IDUM,JDUM,KDUM,kerr4)
      IF(kerr4.eq.0)then
        kerr4 = -3
        return
      endif
!
!     Go pull a record from the chain of empty records nad return with
!     it number.
!
  200 CALL GETEP4(NEMPT,ierr4)
      IF (ierr4.NE.0)  then !No free space
        write(6,'("adkey4: No free space.")')
        kerr4 = -2
        RETURN
      endif
!
!     Get the record number of the last key in the chain of keys.
      CALL READC(I4P1,ierr4)
      IIREC = endian_flip_i4(bufj(i4_words_in_user_area +3))
      CALL READC(IIREC,ierr4)
!
!     Now load the bufi information field with the new key information.
!     but first clear out bufi.
      CALL BUFCL()
      DO j=1,5
           bufi(J) = LKYNM(J)
      enddo
      bufj(4) = endian_flip_i4(NEMPT)
      bufi(6) = LKYFL
      DO  J=1,30
        bufi(10+J) =LKTXT(J)
      enddo
!
!     Now start the new string with only this record.
      bufj(i4_words_in_user_area+1) = endian_flip_i4(-1* nempt)
      bufj(i4_words_in_user_area+2) = endian_flip_i4(nempt)
      bufj(i4_words_in_user_area+3) = endian_flip_i4(nempt)
!
!     Write the record into the catalog
      CALL WRITC(NEMPT)
!
!     String the record onto the chain of keys.
      CALL CHNON4(NEMPT,IIREC,I4P1,ierr4)
!
!     Make this key active.
!
      NKEY = NEMPT
      KYREC = NEMPT
      NREC = NEMPT
      IFRST = NEMPT
      ILAST = NEMPT
      DO I=1,5
        KEY(I) = LKYNM(i)
      enddo
      index4 = NREC
      CALL READC(NREC,ierr4)
      kerr4 = 0
!
!     Use INSRT to update the HASH FILE for this new key
      CALL insert_into_hash(key_holdc,NREC)
!
      RETURN
      END