#!/usr/bin/perl

use CGI;
use Mysql;
use Getopt::Long;

use Env qw(LSBUSER LSBDBPASSWD LSBDB LSBDBHOST);

sub usage()
{
print STDERR "mkcmdlist2 -v lsbversion\n";
die;
}

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

# Uncomment to trace SQL statments
#$trace=1;

#
# 2) Establish connection to the database
#

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

#
# 3) Get the list of cmds
#

open(CMDS,">cmdlist");
print CMDS "# Generated file\n";
print CMDS "# List of LSB commands for the Python cmdchk\n";

$select = "SELECT DISTINCT Cid,Cname,Cpath FROM Command ";
$select.= "LEFT JOIN ModCmd ON MCcid=Cid ";
$select.= "WHERE (MCappearedin <= '$lsbversion' and MCappearedin<>'') ";
$select.= "AND (MCwithdrawnin IS NULL OR MCwithdrawnin > '$lsbversion') ";
$select.= "AND Cbuiltin!='Yes' ";
$select.= "ORDER BY Cname ";

#print $select;

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

for(1..$sth->numrows) {
	%entry=$sth->fetchhash;
	$cmdname=$entry{'Cname'};
	if ($entry{'Cpath'} eq "") {
	    $cmdpath='None';
	} else {
	    $cmdpath=$entry{'Cpath'};
	}
	print CMDS "$cmdname $cmdpath\n";
}
close(CMDS);
