#!/usr/local/bin/perl
# edit_group.cgi
# Display a form for editing or creating a group

require './user-lib.pl';
&ReadParse();
$n = $in{'num'};
%access = &get_module_acl();
if ($n eq "") {
	$access{'gcreate'}==1 || &error($text{'gedit_ecreate'});
	&header($text{'gedit_title2'}, "", "create_group");
	}
else {
	@glist = &list_groups();
	%group = %{$glist[$n]};
	&can_edit_group(\%access, \%group) ||
		&error($text{'gedit_eedit'});
	&header($text{'gedit_title'}, "", "edit_group");
	}

print "<hr>\n";
print "<form action=\"save_group.cgi\" method=post>\n";
if ($n ne "") {
	print "<input type=hidden name=num value=\"$n\">\n";
	}
print "<table border width=100%>\n";
print "<tr $tb> <td><b>$text{'gedit_details'}</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

print "<tr> <td valign=top>",&hlink("<b>$text{'gedit_group'}</b>","ggroup"),
      "</td>\n";
if ($n eq "") {
	print "<td valign=top><input name=group size=10></td>\n";
	}
else {
	print "<td valign=top><tt>$group{'group'}</tt></td>\n";
	}

print "<td valign=top>",&hlink("<b>$text{'gedit_gid'}</b>","ggid"),"</td>\n";
if ($n eq "") {
	# Find the first free GID above the base
	&my_setgrent();
	while(@tmp = &my_getgrent()) {
		$used{$tmp[2]}++;
		}
	&my_endgrent();
	$newgid = int($config{'base_gid'} > $access{'lowgid'} ?
			$config{'base_gid'} : $access{'lowgid'});
	while($used{$newgid}) {
		$newgid++;
		}
	print "<td valign=top><input name=gid size=10 value='$newgid'></td>\n";
	}
else {
	print "<td valign=top><input name=gid size=10 ",
	      "value=\"$group{'gid'}\"></td>\n";
	}
print "</tr>\n";

print "<tr> <td valign=top>",&hlink("<b>$text{'pass'}</b>","gpasswd"),"</td>\n";
printf "<td valign=top><input type=radio name=passmode value=0 %s> $text{'none2'}<br>\n",
	$group{'pass'} eq "" ? "checked" : "";
printf "<input type=radio name=passmode value=1 %s> $text{'encrypted'}\n",
	$group{'pass'} eq "" ? "" : "checked";
print "<input name=encpass size=13 value=\"$group{'pass'}\"><br>\n";
print "<input type=radio name=passmode value=2 %s> $text{'clear'}\n";
print "<input name=pass size=15></td>\n";

print "<td valign=top>",&hlink("<b>$text{'gedit_members'}</b>","gmembers"),
      "</td>\n";
print "<td><table><tr><td><textarea wrap=auto name=members rows=5 cols=10>",
	join("\n", split(/,/ , $group{'members'})),"</textarea></td>\n";
print "<td valign=top><input type=button onClick='ifield = document.forms[0].members; chooser = window.open(\"my_user_chooser.cgi?multi=1&user=\"+escape(ifield.value), \"chooser\", \"toolbar=no,menubar=no,scrollbars=yes,width=500,height=200\"); chooser.ifield = ifield; window.ifield = ifield' value=\"...\"></td></tr></table></td> </tr>\n";
print "</table></td></tr></table><p>\n";

if ($n ne "") {
	print "<table border width=100%>\n";
	print "<tr $tb> <td><b>$text{'onsave'}</b></td> </tr>\n";
	print "<tr $cb> <td><table>\n";

	if ($access{'chgid'} == 1) {
		print "<tr> <td>",&hlink($text{'chgid'},"gchgid"),"</td>\n";
		print "<td><input type=radio name=chgid value=0 checked> $text{'no'}</td>\n";
		print "<td><input type=radio name=chgid value=1> $text{'gedit_homedirs'}</td>\n";
		print "<td><input type=radio name=chgid value=2> $text{'gedit_allfiles'}</td> </tr>\n";
		}

	if ($access{'mothers'} == 1) {
		print "<tr> <td>",&hlink($text{'gedit_mothers'},"others"),"</td>\n";
		print "<td><input type=radio name=others value=1 checked> $text{'yes'}</td>\n";
		print "<td><input type=radio name=others value=0> $text{'no'}</td> </tr>\n";
		}

	print "</table></td> </tr></table><p>\n";
	}
else {
	print "<table border width=100%>\n";
	print "<tr $tb> <td><b>$text{'uedit_oncreate'}</b></td> </tr>\n";
	print "<tr $cb> <td><table>\n";

	if ($access{'cothers'} == 1) {
		print "<tr> <td>",&hlink($text{'gedit_cothers'},"others"),"</td>\n";
		print "<td><input type=radio name=others value=1 checked> $text{'yes'}</td>\n";
		print "<td><input type=radio name=others value=0> $text{'no'}</td> </tr>\n";
		}

	print "</table></td> </tr></table><p>\n";

	}

if ($n ne "") {
	print "<table width=100%>\n";
	print "<tr> <td><input type=submit value=\"$text{'save'}\"></td>\n";
	print "</form><form action=\"delete_group.cgi\">\n";
	print "<input type=hidden name=num value=\"$n\">\n";
	print "<td align=right><input type=submit value=\"$text{'delete'}\"></td> </tr>\n";
	print "</form></table><p>\n";
	}
else {
	print "<input type=submit value=\"$text{'create'}\"></form><p>\n";
	}

print "<hr>\n";
&footer("", $text{'index_return'});

