#!/usr/local/bin/perl
# index.cgi
# Display a list of known filesystems, and indicate which are currently mounted

require './mount-lib.pl';
&header($text{'index_title'}, "", undef, 1, 1, 0,
	&help_search_link("mount fstab vfstab", "man"));
print "<hr>\n";

@mounted = &list_mounted();
&show_button();
print "<table width=100% border>\n";
print "<tr $tb> <td><b>$text{'index_dir'}</b></td>\n",
      "<td><b>$text{'index_type'}</b></td>\n",
      "<td><b>$text{'index_dev'}</b></td>\n",
      "<td><b>$text{'index_use'}</b></td>\n",
      "<td><b>$text{'index_perm'}</b></td> </tr>\n";

# List filesystems from fstab
$yes = $text{'yes'};
$no = "<font color=#ff0000>$text{'no'}</a>";
foreach (&list_fstypes()) { $can_edit{$_}++; }
$i = 0;
foreach (&list_mounts()) {
	@minfo = @{$_};
	$p = &simplify_path($minfo[0], $minfo[2]);
	if ($ignore{$minfo[2]}) { $i++; next; }

	@mmodes = &mount_modes($minfo[2], $minfo[0], $minfo[1]);
	print "<tr $cb>\n";
	if ($can_edit{$minfo[2]} && !$mmodes[4] && !$access{'only'} &&
	    &can_edit_fs(@minfo)) {
		print "<td><a href=\"edit_mount.cgi?index=$i\">",
		      "$p</a></td>\n",
		}
	else {
		print "<td>$p</td>\n";
		}
	local $fsn = &fstype_name($minfo[2]);
	$fsn .= " ($minfo[2])" if (uc($fsn) ne uc($minfo[2]));
	print "<td>",$minfo[2] eq "*" ? $text{'index_auto'} : $fsn,"</td>\n";
	print "<td>",&device_name($minfo[1]),"</td>\n";
	$p = $minfo[2] eq "swap" ? "*" : $minfo[0];
	if (&can_edit_fs(@minfo)) {
		printf "<td>%s</td>\n",
			&get_mounted($p, $minfo[1])>=0 || $mmodes[1]==0 ?
			"<a href='unmount.cgi?index=$i'>$yes</a>" :
			"<a href='mount.cgi?index=$i'>$no</a>";
		}
	else {
		printf "<td>%s</td>\n",
			&get_mounted($p, $minfo[1])>=0 || $mmodes[1]==0 ?
			$yes : $no;
		}
	print "<td>$yes</td>\n";
	print "</tr>\n";

	$already{$minfo[1],$minfo[0]}++;
	$i++;
	}

# List filesystems from mnttab
$i = 0;
foreach (@mounted) {
	@minfo = @{$_};
	if ($already{$minfo[1],$minfo[0]} || $ignore{$minfo[2]}) { $i++; next; }
	$p = &simplify_path($minfo[0], $minfo[2]);

	@mmodes = &mount_modes($minfo[2], $minfo[0], $minfo[1]);
	print "<tr $cb>\n";
	if ($can_edit{$minfo[2]} && !$mmodes[4] && !$access{'only'} &&
	    &can_edit_fs(@minfo)) {
		print "<td><a href=\"edit_mount.cgi?temp=1&index=$i\">",
		      "$p</a></td>\n";
		}
	else {
		print "<td>$p</td>\n";
		}
	local $fsn = &fstype_name($minfo[2]);
	$fsn .= " ($minfo[2])" if (uc($fsn) ne uc($minfo[2]));
	print "<td>$fsn</td>\n";
	print "<td>",&device_name($minfo[1]),"</td>\n";
	print "<td>$yes</td>\n";
	print "<td>",$mmodes[1] ? $no : $yes,"</td>\n";

	$i++;
	}

print "</table>\n";
&show_button();

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

sub simplify_path
{
if ($_[1] eq "swap") {
	return "<i>$text{'index_swap'}</i>";
	}
elsif (length($_[0]) > 20) {
	return &html_escape("... ".substr($_[0], length($_[0])-20));
	}
else {
	return &html_escape($_[0]);
	}
}

sub show_button
{
return if (!$access{'create'} || $access{'only'});
local %donefs;
print "<form action=\"edit_mount.cgi\">\n";
print "<input type=submit value=\"$text{'index_add'}\"> $text{'index_type'}:\n";
print "<select name=type>\n";
foreach (sort { &fstype_name($a) cmp &fstype_name($b) } &list_fstypes()) {
	$nm = &fstype_name($_);
	if (!$donefs{$nm}++) {
		print "<option value=\"$_\">$nm ($_)\n";
		}
	}
print "</select></form>\n";
}

