#!/usr/sbin/rsct/perl5/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
#"@(#)73   1.20.1.14   src/rsct/rm/ER/cli/bin/mkcondition.perl, errmcli, rsct_relgh, relghs001a 7/6/06 18:05:50"
######################################################################
#                                                                    #
# Module: mkcondition                                                #
#                                                                    #
# Purpose:                                                           #
#   mkcondition - make a condition resource                          #
#                                                                    #
# Syntax:                                                            #
#   mkcondition [-h] -r Resource_class -e Event_expression           #
#               [-E Rearm_expression] [-d Event_description]         #
#               [-D Rearm_description] [-n Node_name[,Node_name,...] #
#               [-s "Selection_string"] [-S c|w|i] [-m l|m|p]        #
#               [-p Node_name] [-TV] Condition                       #
#                                                                    #
#   mkcondition [-h] -c Existing_condition[:Node_name]               #
#               [-r Resource_class] [-e Event_expression]            #
#               [-E Rearm_expression] [-d Event_Description]         #
#               [-D Rearm_description] [-n Node_name[,Node_name,..]  #
#               [-p Node_name] [-s "Selection_string"] [-m l|m|p]    #
#               [-S c|w|i] [-TV] Condition                           #
# Flags:                                                             #
#   -h      help - writes this command's usage statement to stdout   #
#   -c      Existing_condition to copy to a new condition. Node_name #
#           specifies where it is defined.                           #
#   -r      Resource_class.  The name of the resource class to be    #
#           monitored.                                               #
#   -e      Event_expression. Expression that determines when an     #
#           event occurs.  Expression usually involves the dynamic   #
#           attribute and a comparison.                              #
#   -E      Rearm_expression. Expression to indicate when to restart #
#           monitoring after an event has occurred.  Expression      #
#           usually involes the dynamic attribute and a comparison.  #
#   -d      Event_description. User supplied text to describe the    #
#           event_expression.                                        #
#   -D      Rearm_description. User supplied text to describe the    #
#           rearm_expression.                                        # 
#   -n      Node_namelist where condition is to be monitored.        #
#   -s      Selection_string. Specifies which resources in           #
#           the resource class are monitored.  Default is all        #
#           resources in the class.                                  #
#   -S c|w|i Specifies the severity of the event. c=critical,        #
#           w=warning, and i=informational.  Default is              #
#           informational.                                           #
#   -m l|m|p Specifies the scope of the condition when monitoring    #
#           starts. l=local, d=distributed domain, and p=peer domain.#
#   -p      Specifies the node on which the condition is defined.    #
#   -T      Trace. IBM Support Center use only.                      #
#   -V      Verbose.                                                 #
#                                                                    #
# Operands:                                                          #
#   Condition       The name of the new condition to be created.     #
#                                                                    #
# Description:                                                       #
#   The mkcondition command creates a new condition definition with  #
#   the name specified by the Condition parameter.  The condition is #
#   used to monitor a resource for the occurrence of the condition   #
#   (or event).   One or more responses to an event can be defined   #
#   by using the mkresponse command.  The conditions can then be     # 
#   linked to the responses by using the mkcondresp command, or by   #
#   using the startcondresp command to link responses and start      #
#   monitoring.                                                      #
#                                                                    #
# Exit Values:                                                       #
#   0  ERRM_CLI_SUCCESS      Command completed successfully.         #
#   1  ERRM_CLI_RMC_ERROR    Command terminated due to an underlying #
#                            RMC error.                              #
#   2  ERRM_CLI_ERROR        Command terminated due to an underlying #
#                            error in the command script.            #
#   3  ERRM_CLI_BAD_FLAG     Command terminated due to user          #
#                            specifying an invalid flag.             #
#   4  ERRM_CLI_BAD_OPERAND  Command terminated due to user          #
#                            specifying a bad operand.               #
#   5  ERRM_CLI_USER_ERROR   Command terminated due to a user error. #
#                            For example specifying a name that      #
#                            already exists.                         #
#                                                                    #
# Examples:                                                          #
#   1. Define a condition with the name "JFS space used up" to check #
#      for percent space used greater than 90% and to rearm when it  #
#      is back down below 85%                                        #
#      mkcondition -r IBM.FileSystem -e "PercentTotUsed > 90" \      #
#           -E "PercentTotUsed < 85" "JFS space used up"             #
#   2. Define a condition with the name "tmp space used up" to check #
#      for percent space used greater than 90% for /tmp and to rearm #
#      when it is back down below 85%, including comments            #
#      mkcondition -r IBM.FileSystem -e "PercentTotUsed > 90" \      #
#           -E "PercentTotUsed < 85" \                               #
#           -d "Generate event when tmp > 90 percent full" \         #
#           -D "Resume monitoring when tmp < 85 percent full" \      #
#           -s 'Name=="/tmp"' "tmp space used up"                    #
#   3. Define a condition with the name "Space used up" as a copy of #
#      "JFS space used up"                                           #
#      mkcondition -c "JFS space used up" "Space used up"            #
#   4. Define a condition with the name "var space used up" as a     #
#      copy of "tmp space used up", but change the selection to /var #
#      mkcondition -c "tmp space used up" -s 'Name=="/var"'  \       #
#           "var space used up"                                      #
#                                                                    #
# Man Page:                                                          #
#   For the most current detailed description of this command see    #
#   the mkcondition man page in /usr/sbin/rsct/man.                  #
#                                                                    #
#--------------------------------------------------------------------#
# Inputs:                                                            #
#   /usr/sbin/rsct/msgmaps/errmcli.mkcondition.map - message mapping #
#                                                                    #
# Outputs:                                                           #
#   stdout - display information on the condition.                   #
#   stderr - any error message.                                      #
#                                                                    #
# External Ref:                                                      #
#   Commands: ctdspmsg                                               #
#   Modules:  ERRM_cli_utils.pm, ERRM_cli_rc.pm                      #
#             CT_cli_utils.pm                                        #
#   Perl library routines: Getopt::Std                               #
#                                                                    #
# Tab Settings:                                                      #
#   4 and tabs should be expanded to spaces before saving this file. #
#   in vi:  (:set ts=4  and   :%!expand -4)                          #
#                                                                    #
# Change Activity:                                                   #
#   001012 JAC 67469: Initial design & write.                        #
#   001226 JAC 67470: Completed mkcondition command.                 #
#   010103 JAC 70296: Make TRUE/FALSE come from utils.               #
#   010116 JAC 70594: Modify selection string processing.            #
#   010125 JAC 70355: Modify Trace message to use variables.         #
#   010126 JAC 70783: Place NodeName value in single quotes.         #
#   010129 JAC 70951: Shift rc after RMC calls if error occurs.      #
#   010303 JAC 71042: Remove setting DynamicAttribute.               #
#   010308 JAC 71005: Escape some special characters in strings.     #
#   010430 JAC 73619: Do not send any RMC CLI std err to garbage.    #
#   010507 JAC 73620: Check for RMC CLI user error (rc=5) and        #
#                     return ERRM user error for this case.          #
#   010516 JAC 74036: Convert nodenames to long names, if possible   #
#   011119 JAC 77597: Add distributed RMC changes                    #
#   011206 JAC 78841: Fix selection string for locating resources.   #
#   011213 JAC 78976: Fix selection string for locating resources.   #
#   020121 JAC 79558: Fix selection string for locating resources.   #
#   020128 JAC 79559: Change how destination/locator node is used.   #
#   020416 JAC 81754: Fix exit code check and Switch to xxx-api cmds.#
#   020428 JAC 82364: Change mgt scope to l|d|p from l|c|s.          #
#   020429 JAC 82425: Change some escaping/quoting for xxx-api cmds. #
#   020503 JAC 82639: Try to fix escaping/quoting again.             #
#   020719 JAC 84425: Add $rc to process_api_error call.             #
#   020725 JAC 84654: Change mgt scopes to l,m,p from l,d,p.         #
#   020729 JAC 85061: Use 4 (DM/SR/local) for scope if locator used  #
#                     or -p used and scope not set.                  #
#   040407 JAC 105863: Use escape_chars for "\" searches.            #
######################################################################

