
use strict;
use diagnostics;

my $gf = '/etc/group';
my $us = '/tmp/SxG';

# Pass me a group to create - unless I am NOT root...
my $group = @ARGV ? $ARGV[0] : 'unknown';

my $sorted = `cut -f3 -d: $gf > $us && sort -n $us`;
my $erc    = '';
my $test   = '';
my $target = 100;

foreach $test ( (split(/\n/, $sorted)) ) {

    next if ($test < 100);
    next if (++$target == $test);

    print "\n\nNext likely number is: $target\n\n";
    print "You are not ROOT ... no group $group will be created ... \n" if $<;

    # Else we may be root
    print "Creating $group ... \n" unless $<;
    $erc = system("groupadd -g $target $group") / 256 unless $<;
    print "\n\nError Cannot add group: $group $! $erc" if $erc;

    last;
}

$erc = system("rm $us") / 256;
print "\n\nError Cannot remove temporary file $us $! $erc" if $erc;
