#! /usr/bin/perl


###############################################################################
#
# Add the 'gnats' system into cron
#

sub addcron_die {
	die "Cannot add 'gnats' system into cron: $!\n";
}

#
# Make sure gnats has cron & at access
#
if (open(ALLOW,"</var/spool/cron/cron.allow")) {
	if (grep(/gnats/,<ALLOW>) == 0) {
		close(ALLOW);
		open(ALLOW,">>/var/spool/cron/cron.allow") || addcron_die;
		print ALLOW "gnats\n";
		print "- 'gnats' has been added to the cron.allow file\n";
	}
	close(ALLOW);
}
if (open(ALLOW,"</var/spool/cron/at.allow")) {
	if (grep(/gnats/,<ALLOW>) == 0) {
		close(ALLOW);
		open(ALLOW,">>/var/spool/cron/at.allow") || addcron_die;
		print ALLOW "gnats\n";
		print "- 'gnats' has been added to the at.allow file\n";
	}
	close(ALLOW);
}

#
# Add the 'gnats' crontab file
#
open(CRON,"| crontab -u gnats -") || addcron_die;
#print CRON "# dpkg:  The above lines are bogus -- ignore them\n";
print CRON "0,10,20,30,40,50 * * * * /usr/lib/gnats/queue-pr --run\n";
close(CRON);
