#!/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 Cms;

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 --path path --file /path/to/file [--verbose]

The RLS entries will all be migrated to the <path> directory.
The /path/to/file file is expected to contain entries in the following form :
	guid pfn RLS-lfn LFC-lfn

EOF
}

# Create arguments variables...
my ($vo, $db_vendor, $host, $lrc_user, $lrc_passwd, $path, $file, $verbose);

# ... and read the arguments
GetOptions("db-vendor:s", => \$db_vendor,
	   "host:s", => \$host,
	   "lrc-user:s", => \$lrc_user,
	   "lrc-passwd:s", => \$lrc_passwd,
	   "path:s", => \$path,
           "file:s", => \$file,
	   "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 path where to migrate the RLS entries must be specified") unless(defined $path);
usage("The file containing the entries must be specified") unless(defined $file);
usage("The LRC/RMC database host or Oracle SID must be specified") unless(defined $host);


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);
my ($lrc);
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;

	#
	# 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";
}

#
# connect to the LRC database
#

$dbh_lrc = Common::connectToDatabase("edg_lrcdb", $lrc_user, $lrc_passwd, $db_vendor, $host);


#
# read the entry file and update the LFC
# 

$count = Cms::queryFromRLSAndFileAndUpdateLFC($dbh_lrc, $lrc_user, $file);


#
# disconnect from the LRC database
#

$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 "lrc password = $lrc_passwd\n";
        print "path= $path\n";
}

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

