#!/usr/bin/perl

use Mysql;
use Getopt::Long;

use Env qw(LSBUSER LSBDBPASSWD LSBDB LSBDBHOST);

sub usage()
{
print STDERR "Usage: mktaghdr -v <lsbversion(s)>\n";
print STDERR "    LSB versions should be comma-separated.\n";
die;
}

GetOptions("v=s" => \$lsbversion);
if( !$lsbversion ) { usage(); }

$lsbversion_min = "";
$lsbversion_max = "";
foreach (split(/,/, $lsbversion)) {
	$lsbversion_min = $_ if (($lsbversion_min eq "") or ($lsbversion_min > $_));
	$lsbversion_max = $_ if (($lsbversion_max eq "") or ($lsbversion_max < $_));
}

$Dbh = Mysql->connect($LSBDBHOST,$LSBDB,$LSBUSER, $LSBDBPASSWD) || die $Mysql::db_errstr;

sub
dumpenum($$)
{
local($condition,$tname)=@_;

$select = "SELECT DISTINCT Rname,Rtag FROM RpmTag ";
$select.= "WHERE ".$condition." ";
$select.= "AND (Rappearedin IS NOT NULL and Rappearedin <= '$lsbversion_max' and Rappearedin<>'') ";
$select.= "AND (Rwithdrawnin IS NULL OR Rwithdrawnin > '$lsbversion_min') ";
$select.= "ORDER BY Rtag ";

#print "$select\n";

$sth = $Dbh->query($select) || die $Dbh->errmsg();

print "typedef enum {\n";

for(1..$sth->numrows) {
	%entry=$sth->fetchhash;
	printf "\t%s\t= %d,\n", $entry{'Rname'}, $entry{'Rtag'};
	}
printf "\t} %s;\n\n", $tname;
}

print "/* Generated file - Do Not Edit */\n\n";

dumpenum("Rgroup='Private'","HdrPrivIndexTag");
dumpenum("Rgroup='Signature'","SigIndexTag");
dumpenum("Rgroup!='Private' AND Rgroup!='Signature' AND Rgroup!='Ignore'","RpmIndexTag");

