#
# $Header: has/crfutl/oclumon.pl.sbs /st_has_12.2.0.1.0/1 2016/11/11 17:53:17 shullur Exp $
#
# stopcrf.pl
#
# Copyright (c) 2007, 2016, Oracle and/or its affiliates. All rights reserved.
#
#    NAME
#      stopcrf.pl - This is the main driver for starting oclumon 
#
#    DESCRIPTION
#
#    NOTES
#      <other useful comments, qualifications, etc.>
#
#    MODIFIED   (MM/DD/YY)
#    shullur     11/08/16 - Backport shullur_bug-24686678 from main
#    shullur     11/01/16 - For fixing bug 24686678
#    arpshukl    04/07/14 - Fix oclumon hostname
#    arpshukl    01/20/14 - Read time zone from crs config file
#    arpshukl    02/25/13 - Set ORACLE_HOME correctly
#    sujkumar    01/31/10 - CRF_HOME as ORACLE_HOME
#    anutripa    01/05/10 - Add CRF_EXEC_PATH
#    sujkumar    12/29/08 - libpath without quotes
#    sujkumar    11/25/08 - add double quotes to args
#    anutripa    11/06/08 - Add return code
#    anutripa    11/06/08 - Cleanup and add the header.
#

use English;
use strict;
use Sys::Hostname;

my $FSSEP         = "";
my $OSNAME        = $^O;
my $CRF_HOME      = "";
my $commandline   = "";
my $argnum;
my $ENV_FILE      = "";
my $MYNAME        = hostname;
my $numError      = "";


sub is_dev_env
{
    my $isDevEnv = uc($ENV{'_SCLS_DEVELOPMENT'});
    if ($isDevEnv eq "TRUE") {
        return 1;
    } else {
        return 0;
    }
}

if ($OSNAME =~ "Win")
{
  $FSSEP = "\\";
}
else
{
  $FSSEP = "/";
}
$CRF_HOME = "%CRFHOME%";

if ($CRF_HOME eq "")
{
  print "\n";
  print "Invalid CRFHOME. Did you miss the crfinst.pl -finish under 'root'?";
  print "\n";
  exit 1;
}

if ($OSNAME =~ "Win")
{
  $ENV{'PATH'}="$CRF_HOME"."$FSSEP"."bin;$ENV{'PATH'}";
}
else
{
  $ENV{'LD_LIBRARY_PATH'}="$CRF_HOME/lib:$ENV{'LD_LIBRARY_PATH'}";
}

$ENV{'CRF_HOME'}=$CRF_HOME;
$ENV_FILE="$CRF_HOME"."$FSSEP"."crs"."$FSSEP"."install"."$FSSEP"."s_crsconfig_$MYNAME"."_env.txt";

sub importEnvFromFile()
{
   my $numErr = 0;

   if ( !open(FILE, "< $ENV_FILE") )
    {
	return $numErr;
    }

   #Validate and import env file.
   foreach (<FILE>)
   {
     next if (/^\s*$/);   #Skip blank line
     next if (/^\s*#/);   #Skip comments

     if ( ! /^(\S+?)=(.*)$/ )
     {
        $numErr++;
         next;
     }

     my $key = $1;
     my $value = $2;

     $ENV{$key} = $value;
   }
   return $numErr;
}

$numError = importEnvFromFile();

#Incorrect ORACLE_HOME cause problems with oclumon.
#Set ORACLE_HOME correctly irrespective of what it is set from user.

$ENV{'ORACLE_HOME'}=$CRF_HOME;

foreach $argnum (0 .. $#ARGV)
{
  $commandline = "$commandline \"@ARGV[$argnum]\"";
}

#if i am in install env call oclumon.bin, else call oclumon
# dev srg/lrg runs creates zero size oclumon.bin in oracle/bin
if (-x "$CRF_HOME/bin/oclumon.bin")
{
  system ("$CRF_HOME/bin/oclumon.bin $commandline"); 
}
else
{
  if ($OSNAME =~ "Win")
  {
    system ("oclumon.exe $commandline");
  }
  else
  {
    if (is_dev_env)
    {
      # linux dev env, oclumon is in has/bin
      system ("$CRF_HOME/has/bin/oclumon $commandline");
    }
    else
    {
      system ("$CRF_HOME/bin/oclumon $commandline");
    }
  }
}
exit($? >> 8);