#--------------------------------------------------------------------#
# General Program Flow/Logic:                                        #
#                                                                    #
# 1. Parse command line flags and operands, determine which flavor   #
#    of this command we are actually invoking.                       #
# 2. Print usage if -h specified.                                    #
# 3. Make sure one and only one condition is specified.              #
# 4. Make sure the condition doesn't already exist.                  #
# 5. If using -c for copy, make sure condition to copy exists.       #
# 6. Translate flags into attribute names for RMC mkrsrc command.    #
# 7. If copy, retrieve any attributes not specified in command.      #
# 8. Call RMC command mkrsrc. Also pass along -VT if necessary.      #
# 9. Return back any errors.                                         #
#                                                                    #
#--------------------------------------------------------------------#

#--------------------------------------------------------------------#
# Included Libraries and Extensions                                  #
#--------------------------------------------------------------------#
use lib "/usr/sbin/rsct/pm";
use locale;
use Getopt::Long;

use CT_cli_utils qw(printIMsg
                    printEMsg);
use CT_cli_input_utils qw(escape_chars);

use ERRM_cli_rc qw(ERRM_CLI_SUCCESS ERRM_CLI_RMC_ERROR
                   ERRM_CLI_ERROR ERRM_CLI_BAD_FLAG
                   ERRM_CLI_BAD_OPERAND ERRM_CLI_USER_ERROR);
