#!/usr/local/bin/perl
# save_dump.cgi
# Save the details of a scheduled backup

require './fsdump-lib.pl';
&foreign_require("cron", "cron-lib.pl");
&ReadParse();
&error_setup($text{'save_err'});

if ($in{'id'}) {
	$dump = &get_dump($in{'id'});
	$access{'edit'} && &can_edit_dir($dump) ||
		&error($text{'dump_ecannot2'});
	@jobs = &foreign_call("cron", "list_cron_jobs");
	foreach $j (@jobs) {
		$job = $j if ($j->{'command'} eq "$cron_cmd $dump->{'id'}");
		}
	$oldenabled = $dump->{'enabled'};
	}
else {
	$access{'edit'} || &error($text{'dump_ecannot1'});
	}

if ($in{'delete'}) {
	# Just delete this dump
	&delete_dump($dump);
	if ($job) {
		&lock_file($job->{'file'});
		&foreign_call("cron", "delete_cron_job", $job);
		&unlock_file($job->{'file'});
		}
	&webmin_log("delete", undef, $dump->{'id'}, $dump);
	&redirect("");
	}
elsif ($in{'restore'}) {
	# Redirect to restore form
	&redirect("restore_form.cgi?fs=$dump->{'fs'}&id=$in{'id'}");
	}
else {
	# Validate and store inputs
	if ($multiple_directory_support || $in{'fs'} eq 'tar') {
		foreach $d (split(/\s+/, $in{'dir'})) {
			-d $d || &error($text{'save_edir'});
			if ($in{'fs'} ne 'tar') {
				$fs = &directory_filesystem($d);
				$fs eq $in{'fs'} || &error($text{'save_efs'});
				}
			&can_edit_dir($d) || &error($text{'dump_ecannot3'});
			}
		$in{'dir'} || &error($text{'save_edir'});
		}
	else {
		-d $in{'dir'} || &error($text{'save_edir'});
		if ($in{'fs'} ne 'tar') {
			$fs = &directory_filesystem($in{'dir'});
			$fs eq $in{'fs'} || &error($text{'save_efs'});
			}
		&can_edit_dir($in{'dir'}) || &error($text{'dump_ecannot3'});
		}
	$dump->{'dir'} = $in{'dir'};
	$dump->{'fs'} = $in{'fs'};
	$dump->{'email'} = $in{'email'};
	$dump->{'subject'} = $in{'subject_def'} ? undef : $in{'subject'};
	$dump->{'extra'} = $in{'extra'};
	$dump->{'before'} = $in{'before'};
	$dump->{'after'} = $in{'after'};
	&parse_dump($dump);
	$dump->{'enabled'} = $in{'enabled'};
	&foreign_call("cron", "parse_times_input", $dump, \%in);

	# Create or update the dump and cron job
	&save_dump($dump);
	local $oldjob = $job;
	if ($dump->{'enabled'}) {
		# Create cron job and script
		$job->{'user'} = 'root';
		$job->{'active'} = 1;
		$job->{'special'} = $dump->{'special'};
		$job->{'mins'} = $dump->{'mins'};
		$job->{'hours'} = $dump->{'hours'};
		$job->{'days'} = $dump->{'days'};
		$job->{'months'} = $dump->{'months'};
		$job->{'weekdays'} = $dump->{'weekdays'};
		$job->{'command'} = "$cron_cmd $dump->{'id'}";
		&lock_file($cron_cmd);
		&cron::create_wrapper($cron_cmd, $module_name, "backup.pl");
		&unlock_file($cron_cmd);
		}
	&lock_file(&cron::cron_file($job)) if ($job);
	if ($dump->{'enabled'} && !$oldjob) {
		# Create the cron job
		&foreign_call("cron", "create_cron_job", $job); 
		}
	elsif ($dump->{'enabled'} && $oldjob) {
		# Update the cron job
		&foreign_call("cron", "change_cron_job", $job); 
		}
	elsif (!$dump->{'enabled'} && $oldjob) {
		# Delete the cron job
		&foreign_call("cron", "delete_cron_job", $job);
		}
	&unlock_file(&cron::cron_file($job)) if ($job);

	&webmin_log($in{'id'} ? "modify" : "create", undef, $dump->{'id'}, $dump);
	if ($in{'savenow'}) {
		&redirect("backup.cgi?id=$dump->{'id'}");
		}
	else {
		&redirect("");
		}
	}

