#!/usr/bin/perl
#
#      Author       : Gerald (Jerry) Carter
#      E-mail       : jerry@eng.auburn.edu
#      Filename     : nt2group
#      Date Created : January 21, 1999
#      Last Update  :
#
#      Simple perl script to accept the input from the 
#      'net group /domain > group.txt' on an NT domain member
#      and place the users in an /etc/group formatted file
#      in the current directory
#
#      The generated file may be catenated to /etc/group at
#      your descretion.  I make no claims about the script.
#      **Use it at your own risk**  No warrenty expressed or 
#      implied.  
#
#########################################################################
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
#########################################################################

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

   if ( "$currentarg" eq "-i" ) {
      $no_input = 0;
      $currentarg = shift;
      if (defined($currentarg)) {
         $input_file = $currentarg;
         open ( GROUP_LIST, "$input_file" ) || die $!;
      }
   }
   else {
      &print_usage;
      exit (-1);
   }
}

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

# get the starting uid
print "Enter the gid to start with : ";
$start_gid = <STDIN>;
$start_gid = int ( $start_gid );

if ( $start_uid eq 0 ) {
   printf STDERR "Cannot start with gid 0!\nProgram exiting...\n";
   exit (-1);
}

$current_gid = $start_gid;

# open the output file
open ( GROUP, "> group.new" ) || die $!;
open ( GROUPMAP, "> group.map" ) || die $!;

# loop through the input file 
while ( $string = <GROUP_LIST> ) {

   chop ( $string );

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

   # break up the input
   if ( "$string" ne "" ) {
      # printf "{$string}\n";
      @groups = split (/\*/, $string );

      foreach $group ( @groups ) {
         if ( "$group" ne "" ) {

            ($name) = getgrgid ( $current_gid );
            while ( "$name" ne "" ) {
               $current_gid++;
               ($name) = getgrgid ( $current_gid );
            }
   
            # clean up the group name by converting to lower case and 
            # removing the trailing spaces
            $group =~ s/./\l$&/g;
            $group =~ s/\s+$//;

            if ( length($group) > 8 ) {
               $got_group = 0;
               $new_group = "";
               while ( $got_group == 0 ) {
                  print "Please enter a group name for [$group] of 8 characters of less: ";
                  $new_group = <STDIN>;
                  chop ($new_group);
                  ($new_groupname) = getgrnam ($new_group);
                  if ("$new_groupname" ne "") {
                     print "[$new_groupname] exists.  Do you want to enter another name? (y/n) ";
                     $result = <STDIN>;
                     $result = substr ($result, 0, 1);
                     $result =~ s/./\l$&/g;
                     if ( "$result" eq "n" ) {
                        $got_group = 1;
                     }
                  }
                  else {
                     $got_group = 1;
                  }
               }
               if ( ($got_group == 1) && ("$new_group" ne "") ) {
                  print GROUPMAP "$group:$new_group\n";
                  $group = $new_group;
               }
            }
            if ( (length($group) <= 8) && (length($group) != 0) ) {
               print GROUP "$group:*:$current_gid:\n";
            }
            $current_gid++;
         }
      }
   }
}

printf STDERR "The new accounts have been created in group.new.  They\n";
printf STDERR "have not been automatically added to /etc/group\n\n";
printf STDERR "To create the accounts, you must execute\n\n";
printf STDERR "    cat group.new >> /etc/group\n\n";


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

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

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

   if ( $input =~ '\\\\' ) {
      $input = '';
   }
   elsif ( $input =~ "command completed" ) {
      $input = '';
   }
   elsif ( $input =~ '---------' ) {
      $input = '';
   }
   elsif ( $input =~ 'The request will be processed at a domain controller' ) {
      $input = '';
   }

   $input;
}

sub print_usage {
   printf STDERR "Usage : nt2group -i <inputfile>\n";
   printf STDERR "	<inputfile> 	- output from 'net group /domain' command\n";
}