use ERRM_cli_utils qw(error_exit
                    printCIMsg
                    printCEMsg
                    find_resource_name get_source_node
                    resolve_node get_locator_node
                    set_orig_rmc_scope check_set_cluster_scope
                    process_api_error process_exit_code
                    $TRUE $FALSE
                    $CTBINDIR $CTDIR
                    $SEVCRIT $SEVWARN $SEVINFO $DELIMITER
                    $MKCNDCRIT $MKCNDWARN $MKCNDINFO
                    $LCL_SCOPE $SR_SCOPE $CSM_SCOPE 
                    $SR_SCOPE_OLD $CSM_SCOPE_OLD $CSM_SCOPE_OLD2
                    $LCL_SCOPE_VAL $SR_SCOPE_VAL $CSM_SCOPE_VAL
                    $RMC_CLI_USER_ERROR $RMC_LOCAL_SCOPE
                    $LSRSRC $MKRSRC $RSCCOND);

#--------------------------------------------------------------------#
# Global Variables                                                   #
#--------------------------------------------------------------------#
Getopt::Long::Configure ("bundling", "no_auto_abbrev", 
                         "no_ignore_case", "require_order",
                         "prefix_pattern=(--|-)");
$Trace = $FALSE;                        # default - trace off
$Verbose = $FALSE;                      # default - verbose turned off

$Scope_orig_set = $FALSE;               # default - no CT_MANAGEMENT_SCOPE
$Scope_orig_value = 0;                  # default - scope value

$Opt_Resource = $FALSE;                 # default - no resource
$Opt_Event_Expr = $FALSE;               # default - no event expr
$Opt_Event_Descr = $FALSE;              # default - no event descr
$Opt_Rearm_Expr = $FALSE;               # default - no rearm expr
$Opt_Rearm_Descr = $FALSE;              # default - no rearm descr
$Opt_Selection = $FALSE;                # default - no selection
$Opt_Severity = $FALSE;                 # default - no severity
$Opt_Nodes = $FALSE;                    # default - no node list
$Opt_Copy_Condition = $FALSE;           # default - no copy condition
$Opt_Mgt_Scope = $FALSE;                # default - no management scope
$Opt_Put_Node = $FALSE;                 # default - no put node
$Opt_No_Toggle= $FALSE;                 # default - no no toggle expr

$PROGNAME = "mkcondition";              # Program Name for messages
$LSMSG = "$CTBINDIR/ctdspmsg";          # list / display message rtn
$MSGCAT = "errmcli.cat";                # msg catalogue for this cmd
$ENV{'MSGMAPPATH'} = "$CTDIR/msgmaps";  # msg maps used by $LSMSG

#--------------------------------------------------------------------#
# Variables                                                          #
#--------------------------------------------------------------------#
my $condition = "";                     # Condition to make
my $resource_name = "";                 # resource for -r flag
my $event_expr = "";                    # expression for -e
my $event_desc = "";                    # description for -d
my $rearm_expr = "";                    # expression for -E
my $rearm_desc = "";                    # description for -D
my $select_str = "";                    # select string for -s 
my $severity = 0;                       # default for -r
my $nodelist = "";                      # default for -n
my $new_nodelist = "";                  # resolved nodelist 74036
my $copy_condition = "";                # Condition to copy
my $put_node = "";                      # where to define condition -p
my $mgt_scope = "";                     # management scope -m
my $get_attrs = "";                     # attr list for copy
my $copy_vals = "";                     # attr values for copy
my $passopts = "";                      # options to pass to RMC
my $other_opts = "";                    # options to pass to RMC
my $this_attr = "";                     # attr name for copy
my $this_value = "";                    # value of attr name for copy
my $one_node = "";                      # one from nodelist 74036
my @nodes = ();                         # list of nodes 74036
my $hname = "";                         # used by gethost 74036
my $haliases = "";                      # used by gethost 74036 
my $haddrtype = "";                     # used by gethost 74036
my $hlength = 0;                        # used by gethost 74036
my @haddrs = ();                        # used by gethost 74036
my $comma = "";                         # used by gethost 74036
my @cmd_out = ();                       # command output 

