#!/usr/bin/perl

 

  
$| = 1;
  
use File::Basename;

use POSIX 'locale_h';
use locale;


BEGIN {
  $scriptdir = &File::Basename::dirname($0);
  # May want to use shift to put it at the beginning
  push @INC, "$scriptdir/lib/";
  push @INC, "$scriptdir/../lib/";  
  push @INC, "$scriptdir/../lib/resources/";    
  push @INC, "$scriptdir/../share/lib/resources/";    # for linux
  push @INC, "$scriptdir/../share/lib/";              # for linux
}
 

#
# Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
# This software is the proprietary information of Sun Microsystems, Inc.
# Use is subject to license terms.
# 
# Copyright 2004 Sun Microsystems, Inc.  Tous droits rservs.
# Ce logiciel est proprit de Sun Microsystems, Inc.
# Distribu par des licences qui en restreignent l'utilisation.
#
# ident        "@(#)mfwkadm 1.17     04/09/06 SMI"
#
#

# Solaris 8 does not have Gettext in standard
# We provide our own implem
use JesmfGettext;
#use JesmfMessages;


use Getopt::Long;
use POSIX;
use utf8;

#-------------------------------------------------
#  Usage
#-------------------------------------------------


sub usage
{
  printMessage ($JesmfMessages::MFWK_USG1);
  printMessage ($JesmfMessages::MFWK_USG2);
  printMessage ($JesmfMessages::MFWK_USG3);
  printMessage ($JesmfMessages::MFWK_USG4);
  printMessage ($JesmfMessages::MFWK_USG5);
  printMessage ($JesmfMessages::MFWK_USG6);
  printMessage ($JesmfMessages::MFWK_USG7);
  printMessage ($JesmfMessages::MFWK_USG8);
  printMessage ($JesmfMessages::MFWK_USG9);
  printMessage ($JesmfMessages::MFWK_USG10);
  printMessage ($JesmfMessages::MFWK_USG11);
  printMessage ($JesmfMessages::MFWK_USG12);
  printMessage ($JesmfMessages::MFWK_USG13);
  printMessage ($JesmfMessages::MFWK_USG14);
  printMessage ($JesmfMessages::MFWK_USG15);
  printMessage ($JesmfMessages::MFWK_USG16);
  printMessage ($JesmfMessages::MFWK_USG17);
  printMessage ($JesmfMessages::MFWK_USG18);
  printMessage ($JesmfMessages::MFWK_USG19);
  printMessage ($JesmfMessages::MFWK_USG20);
  printMessage ($JesmfMessages::MFWK_USG21);
  printMessage ($JesmfMessages::MFWK_USG22);
  printMessage ($JesmfMessages::MFWK_USG23);
  printMessage ($JesmfMessages::MFWK_USG24);
  printMessage ($JesmfMessages::MFWK_USG25);
  printMessage ($JesmfMessages::MFWK_USG26);
  printMessage ($JesmfMessages::MFWK_USG27);
  printMessage ($JesmfMessages::MFWK_USG28);
  printMessage ($JesmfMessages::MFWK_USG29);
  printMessage ($JesmfMessages::MFWK_USG30);
  printMessage ($JesmfMessages::MFWK_USG31);
  printMessage ($JesmfMessages::MFWK_USG32);  
  printMessage ($JesmfMessages::MFWK_USG33);  
  printMessage ($JesmfMessages::MFWK_USG34);  
  printMessage ($JesmfMessages::MFWK_USG35);  
  printMessage ($JesmfMessages::MFWK_USG36);  
  printMessage ($JesmfMessages::MFWK_USG37);  
  printMessage ($JesmfMessages::MFWK_USG38);  
  printMessage ($JesmfMessages::MFWK_USG39);
  printMessage ($JesmfMessages::MFWK_USG40);
  printMessage ($JesmfMessages::MFWK_USG41);
  printMessage ($JesmfMessages::MFWK_USG42);  
  printMessage ($JesmfMessages::MFWK_USG43);  
  printMessage ($JesmfMessages::MFWK_USG44);  
  printMessage ($JesmfMessages::MFWK_USG45); 
}


