#!/usr/bin/perl
#
# $Revision: 1.4 $ $Date: 2012-07-26 17:11:47-04 $
# $Source: /home/vogelke/bin/RCS/grplen,v $
# $Host: sys7.com $
# $UUID: eb4998ae-8988-3ba6-8149-bf20413cf72f $
#
#<grplen: print length of specific group lines
# usage: grplen g1 g2 g3        OR     grplen g1,g2,g3

my $name;
my $fh;
my @args;

# Allow for groups separated by commas.

die "usage: $0 group [group ...]\n" unless @ARGV;
foreach (@ARGV) {
    s/,/ /g;
    foreach (split) { push(@args, $_); }
}

# Compile the regex once instead of going through it repeatedly:
#      ^(pat1|pat2|pat3)\d*:
# The \d* handles groups that get too long and are split up
# over multiple lines.

my $groups = '^(' . pop(@args);
foreach (@args) {
    $groups .= '|' . $_;
}
$groups .= ')\d*:';

my $regex;
eval { $regex = qr/$groups/ };
die "$@\n" if $@;

# Now check the file.

open ($fh, "< /etc/group") or die "group: $!\n";
while (<$fh>) {
    $name = (split(/:/))[0];
    print("$name\t", length(), "\n") if /$regex/;
}
close($fh);

exit (0);
