#!/usr/bin/perl
#
#      Author       : Gerald (Jerry) Carter
#      E-mail       : jerry@eng.auburn.edu
#      Filename     : add2group
#      Date Created : January 21, 1999
#      Last Update  :
#

$no_input = 1;
$group_map_file = "";
$user_map_file = "";
$input_file = "";
while ( 1 ) {
   $currentarg = shift;
   last if (!defined($currentarg));

   if ( "$currentarg" eq "-g" ) {
      $no_input = 0;
      $currentarg = shift;
      if (defined($currentarg)) {
         $group_map_file = $currentarg;
         open (GROUP_MAP, "$group_map_file") || die $!;
      }
   }
   elsif ( "$currentarg" eq "-i" ) {
      $no_input = 0;
      $currentarg = shift;
      if (defined($currentarg)) {
         $input_file = $currentarg;
         open (GROUP_MEMBERS, "$input_file" ) || die $!;
      }
   }
   elsif ( "$currentarg" eq "-u" ) {
      $no_input = 0;
      $currentarg = shift;
      if (defined($currentarg)) {
         $user_map_file = $currentarg;
         open (TEST, "$user_map_file" ) || die $!;
         close (TEST);
      }
   }
   else {
      &print_usage;
      exit (-1);
   }
}

if ($no_input || !"$group_map_file" || !"$user_map_file" || !"$input_file") {
   &print_usage();
   exit -1;
}


# get the group name
$group_entry = <GROUP_MEMBERS>;
$group_entry =~ s/\ +/_/;
$group_entry =~ s/\ +/:/;
($tmp1,$groupname) = split (/:/, $group_entry );
chop ($groupname);
$groupname =~ s/./\l$&/g;
$groupname =~ s/\s+$//;

# get the unix name from the map
$done = 0;
while ( ($done != 1)  && ($grstring = <GROUP_MAP>) ) {
   chop ($grstring);
   ($ntname, $mapname) = split (/:/, $grstring); 
   $ntname =~ s/./\l$&/g;
   if ( "$ntname" eq "$groupname" ) {
      $done = 1;
   }
}
close ( GROUP_MAP );

if (! $done ) {
   print "Unable to locate \[$groupname]\ in $group_map_file.  Exiting...\n";
   exit -1;
}

# open files
open ( GROUP, "/etc/group" ) || die $!;

# iterate up the to the groupname we want.
$done = 0;
while ( ($done != 1) && ($grpent = <GROUP>) ) {
   chop ($grpent);
   ( $gname, $gpass, $gid, $gmembers ) = split (/:/, $grpent);
   if ( "$gname" eq "$mapname" ) {
      $done = 1;
   }
   else {
      print "$grpent\n";
   }
}
if ( $done != 1 ) {
   printf STRDERR "Unable to locate group [$ntname]!\n";
   exit -1;
}

# loop through the input file 
# now we have the current group entry
while ( $string = <GROUP_MEMBERS> ) {

   chop ( $string );

   # weed out the command output and keep the list of users
   $string = &checkInput ( $string );

   # break up the input
   if ( "$string" ne "" ) {
      $tmp_list = "";
      while ( "$string" ne "" ) {
         $user1 = substr ($string, 0, 25);
         $user1 =~ s/\s+$//;
         $string = substr ($string, 25, length($string)-25);
         $tmp_list .= $user1 . ":";
      }
      @members= split (/:/, $tmp_list);

      foreach $user ( @members ) {
         
         if ( "$user" ne "" ) {
            $user =~ s/./\l$&/g;
	    ($name, $pass, $uid, $pwgid ) = getpwnam ($user);
            if ( ("$name" ne "") && ($pwgid != $gid)  && !($gmembers =~ $user) ) {
               
               if ( "$gmembers" ne "" ) {
                  $gmembers = $gmembers . ',';
               }
               $gmembers = $gmembers . $user; 
     
            }
            ## commentd out do to a bug spotted by Martin Wheldon
            ## if ( ("$name" eq "") && ("$ARGV[2]" ne "") ) {
               open ( MAPFILE, "$user_map_file") || die $!;
               while ( $map_string = <MAPFILE> ) {
                  chop ($map_string);
                  ($map_name, $ntname) = split (/=/, $map_string);
                  if ("$ntname" eq "$user") {
                     $user = $map_name;
                  }
               }
  	       ($name, $pass, $uid, $pwgid ) = getpwnam ($user);
               if ( ("$name" ne "") && ($pwgid != $gid)  && !($gmembers =~ $user) ) {
                 
                  if ( "$gmembers" ne "" ) {
                     $gmembers = $gmembers . ',';
                 }
                  $gmembers = $gmembers . $user; 
     
               }
            ## }
            ## commentd out do to a bug spotted by Martin Wheldon
         }
      }
   }
}
print "$gname:$gpass:$gid:$gmembers\n";
while ( $grpent = <GROUP> ) {
   print "$grpent";
}

printf STDERR "\n\n";
printf STDERR "The new group file has been printed to standard output.\n";
printf STDERR "The user accounts have not been added to /etc/group.\n";
printf STDERR "You should replace your /etc/group file with the one printed\n";
printf STDERR "to standard output.\n\n";


# close the files 
close ( GROUP_MEMBERS);
close ( GROUP );

# successful completion
exit (0);
#################################################################

sub checkInput {
   local ( $input ) = @_;

   if ( $input =~ '\\\\' ) {
      $input = '';
   }
   elsif ( $input =~ "command completed" ) {
      $input = '';
   }
   elsif ( $input =~ '---------' ) {
      $input = '';
   }
   elsif ( $input =~ 'Comment' ) {
      $input = '';
   }
   elsif ( $input =~ 'Members' ) {
      $input = '';
   }

   $input;
}

sub print_usage {
   printf STDERR "Usage : add2group -i <inputfile> -g <groupmapfile> -u <usermapfile>\n";
   printf STDERR "	<inputfile>	- group membership from 'net group' command\n";
   printf STDERR "	<groupmapfile>	- mapfile created by nt2group\n";
   printf STDERR "	<usermapfile>	- mapfile created by nt2passwd\n";
}
