/* This ARexx program strips a filenamme.guide file of @Node and @EndNode
 * tokens. It also takes a line in the form
 *
 *   @{"gadget text" Link GadgetInfo}
 *
 * and changes it to:
 *
 *   gadget text.
 *
 * Usage is: GuideToText file.guide
 *
 * The resulting file will be named file.guide.txt.
 *
*/

parse arg filename
signal on ioerr

  if filename = '?' then do
    Say 'Usage: GuideToText file.guide'
    Return 0
  end

  outfile = filename || '.txt'

  if Open(if,filename,'R') then do
    if Open(of,outfile,'W') then do
      Call stripit()
      Call Close(of)
    end; else
      Say 'Cannot open ' outfile .
    Call Close(if)
  end; else
    Say 'Cannot open ' || filename .
  Return 0

stripit:

  say "processing...   (be patient, this is ARexx!)"

  do until eof(if)
    l = ReadLN(if)
    p = Pos('@',l)                /* amigaguide token? */
    skip_line = 0
    if p ~= 0 then do
      p = Pos('@{"',l)            /* gadget? */
      if p ~= 0 then do
        l=DelStr(l,p,3)           /* delete @{" */
        p1 = Pos('" Link',l)      /* find Link keyword */
        if p1 ~= 0 then do
          p2 = Pos('}',l,p1)      /* find position of } */
          if p2 ~= 0 then do
            l = DelStr(l,p1,(p2-p1)+1)  /* delete Link function} */
          end
        end
      end; else do
        p = Pos('@Node',l) | Pos('@EndNode',l)  /* @Node or @EndNode ? */
        if p ~= 0 then
          skip_line = 1     /* don't write this line */
      end
    end
    if ~skip_line then
      Call WriteLN(of,l)
  end

  Return 0

ioerr:

  say IO Error
  exit