#!/usr/local/bin/rexx

verbose=0

PARSE ARG file
IF STRIP(file)="" THEN DO
   CALL Log "You should at least specify input file name."
   EXIT 
   END 
IF verbose>0 THEN 
   CALL Line 
IF ReadFile(file)<0 THEN DO
   CALL Log 'Error: Can not open input file "'file'"'
   EXIT 
   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 line. file verbose
queue_old=QUEUED()
'/bin/cat' file '>LIFO'
IF rc<>0 THEN RETURN -1
queue=QUEUED()
lines=queue-queue_old
mm=0;
DO i=lines TO 1 BY -1
   mm=mm+1
   PARSE PULL line.mm
   IF LEFT(line.mm, 6)="------" THEN DO 
      lines=mm
      CALL Lout
      mm=0
      END 
END 
line.0=mm
Exit
RETURN line.0

Lout:
lns=0
ll=''
DO j=1 TO lines
   IF STRIP(line.j)='' THEN ITERATE
   IF LEFT(line.j, 5)='-----' THEN ITERATE
   IF INDEX(line.j, 'Other Formats:')>0 THEN ITERATE
   IF INDEX(line.j, 'Links:')>0 THEN ITERATE   
   IF LEFT(line.j, 3)='   ' THEN DO
      ll=STRIP(line.j)' 'll
      END 
   ELSE DO
      ll=STRIP(line.j)' 'll
      lns=lns+1
      PARSE VAR ll index.lns '-' red
      red.lns=STRIP(red)
      ll=''
      END
END 
IF ll<>'' THEN DO
   lns=lns+1
   PARSE VAR ll index.lns '-' red.lns
   END
index.0=lns 
red.0=lns

Call Obrada
RETURN


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 

abstract=""
authors=""
mash=""
address=""

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
      IF STRIP(authors)>"" THEN 
         authors=red.i", "authors
      ELSE 
         authors=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
      abstract=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
      mash=mh"&"type"|"mash
      END
   ELSE IF index.i="AD" THEN DO
      IF verbose>9 THEN
         CALL Log "AD: "uid"^"red.i
      IF STRIP(address)>"" THEN
         address=red.i" ,"address
      ELSE
         address=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"^"address"^"authors"^"abstract)

IF verbose>0 THEN 
   CALL Line "+"
RETURN

