/********************************************************
*                                                       *
*                   RiFtpPut.CMD                          *
*                                                       *
*    This script looks in a particular directory for    *
*    files.  When found, the files are uploaded to a    *
*    host using FTP file transfers.                     *
*                                                       *
********************************************************/
'@echo off'
/* TRACE ?I */

/*
** Load Rexx 'helper' functions
*/

if Rxfuncquery('SysLoadFuncs') <> 0 then do
    rcy = Rxfuncadd('SysLoadFuncs','RexxUtil','SysLoadFuncs')
    call SysLoadFuncs
end /* do */

rc = RxFuncQuery('FtpLoadFuncs')
/*if Rxfuncquery('FtpLoadFuncs') <> 0  then do */
   rcy = Rxfuncadd('FtpLoadFuncs','RxFtp','FtpLoadFuncs')
   call FtpLoadFuncs
/* end   Do */
rc = RxFuncQuery('FtpLoadFuncs')

call on HALT name Finish

/*
** Initialize constants used in program
*/

Host = 'tamsrv2'            /* Name of host computer */
Logon = 'wireless'          /* Logon to use */
Password = 'wireless'       /* Password */
HostDir = '/tmp'            /* Remote host directory */
SecondsToWait = 60          /* Seconds between checks for new data */
GraceSeconds = 1           /* Seconds after data file found */
errLogFile = '\riapps\dbutil\RiFtp.Err' /* Daemon monitors logged to this file */

/*
** Log onto the host and start looking for files
*/

say 'Logging onto FTP host...'
rc = FtpSetUser(Host,Logon,Password)
if rc<>1 then call FtpError 'FtpSetUser('Host','Logon','Password')',rc
rc = FtpChDir(HostDir)
if rc<>0 then call FtpError 'FtpChDir('HostDir')',FTPERRNO
rc = FtpSetBinary('Ascii')
if rc<>1 then call FtpError 'FtpSetBinary("Ascii")',rc

say 'Looking for files to transfer to host...'
do FOREVER
    call SysFileTree '*.0','File','FO'
    if File.0>0 then call XferFiles
    call SysSleep SecondsToWait
end /* do */

/*
** Data files have been found and placed in the global array File.
** This procedure transfers them to the host and deletes them from
** the local directory after the transfer.
*/

XferFiles:
   if file.0 <> 1
      then s = 's'
      else s = ''
   say 'Found' file.0 'file's'...'
   call Beep 200,50
   call SysSleep GraceSeconds      /* give Smalltalk a chance to finish */
   do i=1 to file.0
      filename = FILESPEC('name',file.i)
      call SendData filename
   end /* do */
   say 'Finished transferring file's'...'
return

/*
** Use the FTP DLL to send a single file to the host.  When
** sending the file, prepend it with the characters 'wait' to
** signal the host that the transfer is in progress.  Rename
** it after the transfer is finished, and delete the file from
** the current directory.
*/

SendData:
   parse arg filename
   total = TIME('r')
   rc=FtpPut(filename,'wait_'filename)
   total = TIME('e')
   if (rc<>0) then 
      if (FTPERRNO<>0) then call FtpError 'FtpPut('filename',wait_'filename')',FTPERRNO
   else do
      rc=FtpRename('wait_'filename,filename)
      say '   Data file' filename 'imported in' total 'seconds'
      call SysFileDelete filename
      call SysFileDelete logFileName
   end /* else do */
return

/*
** Finish up and exit
*/

Finish:
   say 'Closing FTP connection...'
   rc=FtpLogoff()
   call beep 400,100
   call beep 200,100
exit

PanicStop:
   say 'Stopping immediately...'
   call beep 400,100
   call beep 200,100
exit

/*
** Display FTP errors
*/

FtpError:
   parse arg FtpFunc,errnum
   ErrMsg = 'FTP function' FtpFunc 'returned error' errnum
   say '    'ErrMsg
   call LINEOUT errLogFile, DATE('N') TIME('N') 'Error:' ErrMsg
return
