# -*-perl-*-

# $Id: cricket_workstation_users.plm,v 1.4 2005/07/29 17:10:30 thomas Exp $

# Generate cricket configuration(s) from an existing system

# $Log: cricket_workstation_users.plm,v $
# Revision 1.4  2005/07/29 17:10:30  thomas
# fix-ups
#
# Revision 1.3  2005/04/11 14:39:36  thomas
# Use Data, not Monitor
#
# Revision 1.2  2005/02/21 20:56:31  thomas
# Help out the stinking pile known as help
#
# Revision 1.1  2005/02/08 16:42:47  thomas
# rebuild configuration files for cricket workstation-users bits
#


=head1 NAME

What::Command::cricket_workstation_users -- Create cricket configs for workstation-users graphs

=head1 SYNOPSIS

what cricket_workstation_users I<cricket-config-file> I<perl-data-file>

=head1 DESCRIPTION

This command grocks the B</Data/Monitor/Cricket-Workstation-Users> subtree and generates two files:

=over 4

=item I<cricket-config-file>

This is a cricket 'targets' file that includes all the computer groups
found in the B<what> configuration.

=item I<perl-data-file>

This data file will be read by the collector specified by the cricket
configuration.  It provides the data structures used by the collector
to know what computers are in each group.

=back

=cut

package What::Command::cricket_workstation_users;

use strict;
use Getopt::Long;
use Sys::Hostname;
use Data::Dumper;
use XML::Simple;
use What::Data;

use base qw(What::Command);

our $helpmsg = "Make cricket configs for workstation-users graphs";

###############################################################################

sub main {
  my $self = shift;
  my $what = shift;
  local (@ARGV) = @_;

  my ($res, $help, $me) = (0, 0, 0);
  Getopt::Long::Configure ("bundling");
  if (!($res = GetOptions (
                           'help|h',   \$help,
                          ))) {
    $what->err ("Bad option passed to cricket command!\n");
    return 0;
  }

  $help and return $self->help();

  if (@ARGV<2) {
    return $self->help;
  }

  $self->doit ($what, $ARGV[0], $ARGV[1]);

  return 0;
}

###############################################################################

sub doit {
  my $self = shift;
  my $what = shift;
  my $cricket_config_file = shift;
  my $collector_data_file = shift;

  my ($content, $target);
  my $monitor = new What::Data;
  if (!defined $monitor) {
    $what->err ("Failed to access monitoring data. (this is Bad (tm)).\n");
    return 0;
  }

  # Build data structure
  my ($host, $d, $data);
  my $bfd = {};
  foreach $host (sort @{$monitor->find()}) {
    $what->debug (3, 'Reading configuration for %s%s', $host, "\n");
    $d = $monitor->find ($host);
    $data = $d->{Data}->{Monitor}->{"Cricket-Workstation-Users"};
    next unless defined ($data);
    foreach (keys %$data) {
      if ($data->{$_}->{do} eq 'yes') {
	# Ignore 2nd floor hosts
	/^2\d\d\d/ and next;
	# Prepend room numbers with rm to avoid confusing xml
	my $dest = $_;
	$dest =~ s/^(\d)/rm$1/;
	$bfd->{$dest}->{hosts}->{$host} = 1;
	$bfd->{$dest}->{comment} = $data->{$_}->{comment};
      }
    }
  }

  # print Dumper ($bfd);

  # Create the cricket config file
  $content = <<__Eof__
# This file created automatically by 
# What::Command::cricket_workstation_users
# Hand-made changes will be lost!
__Eof__
  ;
  foreach $target (sort keys %$bfd) {
    $content .= <<__Eof__
target $target
    short-desc     = "$bfd->{$target}->{comment}"
    directory-desc = "$bfd->{$target}->{comment}"
    lab            = $target
    target-type    = unix-stats

__Eof__
      ;
  }
  $what->check_and_replace_content_revision_control ($cricket_config_file, $content, 0, 0);

  # And write out the collector data file
  $content = XMLout ($bfd);
  $what->check_and_replace_content_revision_control ($collector_data_file, $content, 0, 0);

}

###############################################################################

1;
