#!/usr/local/bin/perl
# save_part.cgi
# Save changes to an existing or new partition

require './fdisk-lib.pl';
&error_setup($text{'save_err'});
&ReadParse();

@dlist = &list_disks_partitions();
$dinfo = $dlist[$in{'disk'}];
&can_edit_disk($dinfo->{'device'}) ||
	&error($text{'save_ecannot'});
@plist = @{$dinfo->{'parts'}};
if ($in{'delete'} && $in{'confirm'}) {
	# deleting a partition
	$pinfo = $plist[$in{'part'}];
	&delete_partition($dinfo->{'device'}, $pinfo->{'number'});
	&webmin_log("delete", "part", $dinfo->{'device'}, \%in);
	&redirect("");
	}
elsif ($in{'delete'}) {
	# Ask the user if he really wants to delete the partition
	&header($text{'delete_title'}, "");
	print "<hr>\n";

	$pinfo = $plist[$in{'part'}];
	$dname = &mount::device_name($pinfo->{'device'});
	print "<center><form action=save_part.cgi>\n";
	print "<input type=hidden name=disk value='$in{'disk'}'>\n";
	print "<input type=hidden name=part value='$in{'part'}'>\n";
	print "<input type=hidden name=delete value=1>\n";
	print "<b>",&text('delete_rusure', $dname,
			  "<tt>$pinfo->{'device'}</tt>"),"</b><p>\n";
	print "<input type=submit name=confirm value='$text{'delete_ok'}'>\n";
	print "</form></center>\n";

	print "<hr>\n";
	&footer("", $text{'index_return'});
	}
elsif (!$in{'new'}) {
	# changing existing partition
	$pinfo = $plist[$in{'part'}];
	&change_type($dinfo->{'device'}, $pinfo->{'number'}, $in{'type'});
	if (defined($in{'label'}) && $in{'type'} eq '83') {
		&set_label($pinfo->{'device'}, $in{'label'});
		}
	&webmin_log("modify", "part", $dinfo->{'device'}, \%in);
	&redirect("");
	}
else {
	# Adding new partition
	$in{start} =~ /^\d+$/ ||
		&error(&text('save_estart', $in{'start'}));
	$in{end} =~ /^\d+$/ ||
		&error(&text('save_eend', $in{'end'}));
	$in{start} >= $in{'min'} ||
		&error(&text('save_emin', $in{'min'}));
	$in{end} <= $in{'max'} ||
		&error(&text('save_emax', $in{'max'}));
	$in{start} < $in{end} ||
		&error($text{'save_eminmax'});

	# Check for partition overlap..
	foreach $pinfo (@plist) {
		if (($in{'start'} >= $pinfo->{'start'} &&
		     $in{'start'} <= $pinfo->{'end'} ||
		     $in{'end'} >= $pinfo->{'start'} &&
		     $in{'end'} <= $pinfo->{'end'}) &&
		     !($in{'new'}==2 && $pinfo->{'extended'})) {
			&error(&text('save_eoverlap', $pinfo->{'number'},
				     $pinfo->{'start'}, $pinfo->{'end'}));
			}
		}
	if ($in{'new'} == 3) {
		&create_extended($dinfo->{'device'}, $in{'newpart'},
				$in{'start'}, $in{'end'});
		&webmin_log("create", "part", $dinfo->{'device'}, \%in);
		}
	else {
		&create_partition($dinfo->{'device'}, $in{'newpart'},
				  $in{'start'}, $in{'end'}, $in{'type'});
		if ($in{'label'} && $in{'type'} eq '83') {
			local $dev = $dinfo->{'prefix'}.$in{'newpart'};
			&set_label($dev, $in{'label'});
			}
		&webmin_log("create", "part", $dinfo->{'device'}, \%in);
		}
	if (&need_reboot($dinfo)) { &ask_reboot($dinfo); }
	else { &redirect(""); }
	}

# ask_reboot(disk)
# Display a form asking for a reboot
sub ask_reboot
{
&header($text{'reboot_title'}, "");
print "<hr>\n";
local $what = &text('select_device', uc($_[0]->{'type'}),
		    uc(substr($_[0]->{'device'}, -1)));
print "<b>",&text('reboot_why', $what),"</b> <p>\n";
print "<form action=reboot.cgi>\n";
print "<center><input type=submit value=\"$text{'reboot_ok'}\"></center>\n";
print "</form>\n";

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

