;UploadAndSave.WAS     v1.0

;*****************************************************************************
;*                                                                           *
;* UploadAndSave.WAS                                                         *
;* Copyright (C) 1998 Symantec                                               *
;* All rights reserved.                                                      *
;*                                                                           *
;* Written by:  Symantec ASPECT Assist                                       *
;* 04/24/98                                                                  *
;*                                                                           *                                                                             
;* This script file will check a directory for files, and then upload those  *
;* files to a host system, and then move the files to a save directory.      *
;* After the file is uploaded and moved, the script will rename the          *
;* the file to the current date (example:  123098).                          *
;*                                                                           *
;*                                                                           *
;* This ASPECT script is intended only as a sample of ASPECT programming.    *
;* Symantec makes no warranty of any kind, express or implied, including     *
;* without limitation, any warranties of merchantability and/or fitness      *
;* for a particular purpose.  Use of this program is at your own risk.       *
;*                                                                           *
;* Technical Support is not provided on this script.                         *                                                                          *
;*****************************************************************************


;****************************************************************************
;* MACROS definitions                                                       *
;****************************************************************************
#define fWATCHDIR "C:\watch\"            ;The directory to watch for files.
#define fBACKUPDIR "C:\backup\"          ;The directory to watch for files.
#define fLOGFILE "C:\backup\logfile.txt" ;The error/log file.
#define szENTRY "The Bank"               ;The connection entry to dial.
#define szLOGIN "XXXXX"                  ;The login id.
#define szPASSWORD "xxxxx"               ;The login password.


;****************************************************************************
;* Main                                                                     *
;****************************************************************************
proc Main

   startprocess()
endproc


;****************************************************************************
;* StartProcess                                                             *
;****************************************************************************
proc StartProcess

   string szMsg, szFile
   
   if fopen 0 fLOGFILE APPEND TEXT       ; Open log file for read only.
      logit("***************************************************")
      strfmt szMsg ("Date: %s") $date    ;Info for log file.
      logit(szMsg)
      strfmt szMsg ("Time: %s") $time
      logit(szMsg)
      logit("")
   else
      statmsg "Error - Couldn't open the log file `"%s`"." fLOGFILE  
      ;Error if not able to create log file.
   endif
   
   WatchIt(&szFile)                      ;Watch for files.
   yield
   SendIt(szFile)                        ;Connect and upload file to host.
   MoveIt(szFile)                        ;Move file to backup dir.
   logit("Done.")
   pwexit                                ;Exit script and Procomm.
endproc


;*******************************************************************************
;* WatchIt                                                                     * 
;*******************************************************************************
proc WatchIt
param string szFile

   string szDir, szMsg

   szDir = fWATCHDIR
   strcat szDir "*.*"
   while 1
      yield
      if findfirst szDir                 ;Find first file matching spec.
         szFile = $filespec              ;Get filename.
         strfmt szMsg ("File found: %s") $filespec
         logit(szMsg)
         exitwhile
      endif
   endwhile
endproc


;*******************************************************************************
;* SendIt                                                                      * 
;*******************************************************************************
proc SendIt
param string szFile

   integer iTemp

   set dial retries 3                    ;Set dial retries to 3.
   dial DATA szENTRY                     ;Dial the host system.
   while $dialing                        ;Loop while dialing.
      yield
   endwhile
   pause 2
   if $carrier                              ;Check to see if connected.
      logit("Connected to host system.")
      transmit "^M"                         ;Get system prompt.
      waitfor "logon"
      mspause 500
      transmit szLOGIN                      ;Send login id.
      mspause 500
      transmit "^M"
      waitfor "password"
      mspause 500
      transmit szPASSWORD                   ;Send password id.
      mspause 500
      transmit "^M"
;     waitfor "zmodem" 10                   ;REMARKED OUT - CHANGE IF NEEDED BY HOST
      iTemp = 1                             ;Set iTemp value to 1 (for loop.)
      sendfile ZMODEM szFile                ;Upload file using ZMODEM.
      while iTemp == 1                      ;While file transfer is going on. 
         iTemp = $XFERSTATUS                ;Store value in $XFERSTATUS in iTemp
         yield                              ;Yield processor time.
      endwhile
      if iTemp == 2                         ;Test the value of iTemp for success
         Logit("File successfully sent.") 
      else                                  ;or failure of file transfer.
         Logit("File transfer failed!.")
      endif
      hangup                                ;Hangup from host.
      pause 2
      yield
   else                                     ;If not connected log error.
      logit("Problems connecting to host system.")
   endif  
endproc


;*******************************************************************************
;* MoveIt                                                                      * 
;*******************************************************************************
proc MoveIt
param string szFile

   string szNewFile, szTemp, szExt
   
   szNewFile = fBACKUPDIR                ;Make new filename.
   strextract szTemp $date "/" 0         ;Add month to filename.
   strcat szNewFile szTemp
   strextract szTemp $date "/" 1         ;Add date to filename.
   strcat szNewFile szTemp
   strextract szTemp $date "/" 2         ;Add year to filename.
   strcat szNewFile szTemp
   splitpath szFile szTemp szTemp szTemp szExt  ;Get original file ext.
   strcat szNewFile "."
   strcat szNewFile szExt                ;Add file ext to new filename.
   if rename szFile szNewFile
      strfmt szTemp ("%s  moved to  %s") szFile szNewFile  ;Info for log file.
      logit(szTemp)
   else
      statmsg "Error moving file!"
      strfmt szTemp ("Error moving file  %s  to  %s") szFile szNewFile
      logit(szTemp)
   endif      
endproc


;*******************************************************************************
;* LogIT                                                                       * 
;*******************************************************************************
proc LogIT
param string szMsg

   fputs 0 szMsg                               ;output to logfile.
endproc


