***************************** LIBEDIT.CMD
************* Allows user to look up a reference
************* by primary author name, and change
************* data.  Automatically updates the
************* keyword history file.

************* Set up databases and memory variables.
SELE PRIM
USE LIBRARY INDEX AUTHORS,DATES
SELE SECO
USE KEYWORDS INDEX KEYWORDS
SELE PRIM
STORE "              " TO K1,K2,K3,K4,K5,K6,K7

************** Find out who to edit.
ERASE
?
ACCEPT " Enter Main Author's last name " TO SEARCH
STORE !(SEARCH) TO SEARCH
ERAS

*************** Determine if 0, 1 or many references
*************** By this author, and react accordingly.
FIND &SEARCH


STORE 0 TO HOWMANY
COUNT WHILE !(AUTHOR) = SEARCH TO HOWMANY


DO CASE

   CASE HOWMANY = 0
        STORE 0 TO RECNO
        ?
        ? " There is no &SEARCH on the database."
        ?
        WAIT

   CASE HOWMANY = 1
        FIND &SEARCH
        STORE # TO RECNO

   CASE HOWMANY > 1
        FIND &SEARCH
        LIST WHILE LNAME = &SEARCH AUTHOR, TITLE, PUB
        ?
        INPUT " Which one (by number) " TO RECNO

ENDCASE

*********** Edit appropriate record, if recno > 0
ERASE
IF RECNO > 0

   GOTO RECNO
   *************** Peel out individual keywords from
   *************** the long KEYWORDS field in the
   *************** library database.
   STORE 2 TO START
   STORE 1 TO COUNT
   STORE KEYWORDS TO TEMP
   DO WHILE "," $(KEYWORDS)
      STORE STR(COUNT,1) TO SCOUNT
      STORE $(KEYWORDS,START,@(",",KEYWORDS)-2) TO K&SCOUNT
      REPLACE KEYWORDS WITH $(KEYWORDS,@(",",KEYWORDS)+1,50)
      STORE COUNT+1 TO COUNT
   ENDDO (while "," $keywords)

   ************* just in case user deletes any or all
   ************* keywords, subtract 1 from the total number
   ************* of references for this keyword from
   ************* The keywords file.
   SELE SECO
   STORE 1 TO X
   STORE "1" TO MAC
   DO WHILE K&MAC <> " "
      FIND &K&MAC
      REPLACE KEYNO WITH KEYNO-1
      STORE X+1 TO X
      STORE STR(X,1) TO MAC
   ENDDO

   ****** Now branch to LIBED2 to do the edit.
   ****** (LIBED2 is stolen from the LIBADD command file.)
   SELE PRIM
   DO LIBED2
ENDIF (recno > 0)

**************** Before returning to main menu,
**************** Eliminate any keywords for which there 
**************** are no references
ERASE
? " Updating keyword file.........."
SELE SECO
DELE ALL FOR KEYNO=0
PACK

RETURN
