#!/usr/local/bin/rexx

CALL rmfile "leptospira"
CALL rmfile "address"
CALL rmfile "mash"
CALL rmfile "author"

verbose=0

PARSE ARG file
IF STRIP(file)="" THEN DO
   CALL Log "You should at least specify input file name."
   EXIT 
   END 
IF ReadFile(file)<0 THEN DO
   CALL Log 'Error: Can not open input file "'file'"'
   EXIT 
   END  

SAY "radim ..."

cnt=0
DO i=1 TO line.0
   cnt=cnt+1
   PARSE VAR line.i index.cnt '-' red.cnt
   IF index.cnt="\/-[]-\/" THEN DO
      red.0=cnt-1
      CALL Obrada 
      cnt=0
      END 
END

EXIT

Line: PARSE ARG sta
IF strip(sta)="" THEN sta="="
SAY COPIES(sta, 78/length(sta))
RETURN

LOG: PARSE ARG Sta
SAY time('n')'> 'Sta
RETURN

ReadFile: procedure EXPOSE file line.
queue_old=QUEUED()
'/bin/cat' file '>LIFO'
queue=QUEUED()
lines=queue-queue_old
DO i=lines TO 1 BY -1
   PARSE PULL line.i
END 
line.0=lines
RETURN line.0

rmfile: PROCEDURE EXPOSE verbose
PARSE ARG file
"/bin/rm" file
RETURN

WriteFile: PROCEDURE EXPOSE verbose
PARSE ARG file"|"what
CALL Lineout file,what
RETURN 0

Obrada: PROCEDURE EXPOSE red. index. verbose
UId=0
DO i=1 TO red.0
   IF index.i="UI" THEN DO
      ui=red.i
      PARSE VAR red.i UId';' .
      uid=STRIP(uid)
      red.i=""
      END  
END 
IF DATATYPE(uid)<>"NUM" THEN DO
   IF verbose>0 THEN 
      CALL Log "Invalid UI datatype ("uid")"
   RETURN 
   END 

DO i=1 TO red.0
   IF strip(red.i)="" THEN ITERATE
   IF index.i="AU" THEN DO
      IF verbose>9 THEN
         CALL Log "AU: "uid"^"red.i
      rc=WriteFile("author|"uid"^"red.i)
      END
   ELSE IF index.i="TI" THEN DO
      title=red.i
      IF verbose>9 THEN
         CALL Log "TI: "title
      END
   ELSE IF index.i="AB" THEN DO
      IF verbose>9 THEN
         CALL Log "AB: "uid"^"red.i
      CALL rmfile "/usr/local/leptospira/"uid
      rc=WriteFile("/usr/local/leptospira/"uid"|"red.i)
      END
   ELSE IF index.i="MH" THEN DO
      PARSE VAR red.i MH"|"type
      IF verbose>9 THEN
         CALL Log "MH: "uid"^"mh"^"type
      rc=WriteFile("mash|"uid"^"mh"^"type)
      END
   ELSE IF index.i="AD" THEN DO
      IF verbose>9 THEN
         CALL Log "AD: "uid"^"red.i
      rc=WriteFile("address|"uid"^"red.i)
      END
   ELSE IF index.i="SO" THEN DO      
      source=red.i
      IF verbose>9 THEN
         CALL Log "SO: "source 
      END
   ELSE IF index.i="DP" THEN DO
      PARSE UPPER VAR red.i year mnth . 
      nm=(index("JAN FEB MAR APR MAY JUN JUL AUG SEP OCT NOV DEC", mnth)+3)/4
      IF nm<1 | nm>12 THEN DO
         IF verbose>0 THEN 
            CALL Log "Month value is invalid:" nm"?="mnth"???" 
         END  
      ELSE IF LENGTH(year)<>4 THEN DO
         IF verbose>0 THEN 
            CALL Log "Year value is invalid: "year"???" 
         year="1997"
         END  
      ELSE 
         IF verbose>9 THEN 
            CALL Log "Year:" year", month:" nm "("mnth")"
      datum=year""RIGHT("00"nm, 2)
      IF verbose>0 THEN
         CALL Log "DP: "datum
      END
   ELSE DO
      IF verbose>8 THEN 
         CALL Log "Field ("index.i") not used by this application."
      END
END 

rc=WriteFile("leptospira|"uid"^"title"^"datum"^"source)
SAY "leptospira|"uid"^"title"^"datum"^"source

IF verbose>0 THEN 
   CALL Line "+"
RETURN
