#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2003 
# 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 
#                                                                   #
# @(#)74   1.22   src/csm/install/rmnode_client.perl, setup, csm_rfish, rfishs001b 8/21/06 07:25:08

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

=head1	rmnode.client


	Description: This command runs of the node throucg dsh

	AIX:	Remove the logs  on the node.
	Linux:	Removes the logs, csm, rsct file sets and /var/opt/csm/CFMlocal
		file from the node

	Return 0 or 1.


        Development notes:

        The html view of the pod headers in this file can be updated
        by running the command:  

                  tidypod <filename>

	The html veiw of the pod headers will be in the file ./<filename>.html
	and can be viewed with a browser.

	The tidy formatted file will be in <filename>.tdy

	Be sure to run tidypod and copy the tdy file to <filename> before checking
	in changes.

=cut

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

BEGIN    # load first at runtime
{
	use lib '/opt/csm/pm';
	use strict;
	use locale;

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

}

use Getopt::Std;
use NodeUtils;
use CSMDefs;
use ServerUtils;
use ArchiveUtils;

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

=head3	initialize	

	Initialize the global data

        Notes:

=cut

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

sub initialize
{

	# setup the global defs data
	ServerUtils->get_OSDefs();

	$::ostype    = NodeUtils->get_OSName;                 #AIX/Linux
	$::osdistr   = NodeUtils->get_DistributionName;       #RedHat
	$::osversion = NodeUtils->get_DistributionVersion;    #5.1.0/7.1/7.2
	$::arch      = ArchiveUtils->get_PkgArchitecture;     #i.86

	if ($::arch =~ /i.86/) { $::arch = "i386"; }

	$::csmvers = NodeUtils->get_CSMVersion("csm.core");

	@::csm_logfilelist;    # csm file, log list thate need to removed
	@::csm_rpmlist;        # Linux CSM RPM list to uninstall
	@::rsct_rpmlist;       # Linux RSCT RPM list to uninstall

	$::GLOBAL_EXIT = 0;
	$::logging     = 0;
	$::temp_remove_item;
	$::rsct_basic_Flg = 0;

}

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

=head3	getArgs	

        Notes:

=cut

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

sub getArgs
{
	if (!getopts('vVh'))
	{
		MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
									 'csminstall', 'I',
									 'IMsgrmnodeClientNodeUsage');
		exit 1;
	}

	if ($::opt_h)
	{
		MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
									 'csminstall', 'I',
									 'IMsgrmnodeClientNodeUsage');
		exit 0;
	}
	if ($::opt_v || $::opt_V)
	{
		if ($::opt_v) { $::VERBOSE = $::opt_v; }
		if ($::opt_V) { $::VERBOSE = $::opt_V; }
	}
}

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

=head3	get_install_lists

	Get the lists of files that we need to remove.

        Notes:

=cut

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

