#!/usr/bin/perl

#
# Script that replaces the DPM server old name with the new one (given as arguments) 
# in the DPNS and DPM tables.
#
# Author : Sophie Lemaitre (sophie.lemaitre@cern.ch)
# Date : 08/11/2005
#

use strict;
use warnings;
use Getopt::Long;
use DBI;
use Env qw(ORACLE_HOME);

use UpdateDpmDatabase;

sub usage($) {
  my $reason = shift(@_);
  print <<EOF and   die "\nWrong usage of the script: $reason.\n";
usage: $0 --db-vendor db_vendor --db db --user user --pwd-file pwd_file [--old-srm old_srm --new-srm new_srm] [--old-disk old_disk_server --new-disk new_disk_server] [--dpns-db dpns_db --dpm-db dpm_db] [--verbose]

The old and new SRM and Disk Server arguments should be the FQDNs of the machines.

This script replaces the old name of the SRM and Disk Server machines with their new name in the DPNS and DPM databases, if needed.
If there are several Disk Servers, just run the script several times.

The dpns-db and dpm-db arguments have to be specified only if the database backend is MySQL.

EOF
}

# Create arguments variables...
my ($db_vendor, $db, $user, $pwd_file, $old_srm, $new_srm, $old_disk_server, $new_disk_server, $dpns_db, $dpm_db, $verbose);

# ... and read the arguments
GetOptions("db-vendor:s", => \$db_vendor,
           "db:s", => \$db,
           "user:s", => \$user,
           "pwd-file:s", => \$pwd_file,
           "old-srm:s", => \$old_srm,
           "new-srm:s", => \$new_srm,
           "old-disk:s", => \$old_disk_server,
           "new-disk:s", => \$new_disk_server,
           "dpns-db:s", => \$dpns_db,
           "dpm-db:s", => \$dpm_db,
	   "verbose" => \$verbose );

# check CLI consistency
usage("The database vendor must be specified. It can either be \'Oracle\' or \'MySQL\'") unless(defined $db_vendor);
usage("The DPNS database user must be specified") unless(defined $user);
usage("The file containing the DPNS database password must be specified") unless(defined $pwd_file);

if ($db_vendor eq "MySQL") {
	usage("The DPNS MySQL database must be specified") unless(defined $dpns_db);
        usage("The DPM MySQL database must be specified") unless(defined $dpm_db);
        usage("The MySQL server host must be specified") unless(defined $db);
}
elsif ($db_vendor eq "Oracle") {
        usage("The Oracle database SID must be specified") unless(defined $db);
}
else {
	usage("The database vendor can either be \'Oracle\' or \'MySQL\'");
}

usage("Either the SRM or the Disk Server old and new FQDNs must be specified") unless(defined $old_srm || defined $old_disk_server);

if (defined $old_srm || defined $new_srm) {
	usage("The old and new FQDNs of the SRM machine must be be specified") unless(defined $old_srm && defined $new_srm);
}

if (defined $old_disk_server || defined $new_disk_server) {
        usage("The old and new FQDNs of the Disk Server must be be specified") unless(defined $old_disk_server && defined $new_disk_server);
}

# useful variables
my ($start_time, $time, $end_time);
my $pwd;
my ($dbh_dpns, $dbh_dpm);
my $select;
my ($count, $count0, $count1, $count2, $count3);


#
# read database password from file
#

open(FILE, $pwd_file) or die("Unable to open password file");
my @data = <FILE>;
$pwd = $data[0];
$pwd =~ s/\n//;
close(FILE);

eval {

$start_time = localtime();
print "$start_time : Starting to replace the DPM name.\n"; 
print "Please wait...\n";

if ($db_vendor eq "Oracle") {
        $dpns_db = $user;

	#
	# Check ORACLE_HOME is defined
	#
	if (!defined($ORACLE_HOME) ) {
	    print STDERR "Error: ORACLE_HOME is not set! Check your Oracle installation.\n";
	    exit(1);
	}

	my @drivers = DBI->available_drivers;
	if ((my $result = grep  /Oracle/ , @drivers) == 0){
	    print STDERR "Error: Oracle DBD Module is not installed.\n";
	    exit(1);
	}

}

$dbh_dpns = Common::connectToDatabase($dpns_db, $user, $pwd, $db_vendor, $db);
$dbh_dpm = Common::connectToDatabase($dpm_db, $user, $pwd, $db_vendor, $db);

#
# Update DPNS table
#
$count = UpdateDpmDatabase::updateDpmCnsFileReplicaTable($dbh_dpns, $old_disk_server, $new_disk_server);

#
# Update DPM tables
#
$count0 = UpdateDpmDatabase::updateDpmFsTable($dbh_dpm, $old_disk_server, $new_disk_server);
$count1 = UpdateDpmDatabase::updateDpmCopyFileReqTable($dbh_dpm, $old_srm, $new_srm);
$count2 = UpdateDpmDatabase::updateDpmGetFileReqTable($dbh_dpm, $old_srm, $new_srm, $old_disk_server, $new_disk_server);
$count3 = UpdateDpmDatabase::updateDpmPutFileReqTable($dbh_dpm, $old_srm, $new_srm, $old_disk_server, $new_disk_server);


$dbh_dpns->commit;
$dbh_dpm->commit;

$dbh_dpm->disconnect;
$dbh_dpns->disconnect;


#
# The migration is over
#

$end_time = localtime();
print "$end_time : The update of the DPNS/DPM databases is over\n\n";
print "$count entries have been migrated in the 'Cns_file_replica' table.\n";
print "$count0 disk server names have been modified in the 'dpm_fs' table.\n";
print "$count1 entries have been migrated in the 'dpm_copy_filereq' table.\n";
print "$count2 entries have been migrated in the 'dpm_get_filereq' table.\n";
print "$count3 entries have been migrated in the 'dpm_put_filereq' table.\n\n";

if ($verbose) {
        print "db vendor = $db_vendor\n";
	print "db = $db\n";
	print "database user = $user\n";
	print "database password = xxxxxx\n";

	if ($db_vendor eq "MySQL") { 
	        print "DPNS database name = $dpns_db\n";
                print "DPM database name = $dpm_db\n";
	}

	if (defined $old_srm) {
	        print "old SRM name = $old_srm\n";
        	print "new SRM name = $new_srm\n";
	}

	if (defined $old_disk_server) {
	        print "old Disk Server name = $old_disk_server\n";
        	print "new Disk Server name = $new_disk_server\n";
	}
}

};
die ("failed to query and/or update the DPNS/DPM databases : $@") if ($@);

