#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,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 
# @(#)50       1.3  src/rsct/rm/ServiceRM/utils/removermcacl, ServiceRM, rsct_relgh, relgh_base 9/12/05 13:14:08

if ( ($#ARGV<0) || ($#ARGV==1) || ( ($#ARGV>=2) && ($#ARGV % 2 !=0) ) )
{
  print <<FNUSAGETEXT;
usage : removermcacl <Class> <Userdesc> <Permissions> [ <Userdesc> <Permissions> ... ]
        removermcacl <Class> 

FNUSAGETEXT
exit 1;
}

# sample input is:
# removermcacl IBM.Foo     joe@LOCALHOST rw
#              $ClassName  $UserDesc[0]  $Perms[0]
# removermcacl IBM.Foo     joe@LOCALHOST rw         jane@somehost r
#              $ClassName  $UserDesc[0]  $Perms[0]  $UserDesc[1]  $Perms[1]
# removermcacl IBM.Foo 
#              $ClassName

# get the class name specified
my $ClassName = $ARGV[0];

# get the pairs of user/permissions
my @UserDesc = ();
my @Perms    = ();

for (my $i=0, my $j=1; $j<=$#ARGV; $j=$j+2, $i++) {

   $UserDesc[$i] = $ARGV[$j];
   $Perms[$i]    = $ARGV[$j+1];
}

# call RSCT command chrmcacls to modify RSCT ACLs file

my $TRUE = 1;
my $FALSE = 0;
my @acl_stanza_lines = ();
my $UPDACL_error = $FALSE;
my $rc = 0;
my $flag = "";

# create the ACL stanza lines that are needed
# first for the Peer Domain class
$acl_stanza_lines[0] = "$ClassName    // $ClassName class - added by addrmcacl\n";

# add the user descriptions and permissions
$j=1;
for ($i=0,$j=1; $i<=$#UserDesc; $i++, $j++) {
   $acl_stanza_lines[$j] = "    $UserDesc[$i]  *   $Perms[$i]  // added by addrmcacl\n";
}

# figure out whether to use chrmcacl -d or use no flags
if ($#acl_stanza_lines > 0) { $flag = "-d";}

# call chrmcacl using a pipe to pass the ACL info
if ( open(UPDACL,"|/usr/sbin/rsct/install/bin/chrmcacl $flag ") ) {

   if ( print UPDACL @acl_stanza_lines ) {

      if (!( close(UPDACL) )) {

         # close failed
         $UPDACL_error = $TRUE;
      }
   }

   # print failed
   else { $UPDACL_error = $TRUE; }
}

# open failed
else { $UPDACL_error = $TRUE; }

# check the return code in case of error from chrmacl
$rc = $?;

# if anything failed, print error message and exit
if ( ($rc != 0) || $UPDACL_error ) {
   print STDERR "addrmcacl encountered an error trying to modify the RSCT ACLs file.\n";
   exit(1);
}

