#!/usr/bin/perl
#
# move_volumes_by_name.pl -- Locate and move AFS volumes by volume name
# Copyright (C) 2011 NC State University
# Written by Jack Neely <jjneely@ncsu.edu>
#
# SDG
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

use strict;
use AFS::VOS;
use AFS::VLDB;
use Getopt::Std;

my %opts = ();
my $debug = 0;

getopts("t:P:c:hd", \%opts);

if ((exists ($opts{'h'})) || (!exists($opts{'t'})) || 
   (!exists($opts{'P'})) || (!exists($opts{'c'}))) {
 print "Usage: $0 -t to_server -P to_partition -c cell [-h help] [-d debug]\n";
 print "Reads a list of volumes passed as arguments or from STDIN to locate\n";
 print "and move to target.\n";
 exit 1;
}

if (exists($opts{'d'})) {
   $debug = 1;
}

my $cell      = $opts{'c'};
my $to        = $opts{'t'};
my $to_part   = $opts{'P'};

my $vos = AFS::VOS->new(0,30,0,0,$cell);
print "Error: ", $AFS::CODE, "\n" if ($AFS::CODE);

my $vldb = AFS::VLDB->new(0,30,0,0, $cell);
print "Error: ", $AFS::CODE, "\n" if ($AFS::CODE);

my $volcount = 0;
my %volumes;
my %temp;

# Parse out seprate strings into a temp hash
if ($ARGV[0] && $ARGV[0] ne "-") {
    foreach (@ARGV) {
        $temp{$_} = 1;
    }
} else {
    while (<STDIN>) {
        while ( /(\w[\.\w-]*)/g ) {
            $temp{$1} = 1;
        }
    }
}

# Parse the temp hash keys and figure out what's a volume
foreach (sort keys %temp) {
    print "Parsing: $_\n";
    my $vldblist = $vldb->listvldbentry($_);
    if ($AFS::CODE) {
        print "   Error: ", $AFS::CODE;
        next;
    }
    my $flag = 0;
    foreach my $val (keys %$vldblist) {
        if ($flag > 1) {
            print "   Confused -- more than one VLDB entry returned\n";
            last;
        }
        $flag++;
        if ( $val =~ /\.backup$/ ) {
            print "   Not moving backup volume $val\n";
            last;
        }
        if ($vldblist->{$val}->{nServers} > 1) {
            print "   Not moving replciated volume $val\n";
            last;
        }
        foreach my $srv (@{$vldblist->{$val}->{server}}) {
            print "$val -> (" . %{$srv}->{name} . ", " . 
                %{$srv}->{partition} . "\n";
            $volumes{$val}->{'name'} = %{$srv}->{name};
            $volumes{$val}->{'partition'} = %{$srv}->{partition};
            $volcount++;
        }
    }
}

print "\nMoving $volcount volumes.\n";
my $c = 0;

foreach my $v (keys %volumes) {
    $c++;
    my $srv = $volumes{$v}->{'name'};
    my $par = $volumes{$v}->{'partition'};
    print "Moving $v from $srv : $par -> $to : $to_part\n";
    print "\tVolume $c of $volcount\n";

    if (!$debug) {
        my $volid = $vos->move($v, $srv, $par, $to, $to_part);
        if (!$volid) {
            die "Error: $!: $volid\n";
        }
        my $ok = $vos->backup($v);
        if (!$ok) {
            print "Failed to make a backup volume for $v\n";
        }
    }
}

print "Done!\n";

exit 0;