#-------------------------------------------------
#  Command line subcommand parsing
#-------------------------------------------------
sub ParseSubCommands (@)
{
  $true=1;

# we just parse the args that remain after the getoptions
# parsing is very basic and just sets variables corresponding
# to subcommands.
# we do not check subcommands consistency at this level

# the only thing being checked is the case of job creation
# where we can have multiple object
# command consistency has to be checked @ this level.
 
  @allargs = @_;
  
  $firstarg = shift @_;

  # agent subcommands (start/stop/restart/list-params)

  if ($firstarg eq "start") {
      $agent = $true;
      $start = $true;
  }
  
  if ($firstarg eq "stop") {
      $agent = $true;
      $stop = $true;
  }
  if ($firstarg eq "restart") {
      $agent = $true;
      $restart = $true;
  }
  if ($firstarg eq "list-params") {
      $agent = $true;
      $config = $true;
  }


  # module subcommands
  if ($firstarg eq "info") {
    $module = $true;   
    $info = shift @_;
  }
  if ($firstarg eq "list-modules") {
    $module = $true;   
    $list = $true;      
  }
 
  # perfjob subcommands (list/info/create/delete/suspend/resume/observable_classes/observable_objects/observable_attributes)
  if ($firstarg eq "pm-job") {
    $perfjob = $true;
    $secondarg = shift @_;
    if ($secondarg eq "list") {
      $list = $true;
    }
    if  ($secondarg eq "info") {
      $info = shift @_;
    }
    if  ($secondarg eq "delete") {
      $delete = shift @_;
    }
    if  ($secondarg eq "suspend") {
      $suspend = shift @_;
    }
    if  ($secondarg eq "resume") {
      $resume = shift @_;
    }
    if ($secondarg eq "create") {
      $create = shift @_;
      $thirdarg = shift @_;
      if  ($thirdarg =~ /^granularity=/) {
        $granularity = $';
        while (@_) {
          $ntharg = shift @_;
          if ($ntharg =~ /^object=/) {
            $newobject = $';
            if (defined ($newobject)) {
              push @object,$newobject;
	          } else {
              debug ("object empty");
              error_usage();
	          }
          } else {
            # at this point only object arg should remain.
            # if not it the cli is wrong
            debug ("object loop");
            error_usage();
	        }
	      }
      }             
    }
    if  ($secondarg eq "observable-classes") {
      $observable_classes = $true;
    }
    if  ($secondarg eq "observable-objects") {
      $observable_objects = $true;      
      while (@_) {
          $ntharg = shift @_;
          if  ($ntharg =~ /^class=/) {
            $class = $';
            if ($class eq "") {
                debug ("class empty");
                error_usage();
            }
          }
          if  ($ntharg =~ /^domain=/) {
            $domain = $';
            if ($domain eq "") {
                debug ("domain empty");
                error_usage();
            }
          }
      }
    }
    if  ($secondarg eq "observable-attributes") {
      $observable_attributes = $true;
      $thirdarg = shift @_;
      if  ($thirdarg =~ /^class=/) {
        $class = $';
        if ($class eq "") {
            debug ("class empty");
            error_usage();
        }
      }
    }
  }

  # opstatusjob subcommands (list/info/create/delete/suspend/resume/observable_classes/observable_objects/observable_attributes)
  if ($firstarg eq "opstat-job") {
    $opstatusjob = $true;
    $secondarg = shift @_;
    if ($secondarg eq "list") {
      $list = $true;
    }
    if  ($secondarg eq "info") {
      $info = shift @_;
    }
    if  ($secondarg eq "delete") {
      $delete = shift @_;
    }
    if  ($secondarg eq "suspend") {
      $suspend = shift @_;
    }
    if  ($secondarg eq "resume") {
      $resume = shift @_;
    }
    if ($secondarg eq "create") {
      $create = shift @_;
      $thirdarg = shift @_;
      if  ($thirdarg =~ /^granularity=/) {
        $granularity = $';
        while (@_) {
          $ntharg = shift @_;
          if ($ntharg =~ /^object=/) {
            $newobject = $';
            if (defined ($newobject)) {
              push @object,$newobject;
	          } else {
              debug ("object empty");
              error_usage();
	          }
          } else {
            # at this point only object arg should remain.
            # if not it the cli is wrong
            debug ("object loop");
            error_usage();
	        }
	      }
      }             
    }
    if  ($secondarg eq "observable-classes") {
      $observable_classes = $true;
    }
    if  ($secondarg eq "observable-objects") {
      $observable_objects = $true;      
      while (@_) {
          $ntharg = shift @_;
          if  ($ntharg =~ /^class=/) {
            $class = $';
            if ($class eq "") {
                debug ("class empty");
                error_usage();
            }
          }
          if  ($ntharg =~ /^domain=/) {
            $domain = $';
            if ($domain eq "") {
                debug ("domain empty");
                error_usage();
            }
          }
      }
    }
    if  ($secondarg eq "observable-attributes") {
      $observable_attributes = $true;
      $thirdarg = shift @_;
      if  ($thirdarg =~ /^class=/) {
        $class = $';
        if ($class eq "") {
            debug ("class empty");
            error_usage();
        }
      }
    }
  }


  # Threshold subcommands (list/info/create/delete/suspend/resume/observable_classes/observable_objects/observable_attributes)
  if ($firstarg eq "thrsh-job") {
    $thrshjob = $true;
    $secondarg = shift @_;
    if ($secondarg eq "list") {
      $list = $true;
    }
    if  ($secondarg eq "info") {
      $info = shift @_;
    }
    if  ($secondarg eq "delete") {
      $delete = shift @_;
    }
    if  ($secondarg eq "suspend") {
      $suspend = shift @_;
    }
    if  ($secondarg eq "resume") {
      $resume = shift @_;
    }
    if ($secondarg eq "create") {
      $create = shift @_;
      while (@_) {
        $ntharg = shift @_;
        if ($ntharg =~ /^object=/) {
            $object = $';
        } elsif ($ntharg =~ /^granularity=/) {
            $granularity = $';
        } elsif ($ntharg =~ /^attributeName=/) {
            $attributeName = $';
        } elsif ($ntharg =~ /^attributeType=/) {
            $attributeType = $';
        } elsif ($ntharg =~ /^thresholdValue=/) {
            $thresholdValue = $';
        } elsif ($ntharg =~ /^thresholdOffset=/) {
            $thresholdOffset = $';      
        } elsif ($ntharg =~ /^thresholdDirection=/) {
            $thresholdDirection = $';      
        } else {
            # at this point only object arg should remain.
            # if not, the cli is wrong
            debug ("object loop");
            error_usage();
	}
      }             
    }
    if  ($secondarg eq "observable-classes") {
      $observable_classes = $true;
    }
    if  ($secondarg eq "observable-objects") {
      $observable_objects = $true;      
      while (@_) {
          $ntharg = shift @_;
          if  ($ntharg =~ /^class=/) {
            $class = $';
            if ($class eq "") {
                debug ("class empty");
                error_usage();
            }
          }
          if  ($ntharg =~ /^domain=/) {
            $domain = $';
            if ($domain eq "") {
                debug ("domain empty");
                error_usage();
            }
          }
      }
    }
    if  ($secondarg eq "observable-attributes") {
      $observable_attributes = $true;
      $thirdarg = shift @_;
      if  ($thirdarg =~ /^class=/) {
        $class = $';
        if ($class eq "") {
            debug ("class empty");
            error_usage();
        }
      }
    }
  } 
}

