#!/usr/bin/perl
#
# Convert hostgroup entries in a mon configuration file
# into a local hosts file
#
# Jim Trocki, trockij@transmeta.com
#
# $Id: cf-to-hosts,v 1.2 1998/01/18 17:33:16 trockij Exp $
#
#    Copyright (C) 1998, Jim Trocki
#
#    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., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA

use Getopt::Std;
use Socket;

getopts ("y");

while (defined ($l = <>)) {
    next if $l =~ /^#/;
    chomp $l;
    $l =~ s/^\s*//;
    $l =~ s/\s*$//;

    if ($l eq "") {
    	$ingroup = 0;
	next;
    }

    if ($l =~ /^hostgroup\s+(\w+)\s+(.*)/) {
            $hosts = $2;
	    $ingroup = 1;
	    &printhosts ($hosts);
	    next;
    } elsif ($ingroup) {
	&printhosts ($l);
	next;
    }
}

exit;



sub printhosts {
    my ($hosts) = @_;

    foreach $host (split (/\s+/, $hosts)) {
	next if (defined $h{$host});
	$ip = gethostbyname ($host);
	if (!defined ($ip)) {
	    print STDERR "could not look up $host\n";
	} else {
	    $h{$host} = inet_ntoa ($ip);
	    print "$h{$host}\t$host\n";
	}
    }
}
