#!/usr/bin/perl
# IBM(c) 2007 EPL license http://www.eclipse.org/legal/epl-v10.html
#(C)IBM Corp

#

#-----------------------------------------------------------------------------

=head1   odbcsetup

 This postscript sets up the odbc for the database running on the Client
 machine usually the Service Node.
 The xCAT service node client and the DB client should have already been
 installed on the node, before this postscript is run.
=cut

#-----------------------------------------------------------------------------

BEGIN
{
    $::XCATROOT = $ENV{'XCATROOT'} ? $ENV{'XCATROOT'} : '/opt/xcat';
}

# if AIX - make sure we include perl 5.8.2 in INC path.
#       Needed to find perl dependencies shipped in deps tarball.
if ($^O =~ /^aix/i) {
    unshift(@INC, qw(/usr/opt/perl5/lib/5.8.2/aix-thread-multi /usr/opt/perl5/lib/5.8.2 /usr/opt/perl5/lib/site_perl/5.8.2/aix-thread-multi /usr/opt/perl5/lib/site_perl/5.8.2));
}

use lib "$::XCATROOT/lib/perl";
use strict;
use xCAT::Utils;
use xCAT::MsgUtils;

# MAIN
my $log_label=$ENV{'LOGLABEL'};
if (!$log_label) {
    $log_label="xcat"
}
my $rc = 0;
my $cmd;

# setup some important variable

$::sdate = `/bin/date`;
chomp $::sdate;


my $msg;

# get location of database
$::databaseloc = $ENV{'DATABASELOC'};

# check the /etc/xcat/cfgloc file to see if MySQL or DB2
# if it is not here,  error out
my $dbname = xCAT::Utils->get_DBName;
if ($dbname eq "DB2")
{
    $msg = "odbcsetup:Setting up ODBC for DB2";
    `logger -t $log_label -p local4.info $msg`;
    &setupdb2odbc;

}
else
{
    if ($dbname eq "MYSQL")
    {
        $msg = "odbcsetup:Setting up ODBC for MYSQL";
        `logger -t $log_label -p local4.info $msg`;
        &setupmysqlodbc;
    }
    else {
        if ($dbname eq "PG")
        {
            $msg = "odbcsetup:Setting up ODBC for PostgreSQL";
            `logger -t $log_label -p local4.info $msg`;
            &setupPGodbc;
        } else {
            $msg =
"odbcsetup:cfgloc file does not contain MySQL, DB2 or PG,  will not setup ODBC.";
            `logger -t $log_label -p local4.err  $msg`;
            exit 1;
        }
    }
}

exit 0;

#
# Subroutines
#

#####################################################
#
# setupdb2odbc
#     runs the db2sqlsetup script and sets up the ODBC on the Client
#
#####################################################
sub setupdb2odbc
{
    my $msg;
    my $rc = 0;
    my $cmd;
    my $filename = "/etc/xcat/cfgloc";

    # get the info
    my $xcatcfg;
    my $cfgl;
    open($cfgl, "<", $filename);
    $xcatcfg = <$cfgl>;
    close($cfgl);
    chomp $xcatcfg;
    my ($database, $instance, $password) = split('\|', $xcatcfg);
    if ($::databaseloc) {
        $cmd = "DATABASELOC=$::databaseloc $::XCATROOT/bin/db2sqlsetup -o -C";
    } else {
        $cmd = "$::XCATROOT/bin/db2sqlsetup -o -C";
    }
    $msg = "odbcsetup: Running Client ODBC setup. \"$cmd\"\n";
    `logger -t $log_label -p local4.info $msg`;

    $rc = &runcmd($cmd);

    $msg = "odbcsetup: Client ODBC setup finished.\n";
    `logger -t $log_label -p local4.info  $msg`;

    return $rc;

}
#####################################################
#
# setupmysqlodbc
#     runs the mysqlsetup script and sets up the ODBC on the Client
#
#####################################################
sub setupmysqlodbc
{
    my $msg;
    my $rc = 0;
    my $cmd;
    $cmd = "$::XCATROOT/bin/mysqlsetup -o";
    $msg = "odbcsetup: Running Client ODBC setup. \"$cmd\"\n";
    `logger -t $log_label -p local4.info $msg`;

    $rc = &runcmd($cmd);

    $msg = "odbcsetup: Client ODBC setup finished.\n";
    `logger -t $log_label -p local4.info $msg`;

    return $rc;

}

#####################################################
#
# setupPGodbc
#     runs the pgsqlsetup script and sets up the ODBC on the Client
#
#####################################################
sub setupPGodbc
{
    my $msg;
    my $rc = 0;
    my $cmd;
    $cmd = "$::XCATROOT/bin/pgsqlsetup -o";
    $msg = "odbcsetup: Running Client ODBC setup. \"$cmd\"\n";
    `logger -t $log_label -p local4.info $msg`;

    $rc = &runcmd($cmd);

    $msg = "odbcsetup: Client ODBC setup finished.\n";
    `logger -t $log_label -p local4.info $msg`;

    return $rc;

}

#
# run the command
#
sub runcmd
{
    my ($cmd) = @_;
    my $rc = 0;

    $cmd .= ' 2>&1';

    # my $outref = [];
    # @$outref = `$cmd`;

    $::outref = [];
    $::outref = `$cmd`;
    if ($?)
    {
        $rc = $? >> 8;
        if ($rc > 0)
        {
            my $msg = "odbcsetup:\"$cmd\" returned rc=$rc \"$::outref\"\n";
            print $msg;
            `logger -t $log_label -p local4.err $msg`;
            return 1;
        }
    }
    return 0;
}
