/***************************************************************************/
/* TempTool.spot (C) Phil O'Malley 1993, Fidonet: 2:250/107.96             */
/*                                                                         */
/* $VER: TempTool v1,1 [02.9.93]                                           */
/***************************************************************************/
/*                                                                         */
/* Usage: Call the script as a separate program immediately before your    */
/*        text editor, ie. from an execution script, specifing the path    */
/*        of the temp file on the CLI.                                     */
/*                                                                         */
/*          eg. rx TempTool.spot dh0:T/Spot.temp                           */
/*                                                                         */
/*        Installation of the script requires conciderable alteration of   */
/*        your Spot reply headers and additions to your userlists. Please  */
/*        see the accompanying documentation (TempTool.doc).               */
/*                                                                         */
/***************************************************************************/
/*                                                                         */
/* Other: TempTool.spot is freely distributable provided its documentation */
/*        file accompanies it, and both remain unaltered. The copyright is */
/*        retained by P.A.O'Malley (2:250/107.96@fidonet), and thus the    */
/*        distribution is *not* public domain.                             */
/*                                                                         */
/***************************************************************************/

options results

version = 'TempTool v1.1'

/* Firstly grab the filename to peruse */ 

parse arg temp_file

if temp_file = '' then do
  say 'Usage: rx TempTool.spot <filename>'
  exit
end

if exists(temp_file) = 0 then do
  say 'Error: File '''temp_file''' not found!'
  exit
end

/***************************************************************************/
/* Check for, and install if necessary, any libraries that are needed.     */
/***************************************************************************/

/* For the statef() function */

if ~show(l, "rexxsupport.library") then do
  if ~addlib("rexxsupport.library", 0, -30) then do
    say "Error: Can''t find 'rexxsupport.library' in libs:"
    exit
  end
end

/***************************************************************************/
/* Define global variables                                                 */
/***************************************************************************/

/* Logical assigns */

true    = 1
false   = 0
newline = '0a'x

/* Intra-script marker assigns. Notice that the intro markers are not really
   necessary to make the script work, but they do ensure a roughly constant
   execution speed on a 68000, particularly with long messages. */

marker_intro_start = '!intro_start'||newline
marker_intro_end   = '!intro_end'||newline

marker_name_start  = '!#'
marker_name_end    = '#!'

/* Intra-script path assigns */

userlist.1 = 'spot:userlist_fidonet'
userlist.2 = 'spot:userlist_amiganet'

/***************************************************************************/
/* Grab the data from the temp file and alter it...                        */
/***************************************************************************/

/* Grab the data from the tempory file */

call open(file,temp_file,'r')
temp_data = readch(file,filelength(temp_file))
call close(file)

/* Call the AlterIntro procedure */

temp_data = alterintro(temp_data,marker_intro_start,marker_intro_end,marker_name_start,marker_name_end)

/***************************************************************************/
/* Save the altered data back to the original file name and quit...        */
/***************************************************************************/

call open(file,temp_file,'w')
writech(file,temp_data)
call close(file)

exit

/***************************************************************************/
/* AlterIntro procedure.                                                   */
/***************************************************************************/

alterintro: procedure expose userlist. newline true false

  parse arg temp.0,intro_start_marker,intro_end_marker,name_start_marker,name_end_marker

  /* Grab the position of the intro markers. */ 

  intro_start = index(temp.0,intro_start_marker,1)
  intro_end   = index(temp.0,intro_end_marker,1)

  /* If either are bust then the following will recognise it and pop the
     procedure. */

  if (intro_end-intro_start) ~> 0 then return temp.0

  /* Strip the intro markers, leaving a copy of what was held within in the
     variable intro.0. */ 

  intro.1 = left(temp.0,(intro_end-1))
  intro.0 = right(intro.1,(length(intro.1)-(intro_start+(length(intro_start_marker)-1))))

  /* Now that we have just the reply header in one easy to access variable,
     we can now look for any name markers. */

  name_start = index(intro.0,name_start_marker,1)
  name_end   = index(intro.0,name_end_marker,1)

  /* And execute this loop until no more are found. */

  do until (name_end-name_start) ~> 0
  
    /* Now purge the name from the markers and hold it in name.0 */
 
    name.1 = right(intro.0,(length(intro.0)-name_start-(length(name_end_marker)-1)))
    name.0 = left(name.1,(name_end-name_start-length(name_end_marker)))

    /* Because there might be multiple userlists to scan through to find a
       match, I've put the scanning routines into their own procedure which
       returns either the contents of the first userlist which matched the
       parameters, or a junk variable. */

    userlist = chooseuserlist()

    /* And now locate the position of the entry concerned. */

    read_posn = index(userlist,name.0,1)

    /* If a valid userlist was returned then the following loop is entered
       into, otherwise it is skipped. */

    if read_posn ~= 0 then do

      /* Grab the exact position of the userlist entry. */

      entry_start = read_posn
      entry_end   = index(userlist,newline,read_posn)

      /* And now the position of the comment annotation */

      comment_start = index(userlist,";",read_posn)

      /* If this entry has no comment, then the following loop will not be
         entered into. */

      if (comment_start ~= 0) then do
        if (comment_start ~> entry_end) then do

          /* This "-2" business is because Spot annotes its comments with a
             "; " string, hence I'm chopping it off. */

          entry.1 = right(userlist,((length(userlist)-(comment_start-1))-2))
          entry.0 = left(entry.1,((entry_end-comment_start)-2))

          name.0 = entry.0

        end
      end 
    end
 
    /* And now reconstruct the reply header. Intro.1 and intro.2 are just
       what is to the left and right of the markers. */

    intro.1 = left(intro.0,(name_start)-1)
    intro.2 = right(intro.0,(length(intro.0)-name_end-(length(name_end_marker)-1)))

    /* Hence, putting name.0 in between will either insert the nickname (if
       present) or, if not, the original. */

    intro.0 = intro.1||name.0||intro.2

    /* And prepare for the loop back. */ 

    name_start = index(intro.0,name_start_marker,1)
    name_end   = index(intro.0,name_end_marker,1)

  end
  
  /* temp.1 is what was originally before the intro_start_marker, and temp.2
     what was after the intro_end_marker. */

  temp.1 = left(temp.0,(intro_start-1))
  temp.2 = right(temp.0,(length(temp.0)-(intro_end+length(intro_end_marker)-1)))

return temp.1||intro.0||temp.2

/***************************************************************************/
/* ChooseUserlist() procedure, spawned from AlterIntro().                    */
/***************************************************************************/

ChooseUserlist: procedure expose userlist. name.0 false

  /* Stack the current userlist marker */

  current_userlist = 1

  /* Now, look through all the userlists */

  do until exists(userlist.current_userlist) = false
 
    call open(file,userlist.current_userlist)
    userlist_data = readch(file,filelength(userlist.current_userlist))
    call close(file)
    
    if index(userlist_data,name.0,1) ~= false then do
      return userlist_data
    end

    current_userlist = current_userlist + 1

  end

return "Non available fitting parameters."

/***************************************************************************/
/* Miscellaneous procedures...                                             */
/***************************************************************************/

/* Return the length of a file */

filelength: procedure

  parse arg file
  parse value statef(file) with type size blk bits day min tick com

return size
