#!/usr/bin/perl

#
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved. 
#
#  $Id:oracle_cell_exadata_version.pl 
#
#
# NAME  
#   oracle_cell_exadata_version.pl
#
# DESC 
#
# This script gets Exadata Server release version.
#
# NOTES
# Before 11.2.2.3.0, the following list matches cellVersion with Exadata Server version:
#   cellVersion                              ExadataServerReleaseVersion
#   OSS_11.2.1.2.6_LINUX.X64_100511          11.2.1.2.6    <<== 11gR1 (aka: Release Version 1)
#   OSS_11.2.0.1.0_LINUX.X64_100818.1        11.2.1.3.1  
#   OSS_11.2.2.1.1_LINUX.X64_101105          11.2.2.1.1    <<== 11gR2 
#   OSS_11.2.0.3.0_LINUX.X64_101206.2        11.2.2.2.0 
#   OSS_11.2.2.2.2_LINUX.X64_110311          11.2.2.2.2
# Starting from 11.2.2.3.0, "releaseVersion" is added to cell's attributes. 
# always read "releaseVersion" since "cellVersion" is not reliable.
#   cellVersion                              releaseVersion(aka: ExadataServerReleaseVersion)
#   OSS_11.2.0.3.0_LINUX.X64_110411          11.2.2.3.0    <<== 11gR3  
#
# MODIFIED  (MM/DD/YY)
#   ystein   04/12/11 	Created the file.
# 

use XML::Parser;
require emx::oracle_cell::oracle_cell_common;
require "emd_common.pl";

$ipaddress=doPing(@ARGV);

if($ipaddress eq "") {
  print "em_error=Failed to ping Cell\n";
  exit -1;
}
my $command = "ssh -l cellmonitor $ipaddress cellcli -xml -e 'list cell detail'";
EMD_PERL_DEBUG("command = $command \n");

my $xmlout = `$command`;

my $str;
use Data::Dumper;
$str = Dumper($xmlout);
EMD_PERL_DEBUG($str); 

my $parser = XML::Parser->new( Handlers => {
                                      Start=>\&handle_start,
                                      End=>\&handle_end,
				      Char=> \&characterData,
                                     });
$str = Dumper($parser);
EMD_PERL_DEBUG($str); 

my $result;
my $myinf;
$parser->parse( $xmlout);

#-----------------------------------------------------------------
# FUNCTIONS :  handle_start
#
#
# DESC : Does node based parsing of the XML output.
#
# ARGUMENTS :
#		elements : Node elements in the XML 
#  		attrs 	 : Attributes of the elements.
#
#
#-----------------------------------------------------------------

sub handle_start {
    my( $expat, $element, %attrs  ) = @_;
    $myinf="";
    if ($element eq "cellVersion")
    {
       $myinf="cellversion";
    }
    elsif ($element eq "releaseVersion")
    {
       $myinf="releaseversion";
    }
    else
    {
       EMD_PERL_DEBUG("Ignored element = $element \n");
    } 
}

sub handle_end {
    my( $expat, $element, %attrs  ) = @_;
    if ($element eq "cell")
    {
      #conservatively,
      #if releaseversion contains 11.2.2.3.0, we know it is Exadata 11.2.2.3.0 release;
      #else get releaseversion based on cellversion;
      #if can not find the matched cellversion, set 11.2.1.2.6 as the default releaseversion. 
      EMD_PERL_DEBUG("Started releaseversion = $releaseversion, cellversion = $cellversion \n");
      
      if ( $releaseversion =~ m/11.2.2.3.0/ )
      { 
         $cellversion="";
         $releaseversion = "11.2.2.3.0";
      }
      else
      {
         if ( $cellversion =~ m/11.2.1.2.6/ ) 
         { #OSS_11.2.1.2.6_LINUX.X64_100511 ==> 11.2.1.2.6
           $releaseversion ="11.2.1.2.6";
         }
         elsif ( $cellversion =~ m/11.2.0.1.0/ )
         { #OSS_11.2.0.1.0_LINUX.X64_100818.1 ==> 11.2.1.3.1
           $releaseversion = "11.2.1.3.1";
         } 
         elsif ( $cellversion =~ m/11.2.2.1.1/ )
         { #OSS_11.2.2.1.1_LINUX.X64_101105 ==> 11.2.2.1.1
           $releaseversion = "11.2.2.1.1";
         } 
         elsif ( $cellversion =~ m/11.2.0.3.0/ )
         { #OSS_11.2.0.3.0_LINUX.X64_101206.2 ==> 11.2.2.2.0 
           $releaseversion = "11.2.2.2.0";
         }
         elsif ( $cellversion =~ m/11.2.2.2.2/ )
         { #OSS_11.2.2.2.2_LINUX.X64_110311 ==> 11.2.2.2.2
           $releaseversion = "11.2.2.2.2";
         }
         else
         { 
            $releaseversion = "11.2.1.2.6";
         }
      }
 
      EMD_PERL_DEBUG("Analyzed releaseversion = $releaseversion, cellversion = $cellversion \n");
      print "em_result=$releaseversion\n";
    }
    else
    {
       EMD_PERL_DEBUG("find not cell element: $element \n");
    }
}

sub characterData {
    my( $parseinst, $data ) = @_;
    $space=" ";
    $data =~ s/\n//g;
    if (($data !~ m/^\s/) and ($data ne "\""))
    {
      if ($myinf eq "cellversion" )
      {
        $cellversion=$cellversion.$data;
      }
      elsif ($myinf eq "releaseversion" )
      {
        $releaseversion=$releaseversion.$data;
      }
    }
}
