#!/usr/bin/perl

#
# Script that:
#	- either updates the LFC/DPNS database schema from version 3.0.0 to 3.1.0
#       - or queries and returns the LFC/DPNS database schema version
#
# Changes are done to support banning and
# to improve directory listings when there are many symbolic links
#
# Author : Jean-Philippe Baud (Jean-Philippe.Baud@cern.ch) based on:
# Author : Sophie Lemaitre (sophie.lemaitre@cern.ch)
# Date : 22/07/2010
#

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

use UpdateCnsDatabase;

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 [--cns-db cns_db] [--verbose] [--show-version-only]\n
The cns-db argument has to be specified only if the database backend is MySQL.

EOF
}

# Create arguments variables...
my ($db_vendor, $db, $user, $pwd_file, $cns_db, $verbose, $show_version);

# ... and read the arguments
GetOptions("db-vendor:s", => \$db_vendor,
           "db:s", => \$db,
           "user:s", => \$user,
           "pwd-file:s", => \$pwd_file,
           "cns-db:s", => \$cns_db,
	   "verbose" => \$verbose,
	   "show-version-only" => \$show_version );

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

if ($db_vendor eq "MySQL") {
        usage("The CNS MySQL database must be specified") unless(defined $cns_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\'");
}

# useful variables
my ($start_time, $time, $end_time);
my $pwd;
my ($dbh_cns);
my $count;


#
# 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();
unless ($show_version) {
  print "$start_time : Starting to update the LFC/DPNS database.\n";
  print "Please wait...\n";
}

if ($db_vendor eq "Oracle") {
	$cns_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);
	}

}

# connect to databases
$dbh_cns = Common::connectToDatabase($cns_db, $user, $pwd, $db_vendor, $db);

if ($show_version) {
  Common::querySchemaVersion($dbh_cns);
} else {
  # update the database schema
  if ($db_vendor eq "MySQL") {
	UpdateCnsDatabase::update_mysql($dbh_cns);
  }
  if ($db_vendor eq "Oracle") {
	UpdateCnsDatabase::update_oracle($dbh_cns);
  }
}

$dbh_cns->disconnect;


#
# The migration or query is over
#

$end_time = localtime();
unless ($show_version) {
  print "$end_time : The update of the LFC/DPNS database is over\n";
}

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

	if ($db_vendor eq "MySQL") {
                print "CNS database name = $cns_db\n";
	}
}

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