# $Id: nwinst.pl,v 1.5 2005/11/28 20:36:28 laim Exp $ Copyright (c) 2000-2005 EMC Corporation
#
#
# Copyright (c) 2000-2005 EMC Corporation.
#
# All rights reserved.  This is an UNPUBLISHED work, and
# comprises proprietary and confidential information of EMC.
# Unauthorized use, disclosure, and distribution are strictly
# prohibited.  Use, duplication, or disclosure of the software
# and documentation by the U.S. Government is subject to
# restrictions set forth in a license agreement between the
# Government and EMC or other written agreement specifying
# the Government's rights to use the software and any applicable
# FAR provisions, such as FAR 52.227-19.

push(@INC, "$ENV{FT_DIR}/bin"); 
require "ft.pl";
$DATA_SOURCE = "networker_ds";
$LVM_DATA_SOURCE = "LVM_networker_ds";
${NSR_SHARED_DISK_DIR}="$ARGV[0]";
$curnode = $ft::CurrentNode;
$attached = 1;
$LVM_used = 1;
$LVM_attached = 1;

# check if LVM_networker_ds exists and is online
$errCode = &ft::GetDataSourceInfo($LVM_DATA_SOURCE, $attachCount, $maxAttachCount, *nodeInfoList);
if (($errCode != $FTERR::SUCCESS) && ($errCode != $FTERR::NO_SUCH_DATASOURCE)) {
	print "ft::GetDataSourceInfo $LVM_DATA_SOURCE failed - $ft::ErrorString[$errCode]\n";
	goto out;
}

if ($errCode == $FTERR::NO_SUCH_DATASOURCE) {
	$LVM_used = 0;
} else {
	if ($attachCount > 0) {
		# there should be only one node attaching LVM_networker_ds since maxAttachCount == 1
		@nodes = keys(%nodeInfoList);
		@states = values(%nodeInfoList);
		while ($#nodes >= 0) {
			$node = pop(@nodes);
			$state = pop(@states);

			if ($node ne $curnode) {
				if ($state == $FT::DS_ATTACHED) {
					print "$LVM_DATA_SOURCE should be detached from $node.\n";
					goto out;
				}
			}
		}
	} else {
		$LVM_attached = 0;
	}
}

# check if networker_ds is online
$errCode = &ft::GetDataSourceInfo($DATA_SOURCE, $attachCount, $maxAttachCount, *nodeInfoList);
if ($errCode != $FTERR::SUCCESS) {
	print "ft::GetDataSourceInfo $DATA_SOURCE failed - ";
	print "$ft::ErrorString[$errCode]\n";
	goto out;
}

@nodes = keys(%nodeInfoList);
@states = values(%nodeInfoList);
while ($#nodes >= 0) {
	$node = pop(@nodes);
	$state = pop(@states);

	if ($node eq $curnode) {
		if ($state == $FT::DS_ATTACHED) {
			break;
		} else {
			$attached = 0;
		}
	} else {
		if ($state == $FT::DS_ATTACHED) {
			print "$DATA_SOURCE and $LVM_DATA_SOURCE should be detached from $node.\n";
			goto out;
		}
	}
}

if ($attached == 0) {
	if (($LVM_used != 0) && ($LVM_attached == 0)) {
		$errCode = &ft::AttachDataSource($LVM_DATA_SOURCE, $curnode, 10, $exitStatus);
		if ($errCode != $FTERR::SUCCESS) {
			print "ft::AttachDataSource $LVM_DATA_SOURCE failed - ";
			print "$ft::ErrorString[$errCode]\n";
			goto out;
		}
	}

	$errCode = &ft::AttachDataSource($DATA_SOURCE, $curnode, 10, $exitStatus);
	if ($errCode != $FTERR::SUCCESS) {
		print "ft::AttachDataSource $DATA_SOURCE failed - ";
		print "$ft::ErrorString[$errCode]\n";
		goto out2;
	}
}

# ${NSR_SHARED_DISK_DIR} should already exist

if (!-d "${NSR_SHARED_DISK_DIR}/nsr") { 
     	if (mkdir("${NSR_SHARED_DISK_DIR}/nsr", 0755) == FALSE) {
		print "mkdir Error\n";
		goto out1;
	}
}
if (!-d "${NSR_SHARED_DISK_DIR}/nsr/res") {
	if (mkdir("${NSR_SHARED_DISK_DIR}/nsr/res", 0755) == FALSE) {
		print "mkdir Error\n";
		goto out1;
	}
}

if ($ENV{OS} eq "Windows_NT") {
	$nwinstcreate="nwinstcreate.exe";
} else {
	$nwinstcreate="nwinstcreate";
}

# call nwinstcreate. If error nwinstcreate will print out messages and the script will continue. 
# IDinitialize file must be created by the system administrator in order
# for it to be used by nsrexecd.
`$nwinstcreate -f "${NSR_SHARED_DISK_DIR}/nsr/res/IDinitialize" -n $ARGV[1]`;

if (!-f "${NSR_SHARED_DISK_DIR}/nsr/res/IDinitialize") {
	print "Create ${NSR_SHARED_DISK_DIR}/nsr/res/IDinitialize failed.\n";
	goto out1;
}

if ($attached == 0) {
	$errCode = &ft::DetachDataSource($DATA_SOURCE, $curnode, 10, $exitStatus);
	if ($errCode != $FTERR::SUCCESS) {
		print "ft::DetachDataSource $DATA_SOURCE failed - $ft::ErrorString[$errCode]\n";
	}
}

if (($LVM_used != 0) && ($LVM_attached == 0)) {
	$errCode = &ft::DetachDataSource($LVM_DATA_SOURCE, $curnode, 10, $exitStatus);
	if ($errCode != $FTERR::SUCCESS) {
		print "ft::DetachDataSource $LVM_DATA_SOURCE failed - $ft::ErrorString[$errCode]\n";
	}
}

exit (0);

out1:
	if ($attached == 0) {
		&ft::DetachDataSource($DATA_SOURCE, $curnode, 10, $exitStatus);
		if ($errCode != $FTERR::SUCCESS) {
			print "ft::DetachDataSource $DATA_SOURCE failed - $ft::ErrorString[$errCode]\n";
			goto out;
		}
	}

out2:
	if (($LVM_used != 0) && ($LVM_attached == 0)) {
		&ft::DetachDataSource($LVM_DATA_SOURCE, $curnode, 10, $exitStatus);
		if ($errCode != $FTERR::SUCCESS) {
			print "ft::DetachDataSource $LVM_DATA_SOURCE failed - $ft::ErrorString[$errCode]\n";
			goto out;
		}
	}

out:
	print "Please make sure you are system administrator and have permissions to operate data sources in EMC Fulltime Autostart.\n";
	exit (1);

