#!/usr/bin/perl

use DBI;
require "config";

open(GRP,$GROUPS) || die "cannot open $GROUPS\n";
@groups = sort <GRP>;
close(GRP);

$dbh = DBI->connect($dsn,$dbUser,$dbPass) || die "cant connect to mysql\n";
$sth = $dbh->prepare("SELECT rpmgroup FROM groups ORDER BY id ASC");
$sth->execute();
while ($row = $sth->fetchrow_array()){
	push @row, $row;
}

for my $i (1 .. $#groups+1){
	chomp($groups[$i-1]);
	if (isInArray($groups[$i-1],\@row)){
		next;
	}
	$sth = $dbh->prepare("INSERT groups VALUES('',?)");
	$sth->execute($groups[$i-1]);
}

if ($sth) {
	$sth->finish();
}
$dbh->disconnect();

sub isInArray{
    my ($element,$array) = @_;
    my $match = 0;
        
    if (ref($array) && defined($element)){
        foreach (@{$array}){
	    if ($element eq $_){
		$match = 1;
		last;
	    }
	}
	return $match;
    } else {
	return undef;
    }
}
