#!/usr/bin/perl

#
# Script that adds the domain name to :
#	- the 'host' and 'sfn' fields in the "Cns_file_replica" table,
#	- the 'server' field in the "dpm_fs" table.
#
# 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 --domain-name domain_name --db-vendor db_vendor --db db --user user --pwd-file pwd_file [--dpns-db dpns_db --dpm-db dpm_db] [--verbose]

This script adds the domain name to the 'host' and 'sfn' fields in the DPNS database, if needed. It also updates the 'server' field in the DPM database, if needed.

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

EOF
}

# Create arguments variables...
my ($domain_name, $db_vendor, $db, $user, $pwd_file, $dpns_db, $dpm_db, $verbose);

# ... and read the arguments
GetOptions("domain-name:s", => \$domain_name,
           "db-vendor:s", => \$db_vendor,
           "db:s", => \$db,
           "user:s", => \$user,
           "pwd-file:s", => \$pwd_file,
           "dpns-db:s", => \$dpns_db,
           "dpm-db:s", => \$dpm_db,
	   "verbose" => \$verbose );

# check CLI consistency
usage("The domain name must be specified") unless(defined $domain_name);
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\'");
}

if ($domain_name eq "") {
	usage("The domain name specified is not correct");
}

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


#
# 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 add the domain 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);
	}

}


#
# update the DPNS database
#

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

$count = UpdateDpmDatabase::parseHostAndSfnAndUpdateDpm($dbh_dpns, $domain_name);
$count2 = UpdateDpmDatabase::parseAndUpdateServer($dbh_dpm, $domain_name);

$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 database is over\n";
print "$count2 disk server names have been modified in the configuration.\n";
print "$count entries have been migrated.\n";

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

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

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

