package Modules::Cronjobs;

########## Confixx(R) 3.0 Professional ############
####### Copyright SWsoft, Inc. 2004-2005 ##########
#### http://www.sw-soft.com - info@sw-soft.com ####

BEGIN {
	use Exporter ();
	@ISA = qw(Exporter);
	@EXPORT_OK = qw(&set_crontab &del_crontab &get_crontab);
}

use strict "vars";

use Modules::ErrDbg qw(&soft_error &hard_error);

## Generate 
sub set_crontab {
	my ($client, $file) = @_;
	
	# write content to temporary file
	use Modules::System qw(&get_temp_file);
	my ($fh, $name) = &get_temp_file();
	&hard_error("Cronjobs module is unable to create a temporary file") unless($fh);
	print $fh "$file\n";
	$fh->close;
	
	# set file to crontab
	use Modules::System qw(&exec_sys_util);
	my $res = &exec_sys_util('crontab', ['-u', $client, $name]);

	# remove temporary file and process errors
	unlink($name) or &soft_error("Cronjobs module is unable to remove temporary file '$name'");
		
	return $res;
}

sub del_crontab {
	my ($client) = @_;
	use Modules::System qw(&exec_sys_util);
	return &exec_sys_util('crontab', ['-u', $client, '-r']);
}

## get crontab
sub get_crontab {
	my ($client) = @_;
	
	use Modules::System qw(&list_sys_util &get_exec_res);
	my $res = &list_sys_util('crontab', ['-u', $client, '-l']);
	my ($ret, $stdout) = &get_exec_res;
	return $res ? $stdout : '';
}

## get descriptor for temporary file
return 1;

END {}
