#!/usr/bin/perl -w
#
# generate mrtg.cfg segments from templates, writes to stdout
#
# format of datafile: 3 entries per line
# hostname disklabel path
#
# usage: gen_mrtg_cfg templatefile datafile > mrtg.conf.file
# 
# version: $Id: gen_mrtg_cfg,v 1.1 2002/08/14 17:00:22 als Exp $
# author: Alexander Schreiber <als@thangorodrim.de>
#



use strict;

if ( scalar(@ARGV) != 2 ) {
    print "usage: gen_mrtg_cfg templatefile datafile > mrtg.conf.file\n";
    exit(0);
}


my $line;
my $template;
my $entry;
my ($hostname, $disklabel, $path);

open(TEMPLATE, "<$ARGV[0]") or die "cannot open template file $ARGV[0]";
$template = '';
while ( $line = <TEMPLATE> ) { 
    $template .= $line;
}
close(TEMPLATE);

open(DATA, "<$ARGV[1]") or die "cannot open data file $ARGV[1]";
while ( $line = <DATA> ) {
    chomp($line);
    if ( $line =~ /^(\S+)\s+(\S+)\s+(\S+)$/ ) {
        ($hostname, $disklabel, $path) = ($1, $2, $3);
        $entry = $template;
        $entry =~ s/\%HOSTNAME\%/$hostname/g;
        $entry =~ s/\%DISKLABEL\%/$disklabel/g;
        $entry =~ s/\%PATH\%/$path/g;
        print "\n$entry\n";
    }
}