#--------------------------------------------------------------------#
# Main Code                                                          #
#--------------------------------------------------------------------#
my $rc = 0;

# save original rmc management scope value, in case it's changed later
if (defined $ENV{CT_MANAGEMENT_SCOPE}) {
   $Scope_orig_set = $TRUE;
   $Scope_orig_value = $ENV{CT_MANAGEMENT_SCOPE};
}

# parse the command line, exit if there are errors 
($rc, $condition, $resource_name, $event_expr, 
 $event_desc, $rearm_expr, $rearm_desc, $select_str, 
 $severity, $nodelist, $copy_condition,
 $mgt_scope, $put_node) = &parse_cmd_line;
($rc == 0) || error_exit($rc);

if ($Verbose) { printIMsg("IMsgmkconditionStart",$condition);}

# pass the trace or verbose flags to mkrsrc
if ($Trace) { $passopts = $passopts." -T"; }
if ($Verbose) { $passopts = $passopts." -V"; }

# for anything that's a string, escape any inner double quotes (71005)
$condition = escape_chars($condition);
$copy_condition = escape_chars($copy_condition);
$resource_name = escape_chars($resource_name);
$event_expr = escape_chars($event_expr);
$event_desc = escape_chars($event_desc);
$rearm_expr = escape_chars($rearm_expr);
$rearm_desc = escape_chars($rearm_desc);
$select_str = escape_chars($select_str);
$nodelist = escape_chars($nodelist);

# add any optional parameters of mkcondition to pass to mkrsrc
# if parameter not specified, may need to get it for copy
if ($Opt_Resource) {                    # resource present
   $other_opts = $other_opts."::ResourceClass::$resource_name"; }
else { $get_attrs = $get_attrs."::ResourceClass"; }
   
if ($Opt_Event_Expr) {                  # event expression present
   $other_opts = $other_opts."::EventExpression::$event_expr"; }
else { $get_attrs = $get_attrs."::EventExpression"; }
   
if ($Opt_Event_Descr) {                 # event description present
   $other_opts = $other_opts."::EventDescription::$event_desc"; }
else { $get_attrs = $get_attrs."::EventDescription"; }
   
if ($Opt_Rearm_Expr) {                  # rearm expression present
   $other_opts = $other_opts."::RearmExpression::$rearm_expr"; }
else { $get_attrs = $get_attrs."::RearmExpression"; }
   
if ($Opt_Rearm_Descr) {                 # rearm description present
   $other_opts = $other_opts."::RearmDescription::$rearm_desc"; }
else { $get_attrs = $get_attrs."::RearmDescription"; }
   
if ($Opt_Selection) {                   # selection string present
   $other_opts = $other_opts."::SelectionString::$select_str"; }
else { $get_attrs = $get_attrs."::SelectionString"; }
   
if ($Opt_Severity) {                    # severity present
   $other_opts = $other_opts."::Severity::$severity"; }
else { $get_attrs = $get_attrs."::Severity"; }
   
if ($Opt_Mgt_Scope) {                   # scope present
   $other_opts = $other_opts."::ManagementScope::$mgt_scope"; }
else { $get_attrs = $get_attrs."::ManagementScope"; }
   
if ($Opt_Nodes) {                       # node list present
   $other_opts = $other_opts."::NodeNames::{$nodelist}"; }
else { $get_attrs = $get_attrs."::NodeNames"; }

if ($Opt_Put_Node) {                    # put node present
   # specify where to put the condition
   $other_opts = $other_opts."::NodeNameList::{$put_node}"; }
  
if ($Opt_No_Toggle)
{
   $other_opts = $other_opts."::NoToggleExprFlag::1";
}  
elsif ($Opt_Toggle)
{
   $other_opts = $other_opts."::NoToggleExprFlag::0";
}
else 
{
   $get_attrs = $get_attrs."::NoToggleExprFlag";
}

