/* ToTape.mrbk
 *
 * This ARexx program implements a generalized tape backup scheme which
 * assumes that a given partition is backed up to alternating media
 * depending upon which "cycle" is being performed. (A and B). The user
 * is prompted for the cycle designation (A or B) and the rest is automatic.
 *
 * This script requires a single argument comprised of two values
 * separated by a comma:
 *
 *      ToTape "<homepath>,<backpath>"
 *
 * where <homepath> is the name of the partition being backed up and
 * backpath is the tape device name. Thus, you could define an entry
 * in your MRBackup Macros menu such as:
 *
 *      MRBackup:Arexx_Scripts/ToTape DH0:,MRTape:
 *      (Only "ToTape DH0:,MRTape:" will show in the menu.)
 *
 * MRBackup will then perform a fully automated backup, using the
 * cycle name (A or B) as a suffix in constructing the save-set,catalog,
 * listing and log file names.
 */

signal on ERROR
signal on BREAK_C

options results
/* trace results */

if ~(Show('P', 'MRBackup_#1')) then do
    address command 'run "MRBackup:MRBackup"'
    address command 'WaitForPort "MRBackup_#1"'
end

address "MRBackup_#1"
poptofront

if arg() ~= 1 then do
usage:
    say "arg() = " arg()
    'notealert "usage: ToTape <homepath>,<backpath>"'
    exit 
end

/* Break up the argument string. The "." is a placeholder which assures
 * that all arguments are tokenized (pg. 79).
 */
parse arg homePath ',' backPath .
say "homePath = " || homePath;
say "backPath = " || backPath;
'getchoice "Which backup cycle?" "A" "B" "Cancel"'
cycle = result

if cycle = "Cancel" then do
    'notealert "This backup has been canceled."'
    exit
end

colonPos = pos(':', homePath)
if colonPos < 2 then do
    'notealert "You must specify a device name for the home path!"'
    exit
end

prefix = substr(homePath,1,colonPos - 1) || Backup || cycle
say "Prefix = " || prefix

'setfilemode "Replace"'

'setbackupmode "SCSI Tape"'

'setcomment "Full partition backup."'

'setcompression "None"'
'testarcbits "NO"'
'setarcbits "NO"'
'keepemptydirs "YES"'
setprefix prefix;
'setvoice "YES"'

'sethomepath' homePath
if rc ~= 0 then do
    say "sethomepath failed - " homePath
    exit 1
end

'setbackpath' backPath
if rc ~= 0 then do
    say "I failed to set the backup path: " backPath
    exit 1
end

catalogFile = "MRBackup:Catalogs/" || prefix || ".cat"
say "Catalog = " catalogFile

'setcatalogname' catalogFile
if rc ~= 0 then do
    say "setcatalogname failed - " catalogFile
    exit
end

logFile = "MRBackup:Lists_and_Logs/" || prefix || ".log"
setlogpath logFile

listFile = "MRBackup:Lists_and_Logs/" || prefix || ".list"
setlistpath listFile

/* setbufsize 60 */

'setbfilterpath " "'
'setcfilterpath " "'
'setdfilterpath " "'

takecontrol
backup
save_rc = rc
releasecontrol
if save_rc ~= 0 then do
    say "Backup failed; error code: " || save_rc
    exit 1
end

exit 0


/*------------------------------------------------------------------*/

break_c:

say "*** Control-C recieved.  Stopped by user. ***"
exit 5

/*------------------------------------------------------------------*/

error:

say "Error"
exit 6

/*------------------------------------------------------------------*/

