#!/usr/local/bin/perl
# edit_export.cgi
# Display a form for editing or creating an export

require './bsdexports-lib.pl';
&ReadParse();

if (defined($in{'index'})) {
	&header("Edit Export", "");
	@exp = &list_exports();
	%exp = %{$exp[$in{'index'}]};
	}
else {
	&header("Create Export", "");
	}
print "<hr>\n";

print "<form action=save_export.cgi>\n";
if (%exp) { print "<input type=hidden name=index value=$in{'index'}>\n"; }
print "<table border width=100%>\n";
print "<tr $tb> <td><b>Export options</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

print "<tr> <td valign=top rowspan=3><b>Directories to export</b></td>\n";
print "<td rowspan=3><textarea name=dirs rows=4 cols=30>",
	join("\n", @{$exp{'dirs'}}),"</textarea></td>\n";

print "<td><b>Export subdirectories?</b></td>\n";
printf "<td><input type=radio name=alldirs value=1 %s> Yes\n",
	$exp{'alldirs'} ? "checked" : "";
printf "<input type=radio name=alldirs value=0 %s> No</td> </tr>\n",
	$exp{'alldirs'} ? "" : "checked";

print "<tr> <td><b>Read only?</b></td>\n";
printf "<td><input type=radio name=ro value=1 %s> Yes\n",
	$exp{'ro'} ? "checked" : "";
printf "<input type=radio name=ro value=0 %s> No</td> </tr>\n",
	$exp{'ro'} ? "" : "checked";

print "<tr> <td><b>Use Kerberos?</b></td>\n";
printf "<td><input type=radio name=kerb value=1 %s> Yes\n",
	$exp{'kerb'} ? "checked" : "";
printf "<input type=radio name=kerb value=0 %s> No</td> </tr>\n",
	$exp{'kerb'} ? "" : "checked";

print "</table></td></tr></table><p>\n";
print "<table border width=100%>\n";
print "<tr $tb> <td><b>Remote user mapping</b></td> </tr>\n";
print "<tr $cb> <td><table width=100%>\n";

print "<tr> <td><b>Map root user to</b></td>\n";
printf "<td><input type=radio name=maproot_def value=1 %s> %s</td>\n",
	$exp{'maproot'} ? "" : "checked", "Unprivileged user";
printf "<td><input type=radio name=maproot_def value=0 %s> User/UID\n",
	$exp{'maproot'} ? "checked" : "";
$user = $exp{'maproot'} =~ /^([^:]+)/ ? $1 : "";
print "<input name=maproot size=8 value=\"$user\">&nbsp;&nbsp;\n";
printf "<input type=checkbox name=maprootg_def value=1 %s> And groups/GIDs\n",
	$exp{'maproot'} =~ /:/ ? "checked" : "";
$groups = $exp{'maproot'} =~ /:(.*)$/ ? join(' ', split(/:/, $1)) : "";
print "<input name=maprootg size=30 value=\"$groups\"></td> </tr>\n";

print "<tr> <td><b>Map all users to</b></td>\n";
printf "<td><input type=radio name=mapall_def value=1 %s> No change</td>\n",
	$exp{'mapall'} ? "" : "checked";
printf "<td><input type=radio name=mapall_def value=0 %s> User/UID\n",
	$exp{'mapall'} ? "checked" : "";
$user = $exp{'mapall'} =~ /^([^:]+)/ ? $1 : "";
print "<input name=mapall size=8 value=\"$user\">&nbsp;&nbsp;\n";
printf "<input type=checkbox name=mapallg_def value=1 %s> And groups/GIDs\n",
	$exp{'mapall'} =~ /:/ ? "checked" : "";
$groups = $exp{'mapall'} =~ /:(.*)$/ ? join(' ', split(/:/, $1)) : "";
print "<input name=mapallg size=30 value=\"$groups\"></td> </tr>\n";

print "</table></td></tr></table><p>\n";
print "<table border width=100%>\n";
print "<tr $tb> <td><b>Clients for this export</b></td> </tr>\n";
print "<tr $cb> <td><table>\n";

print "<tr> <td valign=top><b>Clients</b></td> <td>\n";

printf "<input type=radio name=cmode value=0 %s> <b>Hosts / netgroups</b>\n",
	$exp{'mask'} ? "" : "checked";
printf "<input name=hosts size=40 value=\"%s\"><br>\n",
	join(' ', @{$exp{'hosts'}});

printf "<input type=radio name=cmode value=1 %s> <b>Network</b>\n",
	$exp{'mask'} ? "checked" : "";
print "<input name=network size=20 value=\"$exp{'network'}\">\n";
print "&nbsp;<b>Mask</b>\n";
print "<input name=mask size=20 value=\"$exp{'mask'}\"></td> </tr>\n";

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

if (%exp) {
	print "<table width=100%>\n";
	print "<tr> <td><input type=submit value=Save></td>\n";

	print "</form><form action=\"delete_export.cgi\">\n";
	print "<input type=hidden name=index value=\"$exp{'index'}\">\n";
	print "<td align=right><input type=submit value=Delete></td> </tr>\n";
	print "</form></table><p>\n";
	}
else { print "<input type=submit value=Create></form><p>\n"; }

print "<hr>\n";
&footer("", "exports list");

