#!/usr/local/bin/perl
# search_group.cgi
# Search the group file, and display a list of results

require './user-lib.pl';
&ReadParse();
@glist = &list_groups();
$m = $in{'match'};
$w = $in{'what'};
for($i=0; $i<@glist; $i++) {
	$g = $glist[$i];
	$f = $g->{$in{'field'}};
	if ($m == 0 && $f eq $w ||
	    $m == 1 && eval { $f =~ /$w/i } ||
	    $m == 4 && index($f, $w) >= 0 ||
	    $m == 2 && $f ne $w ||
	    $m == 3 && eval { $f !~ /$w/i } ||
	    $m == 5 && index($f, $w) < 0) {
		push(@match, $g);
		}
	}
if (@match == 1) {
	&redirect("edit_group.cgi?num=".$match[0]->{'num'});
	}
else {
	&header($text{'search_title'}, "");
	print "<hr>\n";
	if (@match == 0) {
		print "<p><b>$text{'search_gnotfound'}</b>. <p>\n";
		}
	else {
		print "<table border width=100%>\n";
		print "<tr $tb> <td><b>$text{'gedit_group'}</b></td>\n";
		print "<td><b>$text{'gedit_gid'}</b></td>\n";
		print "<td><b>$text{'gedit_members'}</b></td> </tr>\n";
		foreach $m (@match) {
			local $members = join(" ", split(/,/, $m->{'members'}));
			print "<tr $cb>\n";
			print "<td><a href=\"edit_group.cgi?num=$m->{'num'}\">",
			      &html_escape($m->{'group'}),"</a></td>\n";
			print "<td>$m->{'gid'}</td>\n";
			print "<td>",&html_escape($members),
			      "&nbsp;</td>\n";
			print "</tr>\n";
			}
		print "</table><p>\n";
		}
	print "<hr>\n";
	&footer("", $text{'index_return'});
	}

