;FileXfer_Host.WAS     v1.0

;*****************************************************************************
;*                                                                           *
;* FileXfer_Host.WAS                                                         *
;* Copyright (C) 1998 Symantec                                               *
;* All rights reserved.                                                      *
;*                                                                           *
;* Written by:  Symantec ASPECT Assist                                       *
;* 01/07/98                                                                  *
;*                                                                           *                                                                             
;* This script file acts as a Host. The Host waits for a data call and       *
;* interacts with FileXfer_Client.WAS. This script opens a log file,         *
;* waits for a call, downloads files, then uploads files, changes the upload *
;* directory and then uploads more files, and then shuts down.               *
;*                                                                           *
;* 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.                         *                                                                          
;*                                                                           *
;*****************************************************************************


;*******************************************************************************
;* Define                                                                      * 
;*******************************************************************************
#define InboxDir   "c:\import\"                ;Files coming in.
#define Outbox1Dir "c:\export\"                ;Outbox 1 dir.
#define OutDone1   "c:\export\done\"           ;Outbox 1 done dir.
#define Outbox2Dir "c:\transfer\"              ;Outbox 2 dir.
#define OutDone2   "c:\transfer\done\"         ;Outbox 2 done dir.
#define LogFile    "c:\logfile.txt"            ;The logfile dir.


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

   string szMsg

   while 1
      if fopen 0 LogFile READAPPEND TEXT           ; Open log file for read only.
         logit("***************************************************")
         logit("")
         logit("Starting Process!")
         strfmt szMsg ("Date: %s") $date           ;Prepare the date
         logit(szMsg)                              ;Log the date
         strfmt szMsg ("Time: %s") $time           ;Prepare the time
         logit(szMsg)                              ;Log the time
         logit("")
      else
         errormsg "Couldn't open the log file `"%s`"." logFile
      endif
      StartProcess()
      logit("Done!")                              ;Update log file.
      logit("")
      logit("***************************************************")
      fclose 0                                    ;Close the log file.
   endwhile
endproc


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

   GoWait()
   GoGetFiles()
   GoSendFiles(OUTBOX1DIR, OutDone1)
   GoChange()
   GoSendFiles(OUTBOX2DIR, OutDone2)
   GoFinish()
endproc


;*******************************************************************************
;* GoWait                                                                      * 
;*******************************************************************************
proc GoWait

   clear
   statmsg "Waiting for call"               ;Tell the user autoanswer is set
   LogIt("Waiting for call")                ;Log the message
   set modem autoanswer PW                  ;Set Procomm to autoanswer
   set modem acceptcall data ON             ;Set Procomm to answer data
   while $carrier < 1                       ;Loop while there is no connection
      yield
   endwhile
   when $carrier call LostCall
   waitfor "Hello" 10                       ;Wait for the Host to respond
   transmit "Ready!^M^J"                    ;Tell the host the client is ready
endproc


;*******************************************************************************
;* LostCall                                                                    * 
;*******************************************************************************
proc LostCall

   LogIt("Lost Connection!")                ;Log the lost connection
   LogIt("")
   when clear
   GoWait()
endproc


;*******************************************************************************
;* GoGetFiles                                                                  * 
;*******************************************************************************
proc GoGetFiles

   string szDDir

   when $carrier call LostCall                  ;When the connection is on call
   						;LostCall procedure
   						
   when $xferstatus call FileStatus             ;When the transfer takes place
    						;call FileStatus procedure
    						
   fetch dnldpath szDDir                        ;Get the download path
   set dnldpath InBoxDir                        ;Set the download path to InBoxDir
   if chdir InBoxDir
      ; path exists.
   else
      errormsg "Path %s does not exist!" InBoxDir
   endif
   mspause 250
   set Zmodem receiver overwrite SKIP           ;Don't overwrite if the file is
   						;already on the system.
   waitfor "Done" forever
   set dnldpath szDDir				;Set the download path to what's in szDDir
   when clear
   mspause 250
endproc


;*******************************************************************************
;* FileStatus                                                                  * 
;*******************************************************************************
proc FileStatus

   string szMsg

   if $xferstatus == 2                                ;If the file transferred ok
      strfmt szMsg "File %s received." $FileName      ;Tell the user about it
      LogIt(szMsg)                                    ;Log the status message
   endif
endproc


;*******************************************************************************
;* GoSendFiles                                                                 * 
;*******************************************************************************
proc GoSendFiles
param string szFile
param string szOutFile

   string szMsg
   integer iXferTemp
   
   strcat szFile "*.*"                         ;Make the OUTBOX path.

   pause 2
   if findfirst szFile                         ;Find first file matching spec.
      sendfile ZMODEM $Filespec                ;Send files in OUTBOX path.
      mspause 250
      iXferTemp = $xferstatus
      while iXferTemp == 1                     ;Loop while sending files.
         iXferTemp = $xferstatus
         yield
      endwhile
      if iXferTemp == 2                        ;If files sent successfully.
         strfmt szMsg "File %s sent successfully." $fileSpec
         logit(szMsg)                          ;Tell the user about it
         szMsg = szOutFile                     ;Change the user message
         strcat szMsg $filename                ;add the file name to the message
         rename $filespec szMsg                
         mspause 500
         strfmt szMsg "File %s deleted from OUTBOX." $fileSpec
         logit(szMsg)                          ;Log message to file.
         yield
      else
         strfmt szMsg "*** File %s not sent successfully!" $fileSpec
         logit(szMsg)                          ;Log failure message to file
      endif
      pause 1
      
      while findnext                           ;If more files exist.
         statmsg "Sending %s" $filespec
         sendfile ZMODEM $Filespec             ;send those files.
         mspause 500
         iXferTemp = $xferstatus
         while iXferTemp == 1
            iXferTemp = $xferstatus            ;Loop while sending files.
            yield
         endwhile
         if iXferTemp == 2                     ;If files send successfully.
            strfmt szMsg "File %s sent successfully." $fileSpec
            logit(szMsg)
            szMsg = szOutFile
            strcat szMsg $filename
            rename $filespec szMsg
            strfmt szMsg "File %s deleted from OUTBOX." $fileSpec
            logit(szMsg)                          ;Log message to file.
            mspause 500
            yield
         else
            strfmt szMsg "*** File %s not sent successfully!" $fileSpec
            logit(szMsg)                          ;Log failure message to file
         endif
      endwhile
   else
      strfmt szMsg "No files to send in %s." szFile
   endif
   pause 2
   transmit "Done sending files!^M^J"                          ;Transmit Done.
   pause 2
endproc


;*******************************************************************************
;* GoChange                                                                    * 
;*******************************************************************************
proc GoChange

   if waitfor "Dir Changed" 15
      logit("Dir changed received.")               ;Log the directory change
   else
      logit("---Dir changed not received!")        ;Log the failure   
      logit("---Going to next file directory.")    ;Log the next attempt
   endif
endproc


;*******************************************************************************
;* GoFinish                                                                    * 
;*******************************************************************************
proc GoFinish

   LogIt("Done with Call.")                        ;Log the call completion
   LogIt("")
   when clear
   pause 1
   hangup
   pause 5
   yield
   clear
endproc


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

   fputs 0 szMsg
endproc