# if copying a condition (-c), get condition information and
# update with anything that was specified on the command line
if ($Opt_Copy_Condition) {              # copy a condition
   # get condition information
   # override any info with info specified in command
   if ($get_attrs ne "") {
      $copy_vals = get_condition_info($copy_condition,$get_attrs);
   }

   $get_attrs = substr($get_attrs,2);
   # add a dummy string on the end to test for
   if ($get_attrs ne "") { $get_attrs = $get_attrs."::END";}
   else { $get_attrs = "END";}

   # add the attr values in for the mkrsrc command to use
   while ($get_attrs ne "END"){

      # get the next attribute and value
      ($this_attr,$get_attrs) = split /::/, $get_attrs, 2;
      ($this_value,$copy_vals) = split /$DELIMITER/, $copy_vals, 2;

      # format it properly to use in the mkrsrc command
      if ($this_attr eq "NodeNames") {
         $other_opts = $other_opts."::${this_attr}::${this_value}"; 
         }
      else {
         
         # if it has outer double quotes, must escape an inner ones
            $this_value = escape_chars($this_value);

            # make sure it's not blank
            $this_value =~ s/^$/\\\"\\\"/;
     
            $other_opts = $other_opts."::${this_attr}::${this_value}"; 
         }
   }
} 

# set CT_MANAGEMENT_SCOPE to original setting, in case it was used
set_orig_rmc_scope;

# if the put node (-p, or destination node) was not specified,
# this is a local definition.  Make sure rmc scope is set to
# local scope.
if (!$Opt_Put_Node) {                   # put node not present
   $ENV{CT_MANAGEMENT_SCOPE} = $RMC_LOCAL_SCOPE;
}

# -p is specified, try to help user if scope not set
else {
   # use DM/SR/Local scope if scope not set 
   check_set_cluster_scope;
}

# call mkrsrc 
if ($Trace) { print STDERR "$PROGNAME: calling mkrsrc-api\n";}

@cmd_out=`$CTBINDIR/mkrsrc-api -D $DELIMITER "${RSCCOND}::Name::${condition}${other_opts}" 2>&1`;

$rc = $?;
$rc = &process_exit_code($rc);

if ($Trace) { print STDERR "mkrsrc-api results:\n";
              print STDERR "@cmd_out";}

if ($Trace) { print STDERR "$PROGNAME: mkrsrc-api returned $rc\n";}

# show any errors if there was a bad rc
if ($rc != 0) {
   process_api_error($DELIMITER,$rc,@cmd_out);
}

# return ERRM CLI user error if it's an RMC CLI user error    73620
($rc == $RMC_CLI_USER_ERROR) && ( error_exit(ERRM_CLI_USER_ERROR) );

# if mkrsrc command failed, print RMC error message and exit
if ($rc != 0) {
#  printCEMsg("EMsgERRMcliUnExpectRMCrc",$rc);
   exit(ERRM_CLI_RMC_ERROR);
}

if ($Verbose) { printIMsg("IMsgmkconditionEnd",$condition);}
exit($rc);

#--------------------------------------------------------------------#
# End Main Code                                                      #
#--------------------------------------------------------------------#


