#!/usr/bin/perl

$YPCAT = '/usr/bin/ypcat';
$NIUTIL = '/usr/bin/niutil';
$LISTUSERS = "$NIUTIL -list . /groups";
$DELETEUSER = "$NIUTIL -destroy / /groups/%s";
$FINDMORTALS = '/usr/bin/nigrep mortality . /groups';
$FIND_GROUP = 'nigrep name . /groups | awk \'{ print $4}\'';

open(NETINFO_MAP,"$FIND_GROUP|") || die "Cannot open nigrep \n"; 

$groupHash =&readGroup(NETINFO_MAP);

close(NETINFO_MAP);


# foreach $_ (keys (%groupHash))

# {print "ele:  $_ \n\n\n";}

$/ = "\n";
open(GROUP_MAP,"$YPCAT group|") || die "Cannot ypcat group\n";
 while (<GROUP_MAP>) 
	{
		print "nome $_\n\n\n";
		$ris="$_";
		@lista=split(':', $ris );

		$name=$lista[0];
		$passwd=$lista[1];
		$gid=$lista[2];
		$users=$lista[3];
	        @utenti=split(',', $users );	
         if (!  defined($groupHash{$name}))
	{
		print " niutil -create / /groups/$name\n";
	#	print " niutil -createprop / /users/$name gid $gid\n";
	#	print " niutil -appendprop / /groups/$name users (@utenti)\n";
	#	print " niutil -createprop / /users/$user passwd $passwd \n";
	#	print " niutil -createprop / /users/$user mortality 1\n";
		
		system " niutil -create / /groups/$name ";
		system " niutil -createprop / /groups/$name  gid $gid";
		system " niutil -createprop / /groups/$name  users  @utenti";
		system " niutil -createprop / /groups/$name  passwd $passwd";
		system " niutil -createprop / /groups/$name  mortality 1";
    		sleep 4; 
   } #endif
 else { print "$name e' di sistema\n";}
	} #endwhile

close(GROUP_MAP);

# Now I have all entries in NI database. I want to delete some.

exit;

sub readGroup () {
  my $fh = shift;
  my %res = {};
  my @tmp;
  while ( <$fh> ) {
    chomp;
    print "readGroup  $_\n";
#     print "\n RKA \n";
    $groupHash{$_} = 1;
    #$res->{$_} = 1;
  }
  return %res;
}


