#!/usr/local/bin/perl

use DBI;
$hostname = 'localhost';          # Host that serves the mSQL Database
$dbname = 'snmp';                 # mySQL Database name
$doit = 1;

sub usage {
    print "$0 [-H host] [-u user] [-p password] [-v] [-h] [-n] [-d] [-t table1:table2:...] GROUP HOSTS\n";
    exit 0;
}

@tables = qw(prEntry extEntry dskEntry  laEntry fileEntry snmperrs memory);

while ($#ARGV > -1 && $ARGV[0] =~ /^-/) {
    $_ = shift @ARGV;
    usage if (/-h/);
    $hostname = shift if (/-H/);
    $user = shift if (/-u/);
    $pass = shift if (/-p/);
    $verbose = 1 if (/-v/);
    $delete = 1 if (/-d/);
    $doit = 0 if (/-n/);
    if (/-t/) {
	$tables = shift;
	@tables = split(/[: ]/,$tables);
    }
}

$group = shift;

die "no group specified" if (!defined($group));

( $dbh = DBI->connect("DBI:mysql:database=$dbname;host=$hostname", $user, $pass))
    or die "\tConnect not ok: $DBI::errstr\n";
$hthandle = $dbh->prepare("insert into hosttables(host, groupname, tablename, keephistory) values(?, '$group', ?, 0)");
$hghandle = $dbh->prepare("insert into hostgroups(host, groupname) values(?, '$group')");

foreach $i (@ARGV) {
    $hghandle->execute($i)
	or die "\tConnect not ok: $DBI::errstr\n";
    foreach $j (@tables) {
	$hthandle->execute($i, $j)
	    or die "\tConnect not ok: $DBI::errstr\n";
    }
}
$dbh->disconnect();
