#!/usr/bin/perl -w
#
#pragma ident	 "@(#)netapp_check_ccr.pl 1.2	 05/06/13 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#
# This script will check for netapp quorum devices.
# If found, it will print a list of devices together with a
# warning messages and futher instruction on how to remove those.
#
# Return values 
#  0, if no netapps quorum devices found.
#  1, if netapps quorum devices are found.
#
 

use strict;
use Sun::Solaris::Utils qw(gettext);

#
# Initialize the global constants
#



my $FILE_ERR = gettext("Error: failed to open %s for reading\n");
my $PATH_ERR = gettext("Error: file %s does not exist.\n");
my $USAGE = gettext("usage: %s\n");

my $deb_out = 0;
my $netappdevs = "";
my @infrastructurefile = ();
my @netappquorumdevices = ();
my $dev = "";
my $device = "";
my $root_dir = "/";

sub dbg_msg_out($);
sub check_infrastructure();

#
if ($#ARGV > 1) {
        # too many arguments
        printf(STDERR "${USAGE}", $0);
        exit(1);
} elsif ($#ARGV == 0) {
                $root_dir = $ARGV[0];
        }

my $INFRASTRUCTUREFILE = "${root_dir}/etc/cluster/ccr/infrastructure";

#
# Make sure the infrastructure file is there.
#
if (!(-e ${INFRASTRUCTUREFILE})) {
        printf(STDERR "${PATH_ERR}", ${INFRASTRUCTUREFILE});
        exit(1);
}

#
# Next, check the infrastructurefile.  If it returns an error, exit 1.#
# If we later decide to check for other things, we would aggregate the
# errors from various checks and exit only after performing all checks.
#
if (check_infrastructure() != 0) {
        exit(1);
}

#
# All checks passed.
#
exit(0);
#
# subroutine dbg_msg_out
# --------------------
# Example: dbg_msg_out("This is a debug message.\n");
#
# Prints argument 0 if global variable $deb_out is true.
#
sub dbg_msg_out($) {
        if ($deb_out) {
                printf(STDERR "$_[0]");
        }
        return (0);
}

sub check_infrastructure() {
#
# Declare our local variables
#
        
	my %quorumdevices = ();
#
# Open the infrastructure file.
#
        if (!open(INF, "${INFRASTRUCTUREFILE}")) {
	    printf(STDERR "${FILE_ERR}", ${INFRASTRUCTUREFILE});
	    return(1);
        }
	
# We loop through the infrastructure file 
# We check to see if there are netapp_nas type quorum devices
# we grap the names of the quorum devices in the same pass.

	$netappdevs = 0;

	while (<INF>) {
	    if(/cluster.quorum_devices.\d+.properties.type\s+netapp_nas/) {
		$dev = /(d+)/;
		push(@netappquorumdevices,$dev); $netappdevs =1;
	    }
	    if(/cluster.quorum_devices.\d+.name/) {
		($dev, $device) = /cluster.quorum_devices.(\d+).name\s+(\w+)/;
		$quorumdevices{"$dev"} = $device;
	    }
	}

	close(INF);

# If they dont have netapp quroum devs, lets bail now.

 if($netappdevs == 0) { return (0); }

# Otherwise we list the devices.

print qq~
Found the following netapp_nas quorum devices configured:

~;

foreach $device (@netappquorumdevices) {
    print $quorumdevices{"$device"};
    print "\n";
}

print qq~

The patch that you have attempted to remove contains the functionality
to support netappnas quorum devices.
Thus, the patch backout will be prevented until you have removed the offending
netappnas quorum devices listed above.
Please reboot the cluster into cluster mode and remove the netappnas quorum
devices using scadm(1M) or scsetup(1M).

~;

return (-1);
 
    }
