#!/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.

BEGIN {
	$gInstallRoot = (($0 =~ m:^(.*/):)[0] || "./") . ".";
}

use lib "$gInstallRoot/../lib";

use Getopt::Long;
use ConfigTree;
use common;
$gCurLogLevel = $kLogDebug;

$gBase = "$ENV{'HOME'}/cricket-config";
GetOptions( "base:s" => \$gBase );

$gCT = newConfigTree();
$gCT->base($gBase);

# reset action for target to my subroutine, instead of the one
# from common.pm that we default to.
$gCT->addAction('target', \&myHandleTarget);

if ($#ARGV+1 > 0) {
	foreach $focus (@ARGV) {
		$gCT->addFocus($focus);
	}
} else {
	# if they gave no args, then they meant "all"
	$gCT->addFocus($gBase);
}

$gCT->processTree();
exit;

sub myHandleTarget {
	my($ct, $tname) = @_;

	# first things first...
    addAutoVariables($ct, 'target', $tname);

	my($target) = $ct->cfg()->{'target'}->{$tname};

	if ($tname eq '--default--') {
		print "Default target set for target path: " .
			$ct->path() . "\n";
	} else {
		# we only try to expand non-default targets
		expandHash($target, $target);

		print "Target dictionary for $tname\n";
	}

	my($k, $v);
	foreach $k (sort (keys(%{$target}))) {
		next if ($k eq "tname");

		$v = $target->{$k};
		print "\t$k = $v\n";
	}
	print "\n";

}

