#!/usr/bin/perl

#
# Script that migrates the entries from the EDG Replica Location Service (RLS)
# database to the new LCG File Catalog (LFC).
#

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

use FindBin;
use lib "$FindBin::Bin/../lib";
use Common;
use QueryAndUpdate;
use Atlas;

sub usage($) {
  my $reason = shift(@_);
  print <<EOF and   die "\nWrong usage of the script: $reason.\n";
usage: $0 --db-vendor db_vendor --host db --lrc-user lrc_user --lrc-passwd lrc_passwd --rmc-user rmc_user --rmc-passwd rmc_passwd --path path --lfc-sid lfc_sid --lfc-user lfc_user --lfc-passwd lfc_passwd [--verbose]

The RLS entries will all be migrated to the <path> directory.

If the LFN stored in the RMC is of the form "/path/to/LFN", it will be parsed, 
and the corresponding directories will be created.  
EOF
}

# Create arguments variables...
my ($vo, $db_vendor, $host, $lrc_user, $lrc_passwd, $rmc_user, $rmc_passwd, $path, $lfc_sid, $lfc_user, $lfc_passwd, $verbose);

# ... and read the arguments
GetOptions("db-vendor:s", => \$db_vendor,
	   "host:s", => \$host,
	   "lrc-user:s", => \$lrc_user,
	   "lrc-passwd:s", => \$lrc_passwd,
	   "rmc-user:s", => \$rmc_user,
	   "rmc-passwd:s", => \$rmc_passwd,
	   "path:s", => \$path,
	   "lfc-sid:s", => \$lfc_sid,
	   "lfc-user:s", => \$lfc_user,
	   "lfc-passwd:s", => \$lfc_passwd,
	   "verbose" => \$verbose );

# check CLI consistency
usage("The Database type must be specified. It can either be \'Oracle\' or \'MySQL\'") unless(defined $db_vendor);
usage("The LRC database user must be specified") unless(defined $lrc_user);
usage("The LRC user password must be specified") unless(defined $lrc_passwd);
usage("The RMC database user must be specified") unless(defined $rmc_user);
usage("The RMC user password must be specified") unless(defined $rmc_passwd);
usage("The path where to migrate the RLS entries must be specified") unless(defined $path);
usage("The LRC/RMC database host or Oracle SID must be specified") unless(defined $host);
usage("The LFC Oracle SID be specified") unless(defined $lfc_sid);
usage("The LFC database user must be specified") unless(defined $rmc_user);
usage("The LFC user password must be specified") unless(defined $rmc_passwd);


if ($db_vendor eq "Oracle") {
	usage("The RLS database Oracle SID must be specified") unless(defined $host);
} elsif ($db_vendor eq "MySQL") { 
	usage("The host where the MySQL server resides must be specified") unless(defined $host);
} else {
	usage("The Database type can only be \'Oracle\' or \'MySQL\'");
}

# useful variables
my ($start_time, $time, $end_time);
my ($dbh_lrc, $dbh_rmc, $dbh_lfc);
my ($lrc, $rmc);
my $select;
my $count;

eval {

$start_time = localtime();
print "$start_time : Starting to migrate data from the RLS to the LFC.\n"; 
print "Please wait...\n";

if ($db_vendor eq "Oracle") {
        $lrc = $lrc_user;
	$rmc = $rmc_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);
	}

} elsif ($db_vendor eq "MySQL") {
        $lrc = "edg_lrcdb";
	$rmc = "edg_rmcdb";
}


#
# First grant select privileges to the RMC user on the LRC tables
#
    $dbh_lrc = Common::connectToDatabase("edg_lrcdb", $lrc_user, $lrc_passwd, $db_vendor, $host);

    Common::grantSelectPrivileges($dbh_lrc, "guid", $rmc_user);
    Common::grantSelectPrivileges($dbh_lrc, "pfn", $rmc_user);

    $dbh_lrc->disconnect;


#
# Connect to the LFC database
# -> to directly add the creation time, as the C API only allows to set the time to the current time)
#

    $dbh_lfc = Common::connectToDatabase("cns_db", $lfc_user, $lfc_passwd, "Oracle", $lfc_sid);


#
# Then, connect to the RMC database and query all the information from the LRC and RMC tables
#
    $dbh_rmc = Common::connectToDatabase("edg_rmcdb", $rmc_user, $rmc_passwd, $db_vendor, $host);

    #$count = QueryAndUpdate::queryFromRLSAndUpdateLFC($dbh_rmc, $lrc, $rmc, $path);
    $count = Atlas::queryFromRLSAndUpdateLFC($dbh_lfc, $dbh_rmc, $lrc_user, $rmc_user);

    $dbh_rmc->disconnect;

    $dbh_lfc->disconnect;

#
# Finally, revoke the select privileges
#
    $dbh_lrc = Common::connectToDatabase("edg_lrcdb", $lrc_user, $lrc_passwd, $db_vendor, $host);

    Common::revokeSelectPrivileges($dbh_lrc, "guid", $rmc_user);
    Common::revokeSelectPrivileges($dbh_lrc, "pfn", $rmc_user);

    $dbh_lrc->disconnect;


#
# The migration is over
#

$end_time = localtime();
print "$end_time : The data migration from the RLS to the LFC is over : $count entries have been migrated.\n";

if ($verbose) {
	print "db vendor = $db_vendor";
	print "host = $host\n"; 
	print "lrc user = $lrc_user\n";
	print "rmc user = $rmc_user\n";
	print "lrc password = $lrc_passwd\n";
	print "rmc password = $rmc_passwd\n";
        print "path= $path\n";
}

};
die ("failed to query information from the LRC and RMC tables : $@") if ($@);

