#!/bin/sh
eval "exec /mit/watchmaker/`/bin/athena/machtype`bin/perl $0 $*"
	if $running_under_some_shell;

	open(PASSWD, "/etc/passwd");
	while (<PASSWD>) {
		chop;
		@passwd = split(/:/);
		$user = $passwd[0];
		open(HESINFO, "hesinfo $user grplist|") 
			|| die "Can't find hesinfo!\n";
		if (!($line = <HESINFO>)) {
			print stderr "User $user has no group information!\n";
			next;
		}
		chop($line);
		@groups = split(/:/,$line);
		while ($#groups > 0) {
			$gname = shift(@groups);
			$glist{$gname, ++$gnum{$gname}} = $user;
			$groupname{shift(@groups)} = $gname;
		}
}

foreach $gid (sort(intcompare keys(groupname))) {
	$gname = $groupname{$gid};
	$num = $gnum{$gname};
	print "$gname:*:$gid:";
	print "$glist{$gname, 1}";
	for ($i=2; $i<=$num; $i++) {
		print ",$glist{$gname, $i}";
	}
	print "\n";
}

sub intcompare {
	$a - $b;
}
