#!/usr/bin/perl
#
# This script creates the schema for the user accounts
#
# Author: James Casey <james.casey@cern.ch>
# Author: Miguel Anjo <miguel.anjo@cern.ch>
# Author: Sophie Lemaitre <Sophie.Lemaitre@cern.ch>
#
# $Id: create-schema-lfc,v 1.2 2005/05/24 21:13:14 slemaitr Exp $
#

use strict;
use warnings;

use Getopt::Long;
use Env qw(ORACLE_SID ORACLE_HOME);
	  
#
# Configuration constants.  These could be changed if required
#

# location to download schemas to
my $tempSchemaDir = "/tmp/schemas";

#################################################################
# start of script - do not change past here
#################################################################

#
# forwards
#
sub configuration();
sub usage($);
sub installSchema($$$);  

#
# check env variables
# 
die "ORACLE_HOME environment variable not defined.\n" 
	if !defined($ORACLE_HOME);	
die "ORACLE_SID environment variable not defined.\n" 
	if !defined($ORACLE_SID);	

#
# get options
#
my ($schema, $name, $password);
my $verbose=0;

GetOptions("schema=s" => \$schema,
     	   "name:s" => \$name,
	   "password=s" => \$password,
	   "v" => \$verbose);

usage("The LFC password must be specified") if !$password;
usage("The name must be specified. The LFC user will be called LFC_<NAME>") if !$name;

# capitalize
$name =~ tr/a-z/A-Z/;

#prefix the name with "LFC_"
$name="LFC_${name}";

configuration() if $verbose;

# schema  used by default
if (!$name) {
	$schema = "/opt/lcg/share/LFC/db-deployment/create_lfc_oracle_tables.sql";
}

installSchema($name, $password, $schema);

print "Done.\n" if $verbose;
`rm -f /tmp/schemas/*.$$`;
exit;

#################################################################
# end of script
#################################################################
sub usage($) {
    my $error = shift @_;
    print <<EOF and die "Wrong usage of the script : $error\n";
usage : $0 --name=name --password=password 
           [--schema=location] [--v]

Options
    name      name       The username for the LFC account will be "LFC_<NAME>"
    password  password   Password for the LFC account
    schema    location   The location on disk of the LFC schema. Defaults to
			 to /opt/lcg/share/LFC/db-deployment/create_lfc_oracle_tables.sql
    v                    verbose mode

Example: To install the schema located in /tmp/foo for the user "LFC_FOO" :
    create-schema --name=foo --password=hello 
                  --schema=/tmp/foo/create_lfc_tables_oracle.sql

EOF

}

sub configuration() {
	print <<EOF;	
Configuration :
    ORACLE_HOME     : $ORACLE_HOME
    ORACLE_SID      : $ORACLE_SID
EOF
}

# 
# install the schema.  We actually wrap the given schema script with a
# dynamically created one which sets up logging and error handling
sub installSchema($$$) {
    my($name, $password, $schemaLocation) = @_;

    my $logFile="/tmp/create-schema.$$.log";
    print "Running SQLPLUS for user : $name\n" if $verbose;
    # start sqlplus
    my $command="$ORACLE_HOME/bin/sqlplus $name/$password";
    open(SQLPLUS, "| $command > /dev/null") or die "can't start SQLPLUS";
    print SQLPLUS <<EOF;

-- setup error handling and logging
--

WHENEVER OSERROR EXIT FAILURE ROLLBACK
--WHENEVER SQLERROR EXIT SQL.SQLCODE ROLLBACK
spool $logFile

\@$schemaLocation

EOF
close SQLPLUS;
    
    if($? != 0 ) {
	die "Error while running sqlplus : see $logFile for more details";
    }
    
    unlink $logFile;
}
