/******************************************************************
**                                                               **
** $VER: StealTag 1.2 (10.9.94) by Preben Nielsen                **
**                                                               **
*******************************************************************
**                                                               **
** This script uses Spot's ability to use taglines stored        **
** in a file.                                                    **
**                                                               **
** The script searches the current message for a tagline,        **
** i.e. a line starting with '... '. Any found tagline will      **
** be presented in a string-requester for further editing        **
** before being saved to file.                                   **
**                                                               **
** Handles multi-line taglines.                                  **
**                                                               **
*******************************************************************
**                                                               **
** Template:                                                     **
**   StealTag TAGLINEFILE                                        **
**                                                               **
** Example:                                                      **
**   StealTag MAIL:Spot/Taglines/Custom                          **
**                                                               **
**   This grabs the tagline from the current message and         **
**   appends it to the file 'MAIL:Spot/Taglines/Custom'.         **
**   The file will be created if it does not already exist!      **
**                                                               **
**   TAGLINEFILE will default to 'MAIL:Spot.tags' if not         **
**   specified.                                                  **
**                                                               **
*******************************************************************
**                                                               **
** Author: Preben Nielsen           FidoNet :  2:236/19.14       **
**                                  AmigaNet: 39:141/104.14      **
**                                  E-mail  : prel@scala.ping.dk **
**                                                               **
******************************************************************/

PARSE ARG tagfile
OPTIONS RESULTS
SIGNAL  ON IOERR

CALL ADDLIB("rexxsupport.library",0,-30,0)
CALL ADDLIB("rexxreqtools.library",0,-30,0)

ADDRESS 'SPOT'

progname = 'StealTag'
tempfile = 'T:'||progname||'.temp'

/*
** The next line is the string used for identifying the tagline.
*/
tagmark  = '... '
tagskip  = LENGTH(tagmark)
tagspace = COPIES(' ',tagskip)

tagtext  = ''
realLF   = '0a'x
fakeLF   = '\n'

IF STRIP(tagfile) = "" THEN tagfile = 'MAIL:Spot.tags'

'ismessages'
IF RC ~= 0 THEN DO 
  'requestresponse TITLE 'progname' PROMPT "Must be in the messages area!" GADGETS "Proceed"'
  EXIT
END

'saveascii TO 'tempfile' OVERWRITE NOHEADER NOTEARLINE NOORIGIN NOKLUDGES NOREFLOW'

/*
** Try to find a tagline. Replace any linefeed with '\n' for later editing.
*/
gottag   = 0
donetag  = 0
CALL OPEN('ifile',tempfile,'R')
allmsg   = READCH('ifile',65535)
tagstart = LASTPOS('0a'x||tagmark,allmsg)
IF tagstart ~= 0 THEN DO
   CALL SEEK('ifile',tagstart,'B')
   DO WHILE ~EOF('ifile') & ~donetag
      iline = READLN('ifile')
      IF gottag = 1 THEN
         IF INDEX(iline,tagspace) = 1 THEN
            tagtext = tagtext || fakeLF || STRIP(SUBSTR(iline,tagskip+1),'T')
         ELSE
            donetag = 1
      ELSE IF INDEX(iline,tagmark) = 1 THEN DO
         tagtext = STRIP(SUBSTR(iline,tagskip+1),T)
         gottag  = 1
      END
   END
END
CALL CLOSE('ifile')
CALL DELETE(tempfile)

IF gottag = 0 THEN
  'requestnotify "Cannot seem to find a tagline!"'
ELSE DO
  /*
  ** Let the user edit the tagline.
  */
  prompt  = "Edit the tagline if necessary." || realLF || "Linefeeds are represented by '" || fakeLF || "'."
  tagtext = rtgetstring(tagtext,prompt,progname)
  IF rtresult = 1 THEN DO
     /*
     ** Replace all '\n' with linefeeds and write the tagline
     ** to the destination file.
     */
     pos = INDEX(tagtext,fakeLF)
     DO WHILE pos > 0
        tagtext = SUBSTR(tagtext,1,pos-1) || realLF || SUBSTR(tagtext,pos+LENGTH(fakeLF))
        pos = INDEX(tagtext,fakeLF,pos+1)
     END
     IF ~EXISTS(tagfile) THEN
        CALL OPEN('ofile',tagfile,'W')
     ELSE DO
        CALL OPEN('ofile',tagfile,'A')
        CALL WRITELN('ofile','%%')
        END
     CALL WRITELN('ofile',tagtext)
     CALL CLOSE('ofile')
     END
  END
EXIT

IOERR:
  SAY 'IO-error in line' SIGL
  EXIT
