#!/usr/bin/perl
#
# This script creates data and index tablespaces for a single VO.
#
# Author: James Casey <james.casey@cern.ch>
#
# 2004/12/14 : Changes made for the LCG File Catalog (LFC) by Sophie Lemaitre <Sophie.Lemaitre@cern,ch>
#

use strict;
use warnings;

use Getopt::Long;
use Env qw(ORACLE_SID ORACLE_HOME);
	  
#
# forwards
#
sub configuration();
sub usage($);

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

die "Could not find sqlplus binary at $ORACLE_HOME/bin/sqlplus"
	  if ! -x "$ORACLE_HOME/bin/sqlplus";

#
# Configuration constants.  These can be changed on the command line if
# required
#

# Location of the data partition.  Files will be placed in the directory
# $dbDatadir/$ORACLE_SID
my $dbDatadir="/ORA/dbs03/oradata";

# initial size of the tablespace file
my $initSize ="128M";

# increment interval when tablespace is increased
my $incrSize="100M";

# maximum size of the tablespace
my $maxSize="20000M";

my $name;
my $verbose=0;

GetOptions("name=s" => \$name, 
	   "v" => \$verbose, 
	   "datadir=s" => \$dbDatadir,
	   "init-size=s" => \$initSize,
	   "incr-size=s" => \$incrSize,
	   "max-size=s" => \$maxSize);

usage("No user name Specified") if !$name;
# capitalize
$name =~ tr/a-z/A-Z/;

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

my $prefix="${dbDatadir}/${ORACLE_SID}/${name}";

my $logFile="/tmp/create-tablespaces.$$.log";

configuration() if $verbose;

print "Running SQLPLUS...\n" if $verbose;
# start sqlplus
open(SQLPLUS, "| $ORACLE_HOME/bin/sqlplus \"/ as sysdba\" > /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

-- -----------------------------------------
-- Creating tablespaces for ${name}
-- -----------------------------------------
-- DATA
CREATE TABLESPACE "${name}_DATA"
DATAFILE '${prefix}_DATA_01.dbf' SIZE $initSize AUTOEXTEND ON NEXT $incrSize
MAXSIZE $maxSize EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;

-- INDEX
CREATE TABLESPACE "${name}_IDX"
DATAFILE '${prefix}_IDX_01.dbf' SIZE $initSize AUTOEXTEND ON NEXT $incrSize
MAXSIZE $maxSize EXTENT MANAGEMENT LOCAL
SEGMENT SPACE MANAGEMENT AUTO;

EOF
close SQLPLUS;

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

unlink $logFile;

print "Done.\n" if $verbose;
exit;

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

Options
    --name       name    The LFC database user will be called "LFC_<NAME>"
    --datadir    dir     The name of the oracle data directory to use.
                         Defaults to "/ORA/dbs03/oradata"
    --init-size  size    Initial tablespace size. Defaults to $initSize
    --init-size  size    Tablespace increment size. Defaults to $incrSize
    --max-size  size     Maximum tablespace size. Defaults to $maxSize
    --v                  verbose mode
EOF
}

sub configuration() {
	print <<EOF;	
Configuration :
    ORACLE_HOME : $ORACLE_HOME
    ORACLE_SID  : $ORACLE_SID
    DB Data Dir : $dbDatadir
    Name        : $name
EOF
}
