#!/usr/local/bin/perl
#
# $Header: emdb/sysman/admin/scripts/has/clustgtinst.pl /st_emdbsa_11.2/1 2011/07/20 17:31:43 ajdsouza Exp $
#
# clustgtinst.pl
# 
# Copyright (c) 2006, 2011, Oracle and/or its affiliates. All rights reserved. 
#
#    NAME
#      clustgtinst.pl - <one-line expansion of the name>
#
#    DESCRIPTION
#      this script removed the PUSH=TRUE from the cluster target instance file conditions in  the
#      <AGENT_HOME>/sysman/emd/collection/cluster_<tgt_name>.xml files
#      the modified file is backed up as <AGENT_HOME>/sysman/emd/collection/old_cluster_<tgt name>.bak
#
#      this scripts expects the ORACLE_HOME for the agent to be provided in the environment
#
#    NOTES
#
#
#    MODIFIED   (MM/DD/YY)
#   ajdsouza     11/01/10 - bug fix 10118817
#                           XbranchMerge ajdsouza_blr_backport_10392806_11.1.0.1.0
#                           from st_emdbgc_10.2.0.1.0
#    ajdsouza    12/10/10 - created
#



use strict;
use warnings;


use File::Spec::Functions;
use File::Basename;
use File::Path;
use File::Copy;

use Data::Dumper;


my $revert=0;
$revert = 1 if @ARGV > 0 and $ARGV[0] and $ARGV[0] =~ /revert/i;

my $oh;

$oh=$ENV{ORACLE_HOME} if $ENV{ORACLE_HOME};

die "ERROR:Failed to get the agent home directory , failed to update the cluster instance file" unless $oh;

my $obin = catdir($oh,'bin');
my $emctl;

for my $emct ( qw ( emctl emctl.bat ) )
{
  my $tmpcmd = catfile($obin,$emct);
  stat $tmpcmd;
  next unless -e $tmpcmd and -r $tmpcmd;
  $emctl = $tmpcmd; 
  last;
}

die "ERROR:Failed to get the emctl cli in $obin , failed to update the cluster instance file" unless $emctl;

my $res = `$emctl getemhome`;

die "ERROR:Failed to get the results from $emctl getemhome, failed to update the cluster instance file" unless $res;

my @results = split/\n/,$res;

my $emhome;

for my $row ( @results )
{
  next unless $row;
  $row =~ s/^\s+|\s+$//;
  next unless $row;
  next unless $row =~ /^EMHOME=/;
  ( $emhome ) = ( $row =~ /^EMHOME=(.*)/);
  $emhome =~ s/^\s+|\s+$// if $emhome;
  last;
}

die "ERROR:Failed to get the emhome from $emctl getemhome, failed to update the cluster instance file" unless $emhome;

my $sysman = catdir($emhome,'sysman');
my $emd = catdir($sysman,'emd');
my $coll = catdir($emd,'collection');

stat $coll;

print "collection directory $coll does nto exist, no cluster target instance files to be updated\n" and exit 0 unless -e $coll and -r $coll;

# open the collection directory and get the cluster_<>.xml files to process
opendir(my $dh, $coll) or die "ERROR:Failed to open target instance collection directory $coll, no cluster target instance files to be updated";

my @clsfiles = grep { /^cluster_.*\.xml/ } readdir($dh);

closedir $dh;

for my $clsfile ( @clsfiles )
{
  next unless $clsfile;

  my $collfilepath = catfile($coll,$clsfile);
  stat $collfilepath;
  warn "WARN:Failed to read the cluster target instance collection file $collfilepath, skipping this file \n" and next 
   unless -e $collfilepath and -r $collfilepath;

  my $datamodified = 0;
  my @clsdata;
  if ( not $revert )
  {

   print "DEBUG:Editing cluster target instance collection file $clsfile, to remove PUSH=TRUE\n";

   # read data from the collection file
   open CLSDATA, "$collfilepath" or warn "WARN:Failed to open $collfilepath $!, skipping this file\n " and next;

   @clsdata = <CLSDATA>;
   for my $datarow ( @clsdata )
   {
    next unless $datarow and $datarow =~ /PUSH\=\"TRUE\"/;
    $datamodified = 1;
    $datarow =~ s/PUSH\=\"TRUE\"//; 
   }

   close (CLSDATA); 
  }
  else
  {
    $datamodified = 1;
  }

  # write the modified data back to the file
  if ( $datamodified )
  {
       $clsfile =~ s/\.xml$/\.bak/;
       $clsfile =~ s/^cluster/patch_cluster/;
       my $backupfilepath = catfile($coll,$clsfile);

       if ( $backupfilepath ne $collfilepath )
       {
         if ( not $revert )
         {
          print "DEBUG:Backed up file $collfilepath to $backupfilepath, before removing PUSH=TRUE\n";
          copy($collfilepath,$backupfilepath) or warn "WARN:Failed to backup file $collfilepath to $backupfilepath, skipping this file \n" and next;
         }
         else
         {
          stat $backupfilepath;
          if ( -e $backupfilepath and -r $backupfilepath )
          {
           print "DEBUG:Reverting from backed up file $backupfilepath to $collfilepath , restoring back PUSH=TRUE\n";
           copy($backupfilepath,$collfilepath) or warn "WARN:Failed to revert from file $backupfilepath to $collfilepath , skipping this file \n" and next;
          }
         }
       }
       else
       {
         warn "WARN:Failed to backup file $collfilepath to $backupfilepath, before removing PUSH=TRUE, skipping this file\n";
       }

      if ( not $revert )
      {
        open(FHD,'>',$collfilepath) or die "ERROR:Failed to open cluster target instance file $collfilepath for writing back without PUSH, no cluster target instance files to be updated\n";
        for my $writedata ( @clsdata )
        {
         print FHD $writedata and next if $writedata;
         print FHD '\n';
        }
        close FHD;

        print "DEBUG:Cluster target instance collection file $collfilepath is updated by removing PUSH=TRUE\n";
      }

  }
  else
  {
      print "DEBUG:Cluster target instance collection file $collfilepath does not have PUSH=TRUE no changes needed\n";
  }

}

exit 0;
