/* SaveOut.rexx by Anthony Brice - 29th February 1996 */

/* Feedback anthony@backyard.demon.co.uk - 2:254/255.1 */

/* Configuration stuff. Edit as required */

Spot_Path = 'Workbench:Spot/Spot'     /* Where is Spot? */
Archive=            'YES'             /* Set to no to not archive */
Archive_Line=       'Lzx -X0 a '      /* Archiver command line */
Archive_Append=     '.Lzx'            /* Archiver filename extension */
Directory=          'Workbench:Spot/' /* Full path to save to (Use : or /) */
Delete=             'NO'              /* Delete area afterwards? Carefull! */ 
DateStamp=          'YES'             /* (Yes/No) Append date to archive name */
TempDir=            'Ram:T/'          /* Temp save path (Use: or /) */
UserName=           'YES'             /* Use your echo names (Yes/No) */

/* Don't fiddle beyond this line unless you know what you're doing! */

Auto='NO'       /* Defaults to manual. If spot is not running then it
                   will supress all requestors and go into backup all mode */

If ~show('ports','SPOT') then do
  Address Command 'run 'spot_path
  'WaitForPort SPOT'
  Auto='YES'
  End

Address SPOT
Options Results

'uniconify'
'spot2front'

LF='0d'x
Version='1.1'
Date='29/2/96'

'isiconified'
If RC=0 then uniconify

/* Lee Kindness for Spot development - Subliminal support message! */

'progressclose 1'   /* make sure stray progress requestors are closed */

Mode=2              /* Defaults to all in case it's automated */

If Auto='NO' then Do
  String="   SaveOut V"||Version||" by Anthony Brice "||Date||"!"
  'requestresponse TITLE "'String'" PROMPT "Select Save Options :- "GADGETS "_Current Area|_All of the Areas|_Exit!"'
  If RC=0 then Exit
  Mode=RC
End

If Mode=1 then Do
  'ismessages'
  If RC~=0 then Do
    If Auto='NO' then do     /* Just in case :-) */
      'requestnotify "Call from Message window in Area Mode"'
    End
    Exit
  End
End

Areas=1 ; First=1  /* Just some flags */

If Mode=2 then Do
  'getnumareas'
  Areas=RESULT  /* if we're backing up all then we need to know total areas */
  'firstarea'   /* Jump to the first area */
End

Do until First=Areas
  'getareaname'
  Area = Translate(Result,'_',' ') /* We have the areaname for filename now */
  If UserName='YES' Then Do
    'getareausername'
    Area = Translate(Result,'_',' ') /* Check 4 spaces in Name & replace with _ */
  End
  If Mode=1 then Do
    'getmessagenum'
    CurrentMsg=Result  /* Grabs current message number to go back when done */
  End
  'getnummsgs'
  Msgs=Result+1        /* Now we have the total number of msgs in this area */
  If Msgs>1 then Do
    'messages'
    Cmsg=0 ; Added=0     /* flags for keeping track of progress */ 
    'firstmessage'       /* Skip to first message in current area */
    'progressopen TITLE "Creating 'Area' area message backup File" PROMPT "Please Wait.."'
    Prog=Result          /* We have to store the progress requester code */
    FileName=TempDir||Area  /* Filename for ascii save */
    Do Until cmsg=msgs   /* Looping the loop */   
      'saveascii TO 'FileName' NOREFLOW NOTEARLINE NOKLUDGES'
      If RC=0 then added=added+1
      'nextmessage'
      cmsg=cmsg+1
      'progressupdate' prog cmsg msgs 'PROMPT "'added' messages stored in file"'
      If RC>0 then Do
        'progressclose' prog
        Address Command 'Delete >NIL: 'TempDir||Area /* Tidy up if aborted */
        'unlockgui'    /* Always cautious */
        Exit 5
      End
    End
/* Mat Bettinson = Aussie Tosser! :-) */
    'progressclose' prog
    Blah='Area '||Area||' Messages have all been saved'
    FileName=Directory||Area
    Address Command 'Copy >NIL: 'TempDir||Area' 'FileName
    Address Command 'Delete >NIL: 'TempDir||Area
    If Archive='YES' then Do
      Address Command Archive_Line||' >NIL: '||FileName||Archive_Append' '||FileName
      Address Command 'C:Delete >NIL: 'FileName
      Blah=Blah||' and archived'
      /* General clean up stuff as it finishes an area */
    End
  End
  If Mode=2 then Do
    Blah='All message areas have been stored'
  End
  Blah=Blah||'.'
  If mode=2 then do
    'nextarea'
    First=First+1
  End
End

If Auto='NO' then do
  'requestnotify PROMPT "'Blah'"'  /* Message saying it's done */
End
If Mode=1 then Do   /* Only returns to original message in save area mode */
  'gotomessage TO "'CurrentMsg'"'
End
'unlockgui'      /* VERY IMPORTANT if you lock gui to free it again */
Exit

