#!/usr/bin/perl 
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2004,2007 
# All Rights Reserved 
#  
# US Government Users Restricted Rights - Use, duplication or 
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp. 
#  
# IBM_PROLOG_END_TAG 
# @(#)28   1.4   src/csm/install/check_gpl_packages.perl, setup, csm_rfish, rfishs001b 4/4/06 03:59:08

#$::DEBUG = 1;

#-------------------------------------------------------------------------------

=head1    check_gpl_packages.perl

	Check if any GPL packages must be installed on this install server.
	This command runs only on the install server (or on the management 
	server if it is acting as an install server).  Checks to see if
	any of the @gpl_packages listed in the pkgdefs file are already
	installed on this install server.  If any GPL packages are not
	intsalled, display their names.  If no GPL packages are installed,
	do not display anything.


	syncServers.client [-h] [-v | -V]

	Flags:

	-v | -V   	verbose mode
	-h        	display this usage information.


	Exit codes:

		0 - success
		1 - failure

	Notes:  
=cut

#-------------------------------------------------------------------------------

=head3	file header

	"use" statements and MSGCAT defines

        Notes:

=cut

#--------------------------------------------------------------------------------

BEGIN
{
	use lib '/opt/csm/pm';

	$::MSGCAT     = 'csmInstall.cat';
	$::MSGSET     = 'csminstall';
	$::MSGMAPPATH = '/opt/csm/msgmaps';
}

use strict;
use Getopt::Std;
use ServerUtils;
use NodeUtils;
use CSMDefs;
use InstallKRB5Utils;

#-------------------------------------------------------------------------------

=head3	initialize

	Read the OSDefs and pkgdefs based on the current machine's attibutes.

=cut

#-------------------------------------------------------------------------------

sub initialize
{

	# Get the attributes for the install server from the local machine.
	$::PREREQS_ATTR{'OSName'}              = NodeUtils->get_OSName;
	if ($::PREREQS_ATTR{'OSName'} eq "Linux")
	{
		exit 0;
	}
	$::PREREQS_ATTR{'PkgArchitecture'}     = NodeUtils->get_PkgArchitecture;
	$::PREREQS_ATTR{'DistributionName'}    = NodeUtils->get_DistributionName;
	$::PREREQS_ATTR{'DistributionVersion'} = NodeUtils->get_DistributionVersion;
	$::PREREQS_ATTR{'CsmCoreVersion'} = NodeUtils->get_CSMVersion("csm\.core");

	if ($::PREREQS_ATTR{'PkgArchitecture'} =~ /i.86/)
	{
		$::PREREQS_ATTR{'PkgArchitecture'} = "i386";
	}
	if (length($::PREREQS_ATTR{'CsmCoreVersion'}) == 0)
	{
		MessageUtils->message('E2', 'EMsgNO_CORE');
	}

	# Simpler global versions of these variables.
	$::ostype    = $::PREREQS_ATTR{'OSName'};
	$::csmvers   = $::PREREQS_ATTR{'CsmCoreVersion'};
	$::osdistr   = $::PREREQS_ATTR{'DistributionName'};
	$::osversion = $::PREREQS_ATTR{'DistributionVersion'};
	$::arch      = $::PREREQS_ATTR{'PkgArchitecture'};

	# Get the operating system definitions for this install server
	%::OSDefs =
	  ServerUtils->get_OSDefs(
							   $::PLTFRM,
							   $::PREREQS_ATTR{'DistributionName'},
							   $::PREREQS_ATTR{'DistributionVersion'},
							   $::PREREQS_ATTR{'PkgArchitecture'}
							  );

	# get the appropriate lists of filesets/packages for this install server
	%::pkgdefs =
	  ServerUtils->get_pkgdefs(
								$::PREREQS_ATTR{'OSName'},
								$::PREREQS_ATTR{'DistributionName'},
								$::PREREQS_ATTR{'DistributionVersion'},
								$::PREREQS_ATTR{'PkgArchitecture'},
								"InstallServer",
								$::PREREQS_ATTR{'CsmCoreVersion'}
							   );

	$::GLOBAL_EXIT = 0;

	use File::Path;        # This gives us the mkpath subroutine
	use File::Basename;    # This gives us the dirname subroutine
	($::Bin) = dirname($0);    # The pathname for this command
}

#-------------------------------------------------------------------------------

=head3	getArgs

	parse command line arguments

        Notes:

=cut

#-------------------------------------------------------------------------------

sub getArgs
{
	if (!getopts('vVh'))
	{
		MessageUtils->message('I', 'IMsgcheck_gpl_packages_Usage');
		exit 1;
	}

	if ($::opt_h)
	{
		MessageUtils->message('I', 'IMsgcheck_gpl_packages_Usage');
		exit 0;
	}
	if ($::opt_v || $::opt_V)
	{
		if ($::opt_v) { $::VERBOSE = $::opt_v; }
		if ($::opt_V) { $::VERBOSE = $::opt_V; }
	}

	return $::OK;
}

#-------------------------------------------------------------------------------

=head3	check_gpl_packages

	Determines if GPL packages must be installed on this install server.

	Print the names of the GPL packages that are not already installed.

	Return Codes:

		0 - success
		1 - failure

        Notes:

=cut

#-------------------------------------------------------------------------------

sub check_gpl_packages
{
	my @gpl_packages = (@{$::pkgdefs{gpl_packages}});

	foreach my $gpl_package (@gpl_packages)
	{
		my $cmd = "$::RPMCMD -q $gpl_package";
		my $output = NodeUtils->runcmd($cmd, -1);    # do not print error or exit
		if ($::RUNCMD_RC != 0)
		{

			# The package is not already installed.
			# Print one package name per line
			print "$gpl_package\n";
		}
	}

	return $::OK;
}

#-------------------------------------------------------------------------------

=head3	MAIN Main main

        Notes:

=cut

#-------------------------------------------------------------------------------

{

	# initialize globals
	&initialize;

	# get/check command line args
	&getArgs;

	# Do the check
	my $rc = &check_gpl_packages;

	exit $rc;
}

