# $Id: offline.pl,v 1.7.2.1 2004/01/09 16:41:38 $ #
#ident "@(#)VCS:$Source: /ovsrc/int/CVS/src/nb_ha/vcs/offline.pl,v $"
# START COPYRIGHT-NOTICE: 1998-2003
# 
# Copyright 1998-2003 VERITAS Software Corporation.
# All rights reserved.
# 
# VERITAS,  the  VERITAS Logo and all other  VERITAS  product names and
# slogans are trademarks  or registered trademarks of  VERITAS Software
# Corporation.  VERITAS and the VERITAS Logo Reg.  U.S. Pat. & Tm. Off.
# Other product names and/or slogans mentioned herein may be trademarks
# or registered trademarks of their respective companies.
# 
# The Licensed Software and Documentation are  deemed to be  commercial
# computer software  and  commercial computer software documentation as
# defined in FAR Sections 12.212 and DFARS Section 227.7202.
# 
# END COPYRIGHT-NOTICE.
#

eval 'exec /usr/openv/netbackup/bin/cluster/vcs/perl -S $0 ${1+"$@"}'
        if 0;

my ($ResName, $ServerType, $RobotDaemon, $MonScript, $AgentDebug);
my ($VCS_HOME);
($ResName, $ServerType, $RobotDaemon, $MonScript, $AgentDebug) = @ARGV;

$VCS_HOME = $ENV{"VCS_HOME"};
$VCS_HOME = "/opt/VRTSvcs" unless (defined ($VCS_HOME));
my ($NbuRoot, $SHARED_NBU_SPACE, $rc);
$NbuRoot = "/usr/openv";

if ( -f "$VCS_HOME/bin/ag_i18n_inc.pl" )
{
	require "$VCS_HOME/bin/ag_i18n_inc.pl";
	VCSAG_SET_ENVS ($ResName, 20001);
}

my $currTime = localtime();

open(AGENTLOG, ">> ./VCS_AGENT_DEBUG.log");

sub doCommand
{
	my $doCmd = $_[0];
	chomp($doCmd);
	print "$doCmd\n";
	system("$doCmd\n");
	if ( $? ne 0 )
	{
		print AGENTLOG "$currTime $doCmd Failed.\n";
		close(AGENTLOG);
		exit 100;
	}
}

print AGENTLOG "\n$currTime Start Offline.......\n";


open(STATEFILE, "> ./.NBU_STATE");
print STATEFILE "STATE=OFFLINE\n";
close(STATEFILE);

### Stop netbackup
$rc = system("$NbuRoot/netbackup/bin/goodies/bp.kill_all FORCEKILL 2> /dev/null");
if ( $rc )
{
	exit $rc/256;
}


if ( ! -f "$NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP" )
{
	print AGENTLOG "$currTime VCS_NBU_RSP file NOT found.";
	close(AGENTLOG);
        exit 100;
}

chomp($SHARED_NBU_SPACE=`grep "^SHARED_DISK" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/SHARED_DISK=//`);


$rootLink = "";
if ( -l "$NbuRoot" )
{
        $rootLink = readlink("$NbuRoot");
}

if ( ("$SHARED_NBU_SPACE" ne "$NbuRoot") && ("$SHARED_NBU_SPACE" ne "$rootLink") )
{
    ### Check for links to shared disk (remove them if needed)...
    chomp($SH_LINKS=`grep "^LINK" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/LINK//`);
    @SHLINK_ARRAY = split('\n',$SH_LINKS);
    $sizeLink = @SHLINK_ARRAY;

    if ( "$sizeLink" == 0 )
    {
	print AGENTLOG "$currTime NO LINKS found in VCS_NBU_RSP file.";
	close(AGENTLOG);
	exit 100;
    }

    foreach $shlink ( @SHLINK_ARRAY )
    {
        chomp($shlink);
        $shlink =~ s/^ *=//;    # Remove = from the start
        $shlink =~ s/^ +//;     # Remove spaces from the start
        $shlink =~ s/ +$//;     # Remove spaces from the end
	if ( -l "$NbuRoot/$shlink" )
        { # Link exists on local disk
		doCommand("rm -f $NbuRoot/$shlink\n");
	}
    }
}

close(AGENTLOG);

exit 0;
