/* ; $VER: Another Stupid Tagline Generator 0.7 (5/18/94) By Jeff Grimmett
**
** Initial Design 9/10/93
**
** Public Release version 0.7
*/

Options Results
ARG MessageFile

/* This is where the user-customizable parameters are.  You must
** change them to fit your system before you can use it.
*/

TagFile = "Mail:/April/Quotes"     /* This is, of course, the file
                                   ** that contains your taglines.
                                   ** You must specify the full
                                   ** path and filename.
                                   */

Editor1 = "TurboText:TTX"          /* The first part of the argument
                                   ** line to call your editor.
                                   */

Editor2 = "WAIT"                   /* The second part of the argument
                                   ** line to call your editor.  See
                                   ** the documentation.
                                   */

/* Checks for the existence of a few files, bails out if they aren't
** found.
*/

If (~exists(MessageFile) | (MessageFile = "")) then do
   say "Message not found!"
   exit
   end

If (~exists(TagFile) | (TagFile = "")) then do
   say "Tagline file not found!"
   exit
   end


/* Search through tagfile and talley up the number of legal taglines
** contained in the file.
*/

NumberOfTags:

LEN = 0
Call Open(Tags,TagFile,"R")
Do Until(z = "###")
   z = READLN(Tags)
   If (Left(z,1) ~= '*' & z ~= "###" & (Length(z) > 0)) then LEN = LEN + 1
   end
Call Close(Tags)

/* This part of the program monitors to see if we're down to one legal
** tagline.  If so, we reset the tagline file markers and start over.
*/

If (LEN == 1) then do
   Call Open(NewTags,"Ram:NewTags","W")
   Call Open(OldTags,TagFile,"R")
   Do Until(EOF(OldTags))
      A = ReadLn(OldTags)
      If (Left(A,1) == "*") Then Do
         B = SubStr(A,2,Length(A))
         Call WriteLn(NewTags,B)
         End
      If ((Left(A,1) ~= "*") & (Length(A) > 0)) Then WriteLn(NewTags,A)
      End
   Call Close(OldTags)
   Call Close(NewTags)
   Address Command "Copy Ram:NewTags "||TagFile
   Address Command "Delete >NIL: Ram:NewTags"
   Signal NumberOfTags
   End


/*
** Choose and extract a tagline, marking it as used along the way.
*/

TagLineLine = Random(1,LEN,Time(s))

GetTagLine:

Index = 1
Call Open(NewTags,"Ram:NewTags","W")
Call Open(OldTags,TagFile,"R")
Do Until(EOF(OldTags))
   A = ReadLn(OldTags)
   If ((Left(A,1) == "*") | A == "###" ) Then Do
      Call WriteLn(NewTags,A)
      End
   If (Left(A,1) ~= "*" & (A ~= "###") & (Length(A) > 0)) Then Do
      B = "*"||A
      If (Index ~= TagLineLine) Then Call WriteLN(NewTags,A)
      If (Index == TagLineLine) Then Do
         Call WriteLn(NewTags,B)
         TagLine = A
         End
      Index = Index + 1
      End
   End
Call Close(OldTags)
Call Close(NewTags)
Address Command "Copy Ram:NewTags "||TagFile
Address Command "Delete >NIL: Ram:NewTags"


/*
** Once the tagline has been chosen, we add it to the message.
*/

Call Open(MsgFile,MessageFile,"A")

If (Index(TagLine,"\n") = 0) Then Do
   CALL WRITELN(MsgFile,TagLine)
   call WriteLn(MsgFile,"")
   call WriteLn(MsgFile,"-*- ASTG 0.7")
   End
Else Do
   TagLineLen = Length(TagLine)
   IndexStart = 1
   IndexEnd = 1

   Do While(IndexEnd < TagLineLen)
      IndexEnd = Index(TagLine,'\n',IndexStart)
      If IndexEnd = 0 then IndexEnd = TagLineLen+1
      Call WriteLn(MsgFile,SubStr(TagLine,IndexStart,IndexEnd-IndexStart))
      IndexStart = IndexEnd + 2
      End
   call WriteLn(MsgFile,"")
   call WriteLn(MsgFile,"-*- ASTG 0.7")
   End
Call Close(Tags)
Call Close(MsgFile)

/* Call editor
 *
 ***/

Address Command Editor1||' '||MessageFile||' '||Editor2

Exit

