#!/usr/local/bin/perl -w
# -*- perl -*-

# Cricket: a configuration, polling and data display wrapper for RRD files
#
#    Copyright (C) 1998 Jeff R. Allen and WebTV Networks, Inc.
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.

#
# run a cricket subtree and send any output to the provided email addresses
#

use strict;
use Getopt::Long;

my $result	= "";
my %opts	= ();
my @output	= ();

$result = GetOptions(\%opts, "mail|m=s", "subtree|s=s");

if (! $result) {
	die "Error handling options.\n";
}

if (! defined($opts{"mail"})) {
	$opts{"mail"} = 'smrtg@corp.webtv.net';
}

if (! defined($opts{"subtree"})) {
	die "Must provide a subtree to run.\n";
}

@output = `$ENV{"HOME"}/cricket/collect-subtrees $opts{"subtree"} 2>&1`;

if ($#output > 0) {
	my $subject = "output from subtree " . $opts{"subtree"};

	open(MAIL, "|/bin/mailx -s \"$subject\" $opts{mail}") ||
		die "Couldn't run /bin/mailx to send subtree output: $!\n";
	print MAIL @output;
	close(MAIL);
}
