#! /usr/bin/perl -sw

# Created by Steve Voisey (srv) 12 Oct 2010.
# email: [steve.voisey@ericsson.com]
# rpmVersion.pl - V1.000
#
# Replace the old script cmsVersion.pl that had release information hard coded in the script
# with a generic version that reads the release components from an external profile file.
#

my $divide = "=" x 60 . "\n";
my $hostname = `hostname`;
chomp($hostname);

my $noFileMessage = <<ENDNOFILEMSG;  

ERROR: No input file specified!

 Either the following file must exist:

    /opt/tandbergtv/cms/scripts/install/cms.core.version.*.txt

 or specify the version file as an input parameter: 

    ./rpmVersion.pl -file=xxxx.txt

ENDNOFILEMSG

unless (defined($file)) {
    @tmp = </opt/tandbergtv/cms/scripts/install/cms.core.version.*.txt>;
    $file = $tmp[0];
}
 
unless (defined($file)) { die "$noFileMessage\n"; }

unless (open (FILE, "<$file")) { die "cannot open input file [$file] $!"; }
@versionArray   = <FILE>;
close FILE;

print "\n";
print "${divide}";
print "Hostname: $hostname\n";
print "Profile:  $file\n";
print "${divide}";

foreach $line (@versionArray) {
    if ( $line =~ /^#/ )    { next; }
    if ( $line =~ /^\s*$/ ) { next; }
    if ( $line =~ /^.*=/i ) {
         ( $heading, $value ) = $line =~ /^(.*)=(.*)$/; 
         chomp($value);
         @valueArray = split(',', $value);

         print "\n${divide}$heading\n${divide}";
         rpmQuery(\@valueArray);
    }
}

print "\n${divide}non RPM component package versions\n${divide}";
otherQuery();
print "${divide}";
print "\n";

exit;

##################################################################################
# rpmQuery
##################################################################################

sub rpmQuery {
        my $symArrayRef = $_[0];
        foreach $element (@$symArrayRef) {
                #print "$element\t\t:";
                print `rpm -q $element`;
        }
}

##################################################################################
# otherQuery
##################################################################################

sub otherQuery {
        print `javac -version`;
}

##################################################################################
# Finish
##################################################################################

