/*
**  RIBACKUP.CMD
**
**  Script to back up the RiApps directory tree using ZIP and FTP.
**  Roos Instruments (c) 2003,2004
**
**  Usage:
**      RIBACKUP SystemName RootDir BackupDir Option
**  Where:
**      SystemName = unique name for your RI Tester with no spaces or special
**          characters (e.g. Roos12)
**      RootDir = root directory of RiApps files (e.g. D:\RiApps)
**      BackupDir = directory to hold backup ZIP file created
**      Option = [DIFF | NOZIP | ] 
**          DIFF creates a smaller backup file which has only the changed
**              files since the last full backup
**          NOZIP skips the creation of the ZIP backup file and proceeds
**              directly to the FTP transfer
**          (empty) creates a full zip backup file and sends it to the server
**              via FTP
**
*/

'@echo off'
/* trace '?I' */

/* CUSTOMIZE THE SERVER LOGON */
server = ''                     /* IP address or name */
logon = 'logon'                 /* FTP logon name */
pw = 'password'                 /* FTP logon password */
/* CUSTOMIZE THE EXCLUDED FILES */
exclude.0 = 3
exclude.1 = '*.zip'
exclude.2 = '*.??2'
exclude.3 = 'testdata\*'
/* END OF CUSTOMIZE SECTION */

if RxFuncQuery('SysLoadFuncs')\=0 then do
    call RxFuncAdd 'SysLoadFuncs', 'RexxUtil', 'SysLoadFuncs'
    call SysLoadFuncs
end /* do */
if RxFuncQuery('FtpLoadFuncs')\=0 then do
    call RxFuncAdd 'FtpLoadFuncs', 'rxFtp', 'FtpLoadFuncs'
    call FtpLoadFuncs
end /* do */

parse value arg(1) with sysName rootdir backdir opt

say ' '
say ' '
newdir = Directory(rootdir)
cmdline = 'zip -rqo'
holdfile = backdir'\'sysName
diffdate = ''
if translate(opt)\='NOZIP' then do
    if translate(opt)='DIFF' then do
        parse value stream(holdfile'.zip','c','query datetime') with mm '-' dd '-' yy filetime
        diffdate = mm || dd || yy
        if diffdate='' then do
            say ' '
            say 'No previous full backup, creating full backup now...'
        end /* then */
        else do
            holdfile = holdfile'-diff'
            cmdline = cmdline || 't' diffdate
        end /* else */
    end /* then */
    rc = SysFileDelete(holdfile'.zip')
    cmdline = cmdline holdfile '*'
    if exclude.0>0 then
        do i=1 to exclude.0
            cmdline = cmdline '-x' exclude.i
        end /* do */
    say ' '
    say 'Now creating zip backup file 'Filespec('name',holdfile)'...'
    '@echo on'
    cmdline
    '@echo off'
end /* if */

if server='' then call finish

say ' '
say 'Now transferring zip backup file to server...'
rc = FtpPing(server,56)
if rc<0 then do
    say ' '
    say 'Server not available... press [enter] to quit.'
    parse pull junk
    exit(1)            /* server not available */
end /* if */

rc = FtpSetUser(server,logon,pw)
if rc\=1 then do
    say ' '
    say 'Logon to server failed... press [enter] to quit.'
    parse pull junk
    exit(2)
end /* if */

rc = FtpPut(holdfile'.zip',sysName'.zip','Binary')
if rc\=0 then do
    say ' '
    say 'Backup file failed to transfer to server... press [enter] to quit.'
    parse pull junk
    exit(3)
end /* if */

rc = FtpLogoff()

finish:

say ''
say 'Finished!'

call SysSleep 1
exit(0)
