#!/usr/bin/perl -w

use strict;
use Getopt::Long;
use MonitisMonitorManager;
use File::Temp qw/tempfile/;

# look for a M3Templates.pm file
my @m3templates_file_locations = ();
defined($ENV{HOME}) and push @m3templates_file_locations, $ENV{HOME} . "/.m3";
defined($ENV{M3_CONFIG_DIR}) and push @m3templates_file_locations, $ENV{M3_CONFIG_DIR};
push @m3templates_file_locations, "/etc/m3.d";

my $m3templates_file = "";
foreach my $directory (@m3templates_file_locations) {
	my $potential_m3templates_file = "$directory/M3Templates.pm";
	if ( -e $potential_m3templates_file ) {
		print "Using $potential_m3templates_file as M3Templates.pm\n";
		$m3templates_file = $potential_m3templates_file;
		last;
	}
}
if ( $m3templates_file eq "" ) {
	die "Couldn't find a proper M3Templates.pm file";
}
require "$m3templates_file";

# print usage
sub usage() {
	my $command = $0;
	$command =~ s#^.*/##;
	print "$command [--dry-run] [--once] [--mass-load] [--raw RAW_COMMAND] [--test-config] [--help] configuration.xml" . "\n";
	print "Example for raw command: $command --raw \"add_monitor memory RRD_localhost_munin_memory free:free:Bytes:2;active:active:Bytes:2\"" . "\n";
	print "Example for raw command: $command --raw \"update_data memory RRD_localhost_munin_memory free:305594368;active:879394816\"" . "\n";
	exit;
}

# prints version
sub version() {
	print "M3v3 version $MonitisMonitorManager::VERSION\n";
	exit;
}

# return a skeleton XML with just the API and secret keys
sub get_basic_xml() {
	my ($fh, $filename) = tempfile();

	# create a basic XML
	print $fh "<?xml version=\"1.0\"?>" . "\n";
	print $fh "<config>" . "\n";
	print $fh "<apicredentials apikey=\"%API_KEY%\" secretkey=\"%SECRET_KEY%\"/>" . "\n";
	print $fh "</config>" . "\n";

	close $fh;
	return $filename;
}

sub main() {
	my $dry_run = 0;
	my $once = 0;
	my $mass_load = 0;
	my $help = 0;
	my $test_config = 0;
	my $syslog = 0;
	my $raw = "";
	my $version = 0;
	GetOptions(
		"d|dry-run" => \$dry_run,
		"o|once" => \$once,
		"m|mass-load" => \$mass_load,
		"r|raw=s" => \$raw,
		"s|syslog" => \$syslog,
		"t|test-config" => \$test_config,
		"h|help" => \$help,
		"v|version" => \$version);

	# display help
	if ($help) {
		usage();
	}

	# display version
	if ($version) {
		version();
	}

	# get the configuration XML
	my $xmlfile = shift @ARGV || "";

	# if test_config is defined - run once and as a dry run - DUH!
	(1 == $test_config) and $dry_run = 1 and $once = 1;

	# if an XML was not specified - use a simple one just with API and
	# secret key
	my $tmp_xml_file_created = "";
	if ($xmlfile eq "") {
		$tmp_xml_file_created = $xmlfile = &get_basic_xml();
	}

	# initialize M3
	my $M3 = MonitisMonitorManager->new(
		configuration_xml => $xmlfile,
		dry_run => $dry_run,
		mass_load => $mass_load,
		test_config => $test_config,
		syslog => $syslog);

	# invoke all the agents
	if ($raw ne "") {
		$M3->handle_raw_command(raw_command => $raw);
	} else {
		if ($once == 1) {
			$M3->invoke_agents();
		} else {
			$M3->invoke_agents_loop();
		}
	}

	# cleanup a tmp xml if it was created
	$tmp_xml_file_created ne "" and unlink $tmp_xml_file_created;
}

&main()