sub CheckSubCommandsConsistency() {

if ($help) {
     debug ("help");
     usage();
     exit(0);
  } elsif (!($agent xor $module xor $perfjob xor $opstatusjob xor $thrshjob)) {
     debug ("agent | module | pm-job | opstat-job | thrsh-job missing");
     error_usage();
  } elsif ($agent && (!($start xor $stop xor $restart xor $config))) {
     debug ("agent");
     error_usage();
  } elsif ($module && (!($list xor defined ($info) ))) {
     debug ("module");
     error_usage();
  } elsif ($perfjob &&     (!($list xor defined ($info) xor defined ($delete) xor defined ($suspend) xor defined ($resume) xor (defined ($create) && defined ($granularity) && defined (@object)) xor $observable_classes xor $observable_objects xor ($observable_attributes && defined($class)) ))) {
     debug ("perfjob");
     error_usage();
  } elsif ($thrshjob &&    (!($list xor defined ($info) xor defined ($delete) xor defined ($suspend) xor defined ($resume) xor (defined ($create) && defined ($granularity) && defined ($object) && defined ($attributeName) && defined ($attributeType) && defined ($thresholdOffset) && defined ($thresholdDirection) && defined ($thresholdValue)) xor $observable_classes xor $observable_objects xor ($observable_attributes && defined($class))))) {
     debug ("thrshjob");
     error_usage();     
  } elsif ($opstatusjob && (!($list xor defined ($info) xor defined ($delete) xor defined ($suspend) xor defined ($resume) xor (defined ($create) && defined ($granularity) && defined (@object)) xor $observable_classes xor $observable_objects xor ($observable_attributes && defined($class))))) {
     debug ("opstatusjob");
     error_usage();
  }
}


