# $Id: online.pl,v 1.7.2.1 2004/01/09 16:41:38 $ #
#ident "@(#)VCS:$Source: /ovsrc/int/CVS/src/nb_ha/vcs/online.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;

# ARGV variable declarations
my ($ResName, $ServerType, $RobotDaemon, $MonScript, $AgentDebug);

# common references
my ($VCS_HOME, $HALOG);

# NetBackup specific
my ($ARCH, $EGREP, $NETBACKUP);

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

$ARCH = `uname`;

$VCS_HOME = $ENV{"VCS_HOME"};
$VCS_HOME = "/opt/VRTSvcs" unless (defined ($VCS_HOME));
$EGREP = "/usr/bin/egrep";
$NETBACKUP = "/usr/openv/netbackup/bin/goodies/netbackup";

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

# Promote the Nodename of the Cluster Member in bp.conf for proper
# Nbu daemon startup

my ($LANGSAVE, $SysName);
$LANGSAVE = "";
$SysName = "";

$LANGSAVE = $ENV{"LANG"};
$ENV{"LANG"} = "C";

my ($NbuRoot, $SHARED_NBU_SPACE, $rc);
$NbuRoot="/usr/openv";


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

my $currTime = localtime();

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

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

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

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


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

### If shared disk is not mounted or linked to /usr/openv - then do some linking stuff
if ( ("$SHARED_NBU_SPACE" ne "$NbuRoot") && ("$SHARED_NBU_SPACE" ne "$rootLink") )
{
    ### Check for shared directories (mv or mkdir if needed)...
    chomp($SH_DIRS=`grep "^DIR" $NbuRoot/netbackup/bin/cluster/vcs/VCS_NBU_RSP | sed -e s/DIR//`);
    @SHDIR_ARRAY = split('\n',$SH_DIRS);
    $sizeDir = @SHDIR_ARRAY;

    if ( "$sizeDir" == 0 )
    {
	print AGENTLOG "$currTime NO DIR found in VCS_NBU_RSP file.\n";
	close(AGENTLOG);
	exit 100;
    }

    foreach $shdir ( @SHDIR_ARRAY )
    {
	chomp($shdir);
	$shdir =~ s/^ *=//;     # Remove = from the start
	($dirName, $cmd) = split(' ', $shdir);

	$dirName =~ s/^ +//;    # Remove spaces from the start
	$dirName =~ s/ +$//;    # Remove spaces from the end
	if ( ! -d "$SHARED_NBU_SPACE/$dirName" )
	{ # Shared dir does not exist
		if ( "$cmd" eq "mv" )
		{
			if (  ! -d "$NbuRoot/$dirName" )
			{
				print AGENTLOG "$currTime DIR - $NbuRoot/$dirName NOT found.\n";
				close(AGENTLOG);
				exit 100;
			}
			else
			{       # Move the local directory to shared disk
				doCommand("$cmd $NbuRoot/$dirName $SHARED_NBU_SPACE/$dirName");
			}
		}
		elsif ( "$cmd" eq "" )
		{       # Make the directory on shared disk
			print AGENTLOG "$currTime DIR - NO command listed for DIR=$shdir in VCS_NBU_RSP file.\n";
			close(AGENTLOG);
			exit 100;
		}
		else
		{       # Make the directory on shared disk
			doCommand("$cmd $SHARED_NBU_SPACE/$dirName");
		}
	}
	else
	{ # Make sure a "moved" dir does not exist on local drive
		if ( "$cmd" eq "mv" )
		{
			if (  -d "$NbuRoot/$dirName" )
			{
				doCommand("rm -rf $NbuRoot/$dirName");
			}
		}
	}
    }


    ### Check for links to shared disk (create 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.\n";
	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 ( ! -e "$SHARED_NBU_SPACE/$shlink" )
	{  # Dir/File does not exist on shared disk
		if ( (! -l "$NbuRoot/$shlink") && (-f "$NbuRoot/$shlink") )
		{ # Move File to shared disk
			doCommand("mv $NbuRoot/$shlink $SHARED_NBU_SPACE/$shlink");
		}
		else
		{
			print AGENTLOG "$currTime LINK - $SHARED_NBU_SPACE/$shlink do NOT exist.\n";
			close(AGENTLOG);
			exit 100;
		}
	}
	if ( -l "$NbuRoot/$shlink" )
	{ # Link exists on local disk
		doCommand("rm -f $NbuRoot/$shlink\n");
	}
	elsif ( -e  "$NbuRoot/$shlink" )
	{ # Move the file/dir
		doCommand("mv $NbuRoot/$shlink $NbuRoot/$shlink.local\n");
		print AGENTLOG "$currTime LINK - WRN - Moved $NbuRoot/$shlink to $NbuRoot/$shlink.local.\n";
	}
	doCommand("ln -s $SHARED_NBU_SPACE/$shlink $NbuRoot/$shlink\n");
    }
}

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

close(AGENTLOG);

$rc = system("$NETBACKUP start 2> /dev/null");
exit $rc/256;