sub get_install_lists
{

	if ($::PLTFRM eq "AIX")
	{    # AIX lists of filesets to install
		    # make sure we have the info we need
		if (($::ostype eq "") || ($::osversion eq "") || ($::csmvers eq ""))
		{
			MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
						'csminstall', 'E', 'EMsgNoConfigInfo', $::CSM_NODENAME);
			return $::FALSE;
		}

		# get the appropriate lists of filesets/packages
		my %pkgdefs =
		  ServerUtils->get_pkgdefs(      $::ostype, $::osdistr, $::osversion,
								   $::arch,   "MgdNode",  $::csmvers);

		# log file list that need to uninstalled
		@::csm_logfilelist = @{$pkgdefs{logfiles}};
	}
	else
	{    # Linux lists of filesets to install
		    # make sure we have the info we need
		if (   ($::ostype eq "")
			|| ($::osdistr   eq "")
			|| ($::osversion eq "")
			|| ($::csmvers   eq ""))
		{
			MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
						'csminstall', 'E', 'EMsgNoConfigInfo', $::CSM_NODENAME);
			return $::FALSE;
		}

		# get the appropriate lists of filesets/packages
		my %pkgdefs =
		  ServerUtils->get_pkgdefs(      $::ostype, $::osdistr, $::osversion,
								   $::arch,   "MgdNode",  $::csmvers);

		# check if the node is management server to it self.
		# if yes, than remove only client
		if (NodeUtils->isMgmtSvr())
		{
			MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
							 'csminstall', 'I', 'IMsgNotRemoveMgmtServerPkgs');
            for my $pkgs ( keys %{$pkgdefs{'pkgs'}{'MgdNode'}})
            {
                if ( $pkgdefs{'pkgs'}{'MgdNode'}{ $pkgs}->needToBeUninstall())
                {
                    $pkgdefs{'pkgs'}{'MgdNode'}{ $pkgs}->
                        removeRpmList($pkgdefs{'pkgs'}{'MgmtServer'}{ $pkgs});
                }
		$pkgdefs{'pkgs'}{'MgdNode'}{ $pkgs}->removeRpmByShortName( 'csm.core');
            }
		}

		# if the rsct.basic is installed than don't remove rsct
		NodeUtils->runcmd("$::RPMCMD -q rsct.basic", -1);
		if ($::RUNCMD_RC == 0)
		{
		    $pkgdefs{'pkgs'}{'MgdNode'}{$::RSCT_PKGS}->setToBeUninstall($::FALSE);
			$::rsct_basic_Flg = 1;
		}

		# log file list that need to uninstalled
		@::csm_logfilelist = @{$pkgdefs{logfiles}};
	}
	push @rpmLists, $pkgdefs{'pkgs'}{'MgdNode'};
	
	return \@rpmLists;
}

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

=head3	remove_files_dir

	Remove the directries and files passes.

        Notes:

=cut

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

sub remove_files_dir
{
	my ($file_name, undef) = @_;
	NodeUtils->runcmd("$::RM -rf $file_name", -1);
	if ($::RUNCMD_RC != 0)
	{
		MessageUtils->messageFromCat(
									 'csmInstall.cat',   $::MSGMAPPATH,
									 'csminstall',       'W',
									 'EMsgCannotDelete', $file_name
									);
	}
	return $::RUNCMD_RC;
}

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

=head3	remove_installp

	Remove the installp immage from the system

        Notes:

=cut

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

sub remove_installp
{
	my ($installp_img, undef) = @_;
	NodeUtils->runcmd("$::INSTALLP -u $installp_img", -1);
	if ($::RUNCMD_RC != 0)
	{
		MessageUtils->messageFromCat(
									 'csmInstall.cat',        $::MSGMAPPATH,
									 'csminstall',            'W',
									 'EMsgRemoveInstallpImg', $installp_img
									);
	}
	return $::RUNCMD_RC;

}

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

=head3	Main	

        Notes:

=cut

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

{    # main

	my $ret_code = 0;

	# initialize the global data
	&initialize;

	# get/check command line args
	&getArgs;

	#  Get the lists of filesets and file that must be uninstalled or removed
	my $rpmListRef = &get_install_lists;
	if (!$rpmListRef)
	{
		MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
									 'csminstall', 'E', 'EMsgNO_LIST');
		$::GLOBAL_EXIT = 1;
		exit;
	}

	# Uninstalling and removing the files from the node
	if ($::PLTFRM eq "AIX")
	{
		MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
									 'csminstall', 'I', 'IMsgUninstallingCsm');

		# Remove the log and otherfiles
		foreach $::temp_remove_item (@::csm_logfilelist)
		{
			$ret_code = &remove_files_dir($::temp_remove_item);
		}
	}
	else
	{
		# Remove the csm rpms
        ArchiveUtils->uninstall_packages_Linux( $rpmListRef, '-e');

		# Remove the log and otherfiles
		foreach $::temp_remove_item (@::csm_logfilelist)
		{
			$ret_code = &remove_files_dir($::temp_remove_item);
		}

		# Remove /var/opt/csm/isInstallServer file if it exists
		if (-f $::IsInstallServer_file)
		{
			$ret_code = &remove_files_dir($::IsInstallServer_file);
		}

	}
	if ($::rsct_basic_Flg == 1)
	{
		MessageUtils->messageFromCat('csmInstall.cat', $::MSGMAPPATH,
									 'csminstall', 'I',
									 'IMsgDidnotUninstallRsct');
	}

	exit $ret_code;
}