#-------------------------------------------------
#  Command line parsing
#-------------------------------------------------
sub ParseCommandLineArgs (@)
{   

  $result = GetOptions ('help|?'       => \$help,		      
                        "debug"          => \$debug,	    		    	    		  
 		     ) or error_usage();

  # ARGV does not contain options anymore, only subcommands (not "--" or "-" prefixed)
  # we parse it, set global variables accordingly and check consistency of what has been set
  &ParseSubCommands(@ARGV);

  debug ("\
          help                  => $help\ 
          debug                 => $debug\ 
          agent                 => $agent\ 
          start                 => $start\
          stop                  => $stop\ 
          restart               => $restart\ 
          config                => $config\ 
          module                => $module\ 
          pm-job                => $perfjob\ 
          opstat-job            => $opstatusjob\ 
          thrsh-job             => $thrshjob\ 
          list                  => $list\ 
          info                  => $info\ 
          create                => $create\ 
          granularity           => $granularity\ 
          object                => @object\ 
          delete                => $delete\ 
          suspend               => $suspend\ 
          resume                => $resume\
          observable_classes    => $observable_classes\
          observable_objects    => $observable_objects\
          observable_attributes => $observable_attributes\
          class                 => $class\
          domain                => $domain");

  # check the combination of subcommands is consistent
  &CheckSubCommandsConsistency();		    	 
}



#-------------------------------------------------
#  Set Cacao Paths And Commands
#-------------------------------------------------
sub SetCacaoPathsAndCommands
{

  debug ("Entering SetCacaoPathsAndCommands");
  ($OS, $nodename, $release, $version, $machine) = POSIX::uname();
  debug ("OS $OS");

  $info_cmd = &File::Basename::dirname($0);
  if($OS eq "Windows NT"){
   $info_cmd = "$CMD_SH $info_cmd\\mfwksetup.pl list-settings | grep ";
  }else{
  $info_cmd = "$CMD_SH $info_cmd/mfwksetup list-settings | grep ";
 }
  debug ("Getting CACAO_HOME");
  $retcmd = `$info_cmd CACAO_HOME`;
  $_ = $retcmd;
  if($OS eq "Windows NT") {
  /CACAO_HOME = (.*)/;
  $CACAO_HOME = $1;
  } elsif($OS eq "HP-UX"){
  /CACAO_HOME = (.*)/;
  $CACAO_HOME = $1;
  }else {
  /CACAO_HOME=(.*)/;
  $CACAO_HOME = $1;
  }
  debug ("Getting MFWK_HOME");
  $retcmd = `$info_cmd MFWK_HOME`;
  $_ = $retcmd;
  if($OS eq "Windows NT"){
  /MFWK_HOME = (.*)/;
  $MFWK_HOME = $1;
  } elsif ($OS eq "HP-UX"){
  /MFWK_HOME = (.*)/;
  $MFWK_HOME = $1;
  }else{
  /MFWK_HOME=(.*)/;
  $MFWK_HOME = $1;
  }
  debug ("Getting CACAO_INSTANCE_HOME");
  $retcmd = `$info_cmd CACAO_INSTANCE_HOME`;
  $_ = $retcmd;  
  if($OS eq "Windows NT"){
  /CACAO_INSTANCE_HOME = (.*)/;
  $CACAO_INSTANCE_HOME = $1;
  }elsif ($OS eq "HP-UX"){
  /CACAO_INSTANCE_HOME = (.*)/;
  $CACAO_INSTANCE_HOME = $1;
  }else{
  /CACAO_INSTANCE_HOME=(.*)/;
  $CACAO_INSTANCE_HOME = $1;
}

  debug ("Setting command variables");
  if ($OS eq "SunOS") {
      $CACAOADM     = "$CACAO_HOME/bin/cacaoadm";
      $CACAOCSC     = "$CACAO_HOME/lib/tools/cacaocsc";
      $SECURITY_DIR = "$CACAO_INSTANCE_HOME/security/nss/wellknown";
      $CACAO_PORT   = `$CACAOADM get-param commandstream-adaptor-port --value`;
      chomp($CACAO_PORT);
      $CACAO_PWD    = "$CACAO_INSTANCE_HOME/security/password";
      
  } elsif ($OS eq "Linux") {
      $CACAOADM     = "$CACAO_HOME/bin/cacaoadm";
      $CACAOCSC     = "$CACAO_HOME/private/bin/cacaocsc";
      $SECURITY_DIR = "$CACAO_INSTANCE_HOME/security/nss/wellknown";
      $CACAO_PORT   = `$CACAOADM get-param commandstream-adaptor-port --value`;
      chomp($CACAO_PORT);
      $CACAO_PWD    = "$CACAO_INSTANCE_HOME/security/password";
      
  # hpux-dev : 6461458 - Added code to support HP-UX
  } elsif ($OS eq "HP-UX") {
      $CACAOADM     = "$CACAO_HOME/bin/cacaoadm";
      $CACAOCSC     = "$CACAO_HOME/private/bin/cacaocsc";
      $SECURITY_DIR = "$CACAO_INSTANCE_HOME/security/nss/wellknown";
      $CACAO_PORT   = `$CACAOADM get-param commandstream-adaptor-port --value`;
      chomp($CACAO_PORT);
      $CACAO_PWD    = "$CACAO_INSTANCE_HOME/security/password";
      # hpux-dev : 6286644 : Corrected the certificate issue with Cacao
      #                      and MFWK
      $ENV{'LANG'} = "C.utf8";
  } elsif ($OS eq "Windows NT") {
      $CACAOADM	    = "$CACAO_HOME/bin/cacaoadm";
      $CACAOCSC     = "$CACAO_HOME/lib/tools/cacaocsc";
      $SECURITY_DIR = "$CACAO_INSTANCE_HOME/security/nss/wellknown";
      $CACAO_PORT   = `$CACAOADM get-param commandstream-adaptor-port --value`;
      chomp($CACAO_PORT);
      $CACAO_PWD    = "$CACAO_INSTANCE_HOME/security/password";
  } else {
      error ($JesmfMessages::MFWK_ERR_OS01, $OS);
  }
  
  
  $CACAOCSC_BASE_COMMAND = "$CACAOCSC -h localhost -p $CACAO_PORT -d $SECURITY_DIR -f $CACAO_PWD -c ";
  debug ("Exiting SetCacaoPathsAndCommands");
}



#-------------------------------------------------
#  Builds the CSC Cacao commands (cmd and args)
#-------------------------------------------------

sub BuildCscCommand {


  $mycmd = undef;


  if ($agent) {  
    debug("agent command");
    if ($config) {
      debug("list-params");
      $mycmd = GetCscString("agent config show");     
    }

  } elsif ($module) {   
    debug("module command");
    if ($list) {
      debug("list");
      $mycmd = GetCscString("module list");     
    } elsif (defined ($info)) {
      debug("info $info");
      $mycmd = GetCscString("module info $info");     
    } else {
     error($JesmfMessages::MFWK_ERR_INT01);
    }

  } elsif ($perfjob) {   
    debug("pm-job command");
    if ($list) {
      debug("list xxx");
      $mycmd = GetCscString("perfjob list xxx");
    } elsif (defined ($info)) {
      debug("info $info");
      $mycmd = GetCscString("perfjob info $info");     
    } elsif (defined ($delete)) {
      debug("delete $delete");
      $mycmd = GetCscString("perfjob delete $delete");     
    } elsif (defined ($suspend)) {
      debug("suspend $suspend");
      $mycmd = GetCscString("perfjob suspend $suspend");     
    } elsif (defined ($resume)) {
      debug("resume $resume");
      $mycmd = GetCscString("perfjob resume $resume");     
    } elsif (defined ($create)) {
      $objarray = join ('@@@', @object);
      debug("create $create granularity $granularity objarray $objarray");
      $mycmd = GetCscString("perfjob create $create granularity $granularity objarray ".$objarray);     
    } elsif ($observable_classes) {
      debug("observable_classes xxx");
      $mycmd = GetCscString("perfjob observable-classes xxx");
    } elsif ($observable_objects) {
      if (defined ($class)) {
        if (defined ($domain)) {
            debug("observable_objects xxx class $class domain $domain");
            $mycmd = GetCscString("perfjob observable-objects xxx class $class domain $domain"); 
        } else {
            debug("observable_objects xxx class $class");
            $mycmd = GetCscString("perfjob observable-objects xxx class $class");  
        }
      } else {
        if (defined ($domain)) {
            debug("observable_objects xxx domain $domain");
            $mycmd = GetCscString("perfjob observable-objects xxx domain $domain"); 
        } else {
            debug("observable_objects xxx");
            $mycmd = GetCscString("perfjob observable-objects xxx");  
        }
      }         
    } elsif ($observable_attributes) {
      debug("observable_attributes xxx class $class");
      $mycmd = GetCscString("perfjob observable-attributes xxx class $class");     
    } else  {
     error($JesmfMessages::MFWK_ERR_UNK02);
    }

  } elsif ($opstatusjob) {   
    debug("opstat-job command");
    if ($list) {
      debug("list xxx");
      $mycmd = GetCscString("opstatusjob list xxx");     
    } elsif (defined ($info)) {
      debug("info $info");
      $mycmd = GetCscString("opstatusjob info $info");     
    } elsif (defined ($delete)) {
      debug("delete $delete");
      $mycmd = GetCscString("opstatusjob delete $delete");     
    } elsif (defined ($suspend)) {
      debug("suspend $suspend");
      $mycmd = GetCscString("opstatusjob suspend $suspend");     
    } elsif (defined ($resume)) {
      debug("resume $resume");
      $mycmd = GetCscString("opstatusjob resume $resume");     
    } elsif (defined ($create)) {
      $objarray = join ('@@@', @object);
      debug("create $create granularity $granularity object @object");
      $mycmd = GetCscString("opstatusjob create $create granularity $granularity objarray ".$objarray);     
    } elsif ($observable_classes) {
      debug("observable_classes xxx");
      $mycmd = GetCscString("opstatusjob observable-classes xxx");
    } elsif ($observable_objects) {
      if (defined ($class)) {
        if (defined ($domain)) {
            debug("observable_objects xxx class $class domain $domain");
            $mycmd = GetCscString("opstatusjob observable-objects xxx class $class domain $domain"); 
        } else {
            debug("observable_objects xxx class $class");
            $mycmd = GetCscString("opstatusjob observable-objects xxx class $class");  
        }
      } else {
        if (defined ($domain)) {
            debug("observable_objects xxx domain $domain");
            $mycmd = GetCscString("opstatusjob observable-objects xxx domain $domain"); 
        } else {
            debug("observable_objects xxx");
            $mycmd = GetCscString("opstatusjob observable-objects xxx");  
        }
      }         
    } elsif ($observable_attributes) {
      debug("observable_attributes xxx class $class");
      $mycmd = GetCscString("opstatusjob observable-attributes xxx class $class");     
    } else {
     error($JesmfMessages::MFWK_ERR_UNK03);
    } 

  } elsif ($thrshjob) {   
    debug("thrsh-job command");
    if ($list) {
      debug("list xxx");
      $mycmd = GetCscString("thrshjob list xxx");     
    } elsif (defined ($info)) {
      debug("info $info");
      $mycmd = GetCscString("thrshjob info $info");     
    } elsif (defined ($delete)) {
      debug("delete $delete");
      $mycmd = GetCscString("thrshjob delete $delete");     
    } elsif (defined ($suspend)) {
      debug("suspend $suspend");
      $mycmd = GetCscString("thrshjob suspend $suspend");     
    } elsif (defined ($resume)) {
      debug("resume $resume");
      $mycmd = GetCscString("thrshjob resume $resume");     
    } elsif (defined ($create)) {
      debug("create $create granularity $granularity object $object attributeName $attributeName attributeType $attributeType thresholdValue $thresholdValue thresholdOffset $thresholdOffset thresholdDirection $thresholdDirection");
      $mycmd = GetCscString("thrshjob create $create granularity $granularity object $object attributeName $attributeName attributeType $attributeType thresholdValue $thresholdValue thresholdOffset $thresholdOffset thresholdDirection $thresholdDirection");
    } elsif ($observable_classes) {
      debug("observable_classes xxx");
      $mycmd = GetCscString("thrshjob observable-classes xxx");
    } elsif ($observable_objects) {
      if (defined ($class)) {
        if (defined ($domain)) {
            debug("observable_objects xxx class $class domain $domain");
            $mycmd = GetCscString("thrshjob observable-objects xxx class $class domain $domain"); 
        } else {
            debug("observable_objects xxx class $class");
            $mycmd = GetCscString("thrshjob observable-objects xxx class $class");  
        }
      } else {
        if (defined ($domain)) {
            debug("observable_objects xxx domain $domain");
            $mycmd = GetCscString("thrshjob observable-objects xxx domain $domain"); 
        } else {
            debug("observable_objects xxx");
            $mycmd = GetCscString("thrshjob observable-objects xxx");  
        }
      }         
    } elsif ($observable_attributes) {
      debug("observable_attributes xxx class $class");
      $mycmd = GetCscString("thrshjob observable-attributes xxx class $class");     
    } else {
     error($JesmfMessages::MFWK_ERR_UNK04); 
    } 

  } else {
     error($MFWK_ERR_UNK01);
  }
  return($mycmd);
}


#-------------------------------------------------
#  Builds the CSC Cacao commands (cmd and args)
#-------------------------------------------------

sub GetCscString($) {
  return ("$CACAOCSC_BASE_COMMAND"."\""."$MFWK_BASE_COMMAND"."@_"."\""); 
}


#-------------------------------------------------
# start/stop/restart agent
#-------------------------------------------------

sub startAgent(){

  if($OS ne "Windows NT"){
  $this_cmd = "$CACAOADM unlock com.sun.mfwk 2>&1";
  }else{
  $this_cmd = "$CACAOADM unlock com.sun.mfwk";
  }
  $retcmd = `$this_cmd`;    
  if ($? != 0) {
    error($JesmfMessages::MFWK_ERR_EXE01, ($this_cmd,$retcmd,$!));
  }
  if($OS ne "Windows NT"){
  @module_list = `$CACAOADM list-modules 2>&1`;
  }else {
  @module_list = `$CACAOADM list-modules`;
  }
  @other_module = grep /com.sun.cmm./, @module_list;  
  for $module (@other_module) {
      $module =~ /(.*) .*/;
	  if($OS ne "Windows NT"){
      $this_cmd = "$CACAOADM unlock $1 2>&1";
	  }else {
	  $this_cmd = "$CACAOADM unlock $1";
	  }
      $retcmd = `$this_cmd`;    
      if ($? != 0) {
        error($JesmfMessages::MFWK_ERR_EXE01, ($this_cmd,$retcmd,$!));
      }
  }

}

		
sub stopAgent() {  
   if($OS ne "Windows NT"){
  @module_list = `$CACAOADM list-modules 2>&1`;
   }else {
  @module_list = `$CACAOADM list-modules`;
   }
  @other_module = grep /com.sun.cmm./, @module_list;  
  for $module (@other_module) {
      $module =~ /(.*) .*/;
	  if($OS ne "Windows NT"){
      $this_cmd = "$CACAOADM lock $1 2>&1";
	  }else {
      $this_cmd = "$CACAOADM lock $1";
	  }
      $retcmd = `$this_cmd`;    
      if ($? != 0) {
        error($JesmfMessages::MFWK_ERR_EXE01, ($this_cmd,$retcmd,$!));
      }
  }
  
  if($OS ne "Windows NT"){
  $this_cmd = "$CACAOADM lock com.sun.mfwk 2>&1";
  } else {
  $this_cmd = "$CACAOADM lock com.sun.mfwk";
  }
  $retcmd = `$this_cmd`;    
  if ($? != 0) {
    error($JesmfMessages::MFWK_ERR_EXE01, ($this_cmd,$retcmd,$!));
  }  
}



# Check the user ID to see if it matches root's (0).
# Return 0 if the user is root. Exit with status 1 otherwise.
sub CheckRootUser() {

	$userId=`id`;
        debug ("id returned: $userId");
        $userId =~ s/uid=([0-9]*).*/\1/;
        debug ("userId: $userId");
        if ($userId != 0) {
          error ($JesmfMessages::MFWK_ERR_ROOT01)
	}

}




sub executeCmd($){

    my ($cmd) = @_;

    debug("executeCmd: $cmd");
	 if($OS ne "Windows NT"){
    $output = `$cmd 2>&1`;
	 }else  {
    $output = `$cmd`;
	 }
    debug("Output: $output");
    if ($? != 0) {
      error ($JesmfMessages::MFWK_ERR_ST04, ($cmd,$output,$!));
    } else {
      print STDOUT ($output);
    }
}


  sub error_usage () {   
    debug("error_usage:");
    usage();
    exit 1;
  }

 
  sub error ($) {
    my ($message,@args) = @_;
    my $untranslatedGenericMsg = $JesmfMessages::MFWK_ERR_UNK05;
    my $translatedGenericMsg = $GETTEXTHANDLE->gettext($untranslatedGenericMsg); 
    debug("error:\nmessage=$message\nargs=@args\n");

    printf STDERR ($translatedGenericMsg, $0);

    my $translatedmsg = $GETTEXTHANDLE->gettext($message);
    printf STDERR ($translatedmsg,@args);
    exit 1;
  }

  sub printMessage() {
    my ($message,@args) = @_;
    my $translatedmsg = $GETTEXTHANDLE->gettext($message);
    printf STDOUT ($translatedmsg,@args);    
  }
 
  sub debug ($) {
    my ($message) = @_;
    if ($debug) {
      $ts = time;
      print "$ts $message\n";
    }
  }

  ##################
  # main starts here
  ##################
  
  # CLI options init

  $help        = 0;
  $debug       = 0;
  $agent       = 0;
  $start       = 0;
  $stop        = 0;
  $restart     = 0;
  $config      = undef;
  $module      = 0;
  $perfjob     = 0;
  $opstatusjob = 0;
  $thrshjob    = 0;  
  $list        = 0;
  $info        = undef;
  $create      = undef;
  $granularity = undef;
  $object      = undef;
  $observable_classes = 0;
  $observable_objects = 0;
  $observable_attributes = 0;
  $class       = undef;
  $domain      = undef;
  $attributeName   = undef;
  $attributeType   = undef;
  $thresholdValue  = undef;
  $thresholdOffset = undef;
  $thresholdDirection = undef;
  $delete      = undef;
  $suspend     = undef;
  $resume      = undef;
  ($OS, $nodename, $release, $version, $machine) = POSIX::uname();
  if($OS eq "Windows NT") {
  $CMD_SH   = "";
  }else{
  $CMD_SH     = "/bin/sh";
  }

#$debug=1;

  debug ("Treating localization");
  $lc_messages = setlocale(LC_MESSAGES);
  $lc_ctype = setlocale(LC_CTYPE);  

  if($lc_ctype eq "C") {
      require JesmfMessages;

  } elsif($lc_ctype eq "") {
      require JesmfMessages;

  } elsif($lc_messages eq "") {
      require JesmfMessages;

  } elsif ($lc_messages eq "C") {
      require JesmfMessages;

  } else {
      $locFile1 = "$scriptdir/../lib/resources/JesmfMessages_$lc_messages.pm";
      $locFile2 = "$scriptdir/../share/lib/resources/JesmfMessages_$lc_messages.pm";

      if (-e $locFile1 || -e $locFile2) {
          require "JesmfMessages_$lc_messages.pm";
      } else {
          require JesmfMessages;
      }
  }

  # for I18N
  # Solaris 8 does not provide gettext in standard 
  # => we had to redevelop it
  $GETTEXTHANDLE = JesmfGettext->new();
  $GETTEXTHANDLE->textdomain("mfwkadm"); 

  # Cacao stuff (paths, commands and ports)
  $SECURITY_DIR = undef;

  $CACAOCSC = undef;
  $CACAOADM            = undef;
#  $CACAOCSA_PORT = 11163;
  $CACAOCSC_BASE_COMMAND = undef;

  #constant for our CLI 
  $MFWK_BASE_COMMAND = "com.sun.mfwk:";
  $MFWKMODULEPREFIXPATTERN = "com.sun.mfwk";
  $MFWKMODULEEXTPATTERN = ".xml";

  #for deploy
  $MFWKAGENTMODULEDESC = "com.sun.mfwk.xml";
  #for undeploy
  $MFWKAGENTMODULE = "com.sun.mfwk";

  # then we parse the args and update the variables accordingly...
  debug ("Parse command line arguments");  
  ParseCommandLineArgs(@ARGV);

  # We check if command is run as root (exit 1 if not)
#  CheckRootUser();

  # sets OS specific Paths for Cacao
  debug ("Set cacao paths and commands");    
  SetCacaoPathsAndCommands();

  # Before anything check if MA is here, if yes, leave
  debug ("Check if MA is locale");      
  if($OS ne "Windows NT"){
  @module_list = `$CACAOADM list-modules 2>&1`;
  } else {
  @module_list = `$CACAOADM list-modules`;
  }
  @ma_module = grep /com.sun.mfwk.masteragent/, @module_list;  
  for $module (@ma_module) {
      error($JesmfMessages::MFWK_ERR_EXE02);
  }


  # for "agent show config", "module", "perfjob" "thrshjob" and "opstatusjob" cmds, 
  # we use pure cacao CSC commands
  if (($agent && defined ($config)) or $module or $perfjob or $opstatusjob or $thrshjob) {

    $cmd = BuildCscCommand();
    # executes the Cacao CSC command
    debug("cmd: $cmd");
    executeCmd ($cmd);
  
  # for "agent start/stop/restart we optionally use cacao CSC to extract
  # some info and then wrap cacaoadm
  } elsif  ($agent && ($start || $stop || $restart)) {
    if ($start) {  
      debug("agent start");
      startAgent();
    } elsif ($stop) {
      debug("agent stop");  
      stopAgent();
    } elsif ($restart) {
      debug("agent restart");  
      stopAgent();
      startAgent();
    } else {
      error($JesmfMessages::MFWK_ERR_UNK01);
    }
  } else {
    error($JesmfMessages::MFWK_ERR_UNK01);
  } 