#--------------------------------------------------------------------#
# parse_cmd_line - Parse the command line for options and operands.  #
#   Set appropriate global variables as outlined below, make sure we #
#   have a valid combination of arguments / options.                 #
#                                                                    #
# Return:                                                            #
#   $rc   0                  Command line parsed fine, no problem.   #
#         ERRM_CLI_BAD_FLAG  Command line contained a bad flag.      #
#   $resource                Name of condition to make.              #
#   $resource_name           Name of resource class.                 #
#   $event_expr              Event expression.                       #
#   $event_desc              Event description.                      #
#   $rearm_expr              Rearm expression.                       #
#   $rearm_desc              Rearm description.                      #
#   $select_str              Selection string.                       #
#   $severity                Severity.                               #
#   $node_list               Node list.                              #
#   $copy_condition          Name of condition to copy.              #
#                                                                    #
# Global Variables Modified:                                         #
#   $Verbose           output   True (-V) turn Verbose mode on.      #
#   $Trace             output   True (-T) turn Trace mode on.        #
#   $Opt_Resource      output   True (-r) resource specified         #
#   $Opt_Event_Expr    output   True (-e) event expression specified #
#   $Opt_Event_Descr   output   True (-d) event description specified#
#   $Opt_Rearm_Expr    output   True (-E) rearm expression specified #
#   $Opt_Rearm_Descr   output   True (-D) rearm description specified#
#   $Opt_Selection     output   True (-s) selection string specified #
#   $Opt_Severity      output   True (-S) severity specified         #
#   $Opt_Nodes         output   True (-n) node names specified       #
#   $Opt_Copy_Condition output  True (-c) copy specified             #
#   $Opt_Mgt_Scope     output   True (-m) mgt scope specified        #
#   $Opt_Put_Node      output   True (-p) put node specified         #
#--------------------------------------------------------------------#
sub parse_cmd_line 
{
my(@original_argv) = @ARGV;             # user enetered flags/parms 
my $resource = "";                      # condition name
my $select_str = "";                    # selection string from -s 
my $resource_name = "";                 # resource class from -r
my $event_expr = "";                    # expression from -e
my $event_desc = "";                    # description from -d
my $rearm_expr = "";                    # expression from -E 
my $rearm_desc = "";                    # description from -D
my $sev_value = "";                     # severity from -S
my $node_list = "";                     # hosts from -n
my $severity = $MKCNDINFO;              # default severity
my $copy_condition = "";                # existing condition for copy
my $copy_condition2 = "";               # copy of condition for copy
my $put_node = "";                      # put node from -p
my $locator = "";                       # locates the condition to copy
my $mgt_scope = "";                     # mgt scope from -m
my $mgt_scope_val = 0;                  # mgt scope translated
my @listout = ();                       # output from lsrsrc
my %opts = ();                          # for options parsing

# Process the command line...
if (!GetOptions(\%opts,
                'h'   ,
                'c=s' ,
                'r=s' ,
                'e=s' ,
                'E=s' ,
                'd=s' ,
                'D=s' ,
                'n=s' ,
                's=s' ,
                'S=s' ,
                'm=s' ,
                'p=s' ,
                'V'   ,
                'T'   ,
                'qtoggle' ,
                'qnotoggle' )) 
{
    &print_usage;                       # display proper usage
    return ERRM_CLI_BAD_FLAG;           # return bad rc - bad flag 
}

# process h flag
if (defined $opts{h}) {                 # -h, help request
    &print_usage;                       # print usage statement
    exit(0);                            # all done with good return
}

if (defined $opts{T}) {                 # -T turn trace on
    $Trace = $TRUE;
}

if (defined $opts{V}) {                 # -V turn verbose mode on
    $Verbose = $TRUE;
}

# check for where to define (put) condition (not required)
if (defined $opts{p}) {                 # -p for put condition
    $put_node = $opts{p};
    $Opt_Put_Node = $TRUE;              # opt flag specified
    if ($put_node eq ""){               # no text for -p
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-p");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# Get the arguments...
# Operands:  resource (only one condition resource allowed)
if ($#ARGV == 0) {                      # index 0 is last one
    $resource = shift @ARGV;            # user specified resources

    # make sure the condition doesn't contain any colon characters
    if ($resource =~ /:/)
    {
        printEMsg("EMsgmkconditionInvalidConditionName");
        exit(ERRM_CLI_USER_ERROR);
    }
   
    # make sure the condition doesn't already exist 
    $rc = find_resource_name($resource,$RSCCOND,$put_node);
    if ($rc == $RMC_CLI_USER_ERROR) {
       exit(ERRM_CLI_USER_ERROR);
       }
    if ($rc == 0) {                     # condition name found
        printEMsg("EMsgmkconditionConditionExists",$resource);
        exit(ERRM_CLI_USER_ERROR);
    }
}

else {            
    # no resource or too many resources specified 
    printCEMsg("EMsgERRMcliInvalidNumberOfOperands");
    &print_usage;
    return ERRM_CLI_BAD_OPERAND;
}

# check for copy condition flag
if (defined $opts{c}) {                 # -c for copy condition
    $copy_condition = $opts{c};
    $Opt_Copy_Condition = $TRUE;        # opt flag specified
    if ($copy_condition eq ""){         # no name for -c
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-c");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
    else {
       # extract the locator if it exists
       ($temp_name, $locator) = get_locator_node($copy_condition);
       # copy the condition_name
       $copy_condition2 = $copy_condition; 
       # put back condition name without locator
       if ($locator ne "") { $copy_condition2 = $temp_name;}
      
       # make sure the condition to be copied already exists
       $rc = find_resource_name($copy_condition2,$RSCCOND,$locator);
       if ($rc != 0) {                  # condition name not found
          if ($rc != $RMC_CLI_USER_ERROR) {
             printEMsg("EMsgmkconditionConditionNotFound",$copy_condition);
          }
          exit(ERRM_CLI_USER_ERROR);
       }
    }
}

# check for resource class to be monitored (required)
if (defined $opts{r}) {                 # -r for resource class
    $resource_name = $opts{r};
    $Opt_Resource = $TRUE;              # opt flag specified
    if ($resource_name eq ""){          # no resource name for -r
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-r");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}
else {
    # -r not specified and is required when no -c for copy
    if (!$Opt_Copy_Condition) {
       printCEMsg("EMsgERRMcliMissingRequiredFlag","-r");
       &print_usage;
       return ERRM_CLI_BAD_FLAG;
    }
}

# check for event expression (required)
if (defined $opts{e}) {                 # -e for event expression 
    $event_expr = $opts{e};
    $Opt_Event_Expr = $TRUE;            # opt flag specified
    if ($event_expr eq ""){             # no expression for -e     
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-e");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}
else {
    # -e not specified and is required when no -c for copy
    if (!$Opt_Copy_Condition) {
       printCEMsg("EMsgERRMcliMissingRequiredFlag","-e");
       &print_usage;
       return ERRM_CLI_BAD_FLAG;
    }
}

# check for rearm event expression (not required)
if (defined $opts{E}) {                 # -E for rearm expr       
    $rearm_expr = $opts{E};         
    $Opt_Rearm_Expr = $TRUE;            # opt flag specified
    if ($rearm_expr eq ""){             # no text for -E     
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-E");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# check for event description (not required)
if (defined $opts{d}) {                 # -d for event expr descr
    $event_desc = $opts{d};         
    $Opt_Event_Descr = $TRUE;           # opt flag specified
    if ($event_desc eq ""){             # no text for -d
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-d");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# check for rearm event description (not required)
if (defined $opts{D}) {                 # -D for rearm expr descr
    $rearm_desc = $opts{D};
    $Opt_Rearm_Descr = $TRUE;           # opt flag specified
    if ($rearm_desc eq ""){             # no text for -D
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-D");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# check for selection string (not required)
if (defined $opts{s}) {                 # -s for selection string
    $select_str = $opts{s};
    $Opt_Selection = $TRUE;             # opt flag specified
    if ($select_str eq ""){             # no string for -s
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-s");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# check for event severity (not required)
$severity = $MKCNDINFO;                 # default severity
if (defined $opts{S}) {                 # -S for severity 
    $sev_value = $opts{S};
    $Opt_Severity = $TRUE;              # opt flag specified
    if ($severity eq ""){               # no severity for -S
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-S");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
    else {                              # check severity used
      if ($sev_value eq $SEVCRIT) { $severity = $MKCNDCRIT; }
      elsif ($sev_value eq $SEVWARN) { $severity = $MKCNDWARN;}
      elsif ($sev_value eq $SEVINFO) { $severity = $MKCNDINFO;}
      else {                            # wrong severity
         printEMsg("EMsgmkconditionInvalidSeverity",$sev_value,"-S");
         &print_usage;
         return ERRM_CLI_BAD_OPERAND;
      }
    }           
}

# check for host list (not required)
if (defined $opts{n}) {                 # -n for node list
    $node_list = $opts{n};
    $Opt_Nodes = $TRUE;                 # opt flag specified
    if ($node_list eq ""){              # no string for -n
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-n");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
}

# check for management scope (not required)
if (defined $opts{m}) {                 # -m for mgt scope
    $mgt_scope = $opts{m};
    $Opt_Mgt_Scope = $TRUE;             # opt flag specified
    if ($mgt_scope eq ""){              # no text for -m
       printCEMsg("EMsgERRMcliMissingRequiredFlagOperand","-m");
       &print_usage;
       return ERRM_CLI_BAD_OPERAND;
    }
    else {                              # check scope used
      if ($mgt_scope eq $LCL_SCOPE) { $mgt_scope_val = $LCL_SCOPE_VAL; }
      elsif ($mgt_scope eq $SR_SCOPE) {$mgt_scope_val = $SR_SCOPE_VAL;}
      elsif ($mgt_scope eq $SR_SCOPE_OLD) {$mgt_scope_val = $SR_SCOPE_VAL;}
      elsif ($mgt_scope eq $CSM_SCOPE) {$mgt_scope_val = $CSM_SCOPE_VAL;}
      elsif ($mgt_scope eq $CSM_SCOPE_OLD) {$mgt_scope_val = $CSM_SCOPE_VAL;}
      elsif ($mgt_scope eq $CSM_SCOPE_OLD2) {$mgt_scope_val = $CSM_SCOPE_VAL;}
      else {                            # wrong scope
         printEMsg("EMsgmkconditionInvalidScope",$mgt_scope,"-m");
         &print_usage;
         return ERRM_CLI_BAD_OPERAND;
      }
    }
}

if (defined $opts{qnotoggle}) 
{
    if (defined $opts{qtoggle})
    {
        printCEMsg("EMsgERRMcliImproperUsageCombination", "--qnotoggle", "--qtoggle");
        &print_usage;
        return ERRM_CLI_BAD_FLAG;
    }
    $Opt_No_Toggle= $TRUE;
}
elsif (defined $opts{qtoggle})
{
    $Opt_Toggle= $TRUE;
}

# return everything back to main
return(0, $resource, $resource_name, $event_expr,
       $event_desc, $rearm_expr, $rearm_desc, $select_str, 
       $severity, $node_list, $copy_condition,
       $mgt_scope_val, $put_node);      # success
}   # end parse_cmd_line


#--------------------------------------------------------------------#
# print_usage : print the usage statement (syntax) to stdout.        #
#--------------------------------------------------------------------#
sub print_usage
{
&printIMsg("IMsgmkconditionUsage3");
}   # end print_usage


#--------------------------------------------------------------------#
# get_condition_info : Uses the list resource RMC CLI command to     #
#   list certain attributes of a condition.                          # 
#                                                                    #
# Paramaters:                                                        #
#   $condition_name  the condition to list                           #
#   $attr_list       double colon delimited list of attribute names  #
#                                                                    #
# Returns:                                                           #
#   @attr_vals       list of attribute values                        #
#                                                                    #
# Global Variables modified:                                         #
#   None.                                                            #
#--------------------------------------------------------------------#
sub get_condition_info
{
my $condition_name = shift(@_);
my $attr_list = shift(@_);
my $attr_vals = ();
my $trace_opt = "";
my $locator  = "";
my $temp_name = "";
my $node_select = "";
my @lsr_out = ();
my $rc = 0;

# extract the locator if it exists
($temp_name, $locator) = get_locator_node($condition_name);
# put back condition name without locator
if ($locator ne "") { $condition_name = $temp_name;}

# re-establish original CT_MANAGEMENT_SCOPE
set_orig_rmc_scope;

# check to see if resource name has a Node_name to locate it.
# if not, set rmc local scope.  if it does, use it in select string.
if ($locator eq "") {
   # set CT_MANAGEMENT_SCOPE to local for query
   $ENV{CT_MANAGEMENT_SCOPE} = $RMC_LOCAL_SCOPE;
}
# else use location is select string
else {
   $node_select = " && NodeNameList |< {\\\"$locator\\\"}";

   # use DM/SR/Local scope if scope not set and there's a locator
   check_set_cluster_scope;
}

# list condition to get the attr values
if ($Trace) { print STDERR "$PROGNAME: calling lsrsrc-api\n";}

$ENV{CT_CLI_QUOTE_STRING} = 0;
@lsr_out = `$CTBINDIR/lsrsrc-api -D $DELIMITER -s ${RSCCOND}::\"Name==\\\"$condition_name\\\"${node_select}\"::::::${attr_list} 2>&1`;

# capture the return code from lsrsrc
$rc = $?;
$rc = process_exit_code($rc);

if ($Trace) { print STDERR "lsrsrc-api results:\n";
              print STDERR "@lsr_out";}

if ($Trace) { print STDERR "$PROGNAME: lsrsrc-api returned $rc\n";}

# show any errors if there was a bad rc
if ($rc != 0) {
   process_api_error($DELIMITER,$rc,@lsr_out);
}

# return ERRM CLI user error if it's an RMC CLI user error
($rc == $RMC_CLI_USER_ERROR) && ( error_exit(ERRM_CLI_USER_ERROR) );

# if list command failed, print RMC error message and exit
if ($rc != 0) {
    printCEMsg("EMsgERRMcliUnExpectRMCrc",$rc);
    exit(ERRM_CLI_RMC_ERROR);
    }

# get attribute values
$attr_vals = $lsr_out[0];
chomp($attr_vals);

return ($attr_vals);
}
