#!/afs/athena/contrib/perl/perl
#

unshift(@RCSID, '$Id$');

### use other relevant files

unshift(@INC, '/afs/athena/user/b/e/bert/project/volmove');
require('agetopt.pl');

### defaults

$DEFlogfile = '/usr/tmp/vos_move.log.'; # plus the PID of the current process

### usage info

sub usage {
    local($err) = @_;
    local($prog) = $0;
    $prog =~ s@.*/@@g;
    $err = "\n$prog: $err\n" if $err;

    print <<"EndOfUsage";
$err
Usage: $prog [options] from-host[.part] to-host.part [cell [logfile]]
Options:
  -b    automatically back up all volumes that don't start in "n."
        or end in ".nb"
        [default: back up those which are currently backed up]
  -r    don't automatically replicate and release volumes on new site
        [default: do replicate and release]
  -o    do not remove old read-only volumes
        [default: remove old read-only volumes]

  -n    just say what we WOULD do
  -v    verbose
  -d    debugging output (probably useless to you)

Omitting the "from" partition means *all* partitions on that host.
Not specifying the cell may lead to authenticantion problems (if it's
something other than athena).
Logfile defaults to $DEFlogfile<pid>

Ctrl-C will abort the operation *after* the move in progress is finished.
Thus it should be safe to Ctrl-C out of $prog while it is performing
vos operations you normally can't safely Ctrl-C out of, like "vos move".

EndOfUsage
    exit 0;
}

### main body

# arg parsing

# $agetopt'debug=2;

($err = &agetopt('-backupbyname', "-unreplicated\000-r", "-keepro\000-o",
		 '-noaction', '-verbose', '-debug',
		 '-from:', '-to:', '-cell:', '-logfile:',
		 "\000-xyzzy:"))
    && &usage($err);

$debug      = $aopt{'-debug'};
$verbose    = $aopt{'-verbose'};
$dontdoit   = $aopt{'-noaction'};

$autobackup = $aopt{'-backupbyname'};
$keepro     = $aopt{'-keepro'};
$norelease  = $aopt{'-unreplicated'};

$shost = $aopt{'-from'} || &usage("Missing required field `-from'");
if ($shost =~ /^(\S+)\.(.)$/) {
    ($shost,$spart) = ($1,$2);
}

$dhost = $aopt{'-to'} || &usage("Missing required field `-to'");
if ($dhost =~ /^(\S+)\.(.)$/) {
    ($dhost,$dpart) = ($1,$2);
} else { &usage("Missing or garbled partition in required field `-to'"); }

if ($afscell = $aopt{'-cell'}) {
    $cell = "-cell $afscell";
}

$logfile = $aopt{'-logfile'} || ($DEFlogfile . $$);



print "** debug      = $debug\n";
print "** verbose    = $verbose\n";
print "** dontdoit   = $dontdoit\n";
print "** autobackup = $autobackup\n";
print "** keepro     = $keepro\n";
print "** norelease  = $norelease\n\n";
print "*< shost      = $shost\n";
print "*< spart      = $spart\n";
print "*> dhost      = $dhost\n";
print "*> dpart      = $dpart\n\n";
print "** cell       = $cell\n";
print "** logfile    = $logfile\n";
