#!/usr/bin/perl

use strict;

our $file = "/etc/sudoers";

sub main() {

	# Search for plugins in sudo file
	open( FILE, "$file" );
	my $found = 0;
        my $opmondb = 0;
        my $postfix_queue = 0;
	while(<FILE>) {
		$found = 1 if (/check_opmon_(table_size|deleted_hosts|perfparse)\.pl/);
                $opmondb = 1 if (/check_opmon_opmondb_size\.pl/);
                $postfix_queue = 1 if (/postqueue/);
	}
	close( FILE );

	# Add some entries if necessary
	if (not $found) {
		open( FILE, ">>$file" );
		print FILE "opuser  ALL=(ALL)       NOPASSWD: /usr/local/opmon/libexec/opservices/check_opmon_table_size.pl\n";
		print FILE "opuser  ALL=(ALL)       NOPASSWD: /usr/local/opmon/libexec/opservices/check_opmon_deleted_hosts.pl\n";
		print FILE "opuser  ALL=(ALL)       NOPASSWD: /usr/local/opmon/libexec/opservices/check_opmon_perfparse.pl\n";
		close( FILE );
	}
        if (not $opmondb) {
                open( FILE, ">>$file" );
                print FILE "opuser  ALL=(ALL)       NOPASSWD: /usr/local/opmon/libexec/opservices/check_opmon_opmondb_size.pl\n";
                close( FILE );
        }
        if (not $postfix_queue) {
                open( FILE, ">>$file" );
                print FILE "opuser  ALL=(ALL)       NOPASSWD: /usr/sbin/postqueue\n";
                close( FILE );
        }


}

&main();
