#======================================================
# SCMS-SM VCS agents
# SubscriberManager agent - clean
# Created: Aug 2004
#======================================================
# Developed to clean the SM resource
# if the SM is not in failure state the script restarts the SM
#======================================================
# Modification list:
# 5/8/2004: created
#
#======================================================
# Messages Used:
# MSGID               Message
#------------------------------------------------------
# 37021: SubscriberManager:% s: clean: invalid path to the SM '$SmBinPathname'
# 37022: SubscriberManager:% s: clean: parameters: $SmBinPathname, $State, $DebugLevel, $CleanReason
# 37023: SubscriberManager:% s: clean: current state is Failure => nothing to do aborting
# 37024: SubscriberManager:% s: clean: the SM is not up ...
# 37025: SubscriberManager:% s: clean: the SM is up and running
# 37046: SubscriberManager:% s: clean: current state is Initialization => nothing to do aborting
#======================================================

eval 'exec $VCS_HOME/bin/perl5 -S $0 ${1+"$@"}'
        if 0;
($ResID, $CleanReason ,$SmBinPathname, $State, $DebugLevel) = @ARGV;

use SmOperations;

#
# Set the OS locale
#
$ENV{"LANG"}="C";
$vcs_home =$ENV{"VCS_HOME"} || "/opt/VRTSvcs";
use ag_i18n_inc;
VCSAG_SET_ENVS ("SubscriberManager", "clean",$category_id);

my ( $log_message );

$SmBinPathname = cleanString($SmBinPathname);

if (($SmBinPathname eq "") || ( ! -d $SmBinPathname )) {
   VCSAG_LOG_MSG("E","invalid path to the SM '$SmBinPathname'",37021,"$SmBinPathname");
   exit 1;
}

if ($DebugLevel > 0) 
{
   VCSAG_LOG_MSG("I","parameters: $SmBinPathname, $State, $DebugLevel, $CleanReason",37022,"$SmBinPathname","$State","$DebugLevel","$CleanReason");
}

my %args100 = ("SmBinPathname" => $SmBinPathname,
            "DebugLevel" => $DebugLevel,
            "ResID" => $ResID);
my $getStateResult = getState(\%args100);
if ($getStateResult eq "Failure") 
{
   VCSAG_LOG_MSG("C","current state is Failure => nothing to do aborting",37023);
   exit 0;
}
elsif ($getStateResult eq "Initialization") 
{
   VCSAG_LOG_MSG("C","current state is Initialization => nothing to do aborting",37046);
   exit 0;
}

# these paraeters define how long and in what rate the SM is polled
$startUpTimeout = 30;
$initialSleep   = 2;
$pollInterval   = 5;

%args = ("Timeout" => ($startUpTimeout - $initialSleep),
         "PollInterval" => $pollInterval,
         "InitialSleep"=> $initialSleep,
         "SmBinPathname" => $SmBinPathname,
         "DebugLevel" => $DebugLevel,
         "ResID" => $ResID);

# call the SM to restart
if (smRestartAndWait(\%args) > 0)
{
   # SM is up and running
   %args1 = ("Operation" => "su - pcube -c \"$SmBinPathname/p3cluster --standby\"" ,
         "DontReturnResult" => "0",
         "DebugLevel" => $DebugLevel,
         "ResID" => $ResID,
         "maxRetries" => "5");
   
   my $cluResult = runOperation(\%args1);
   
   my $success = grep("Command terminated successfully",$cluResult);

   if ($success) 
   {
      # called the SM to become offline
      # the operation timed-out
      VCSAG_LOG_MSG("C","the SM is up and running",37025);
      exit 0;
   }   
   else
   {
      # failed to put offline
      exit 1;
   }
}
else
{
   # the operation timed-out
   VCSAG_LOG_MSG("C","the SM is not up ...",37024);
   exit 1;
}

