#!/usr/bin/perl
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# 61haes_r714 src/43haes/usr/sbin/cluster/utilities/clresetiface.sh 1.6.2.1 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 2002,2009 
# 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 
# @(#)19	1.6.2.1 src/43haes/usr/sbin/cluster/utilities/clresetiface.sh, hacmp.utils, 61haes_r714 1/26/09 15:19:38
# $Id$
#
# This script is not to be run except via SMIT!
#
# clresetiface -l
#     - returns list of all IP adapters with configured interface name
#
# clresetiface -r <adapter>
#     - if cluster services are stopped, sets the interface name for
#       the named adapter to "" then instructs the user to re-synch
#       the cluster
#
# clresetiface <anything other than the two flavors above>
#     - displays usage message


#--------------------------------------------------------------------#
# Included Libraries                                                 #
#--------------------------------------------------------------------#
use locale;


#--------------------------------------------------------------------#
# Globals                                                            #
#--------------------------------------------------------------------#

# supported command-line flags
$lst_flag = "-l";    # get list of adapters with interface names
$rm_flag  = "-r";    # reset interface for adapter

# default message text
%msg_xref = (7900, "Usage:\nTo query the interface name for a boot network interface/service IP label :\n\
clresetiface -q adapter\nTo get a list of all network interfaces which have\
interfaces configured:\nclresetiface -l\
To reset the interface name for an adapter:\nclresetiface -r adapter\n",
             7901, "No communication interface label was specified.\n",
             7902, "Not able to retrieve HACMPadapter information from ODM.\n",
             7903, "Cluster services must be stopped before attempting\
to reset the network interface.\n",
             7904, "Not able to open temporary file for writing.\n",
             7905, "Not able to update configuration in ODM.\n",
             7906, "Configuration changes have been made.  Please\
re-synch the cluster configuration in order to complete\
the boot communication interface reset operation.\n");


#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
# Main                                                               #
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#

# exit if no args
if (scalar(@ARGV) == 0) {
    &usage;
    exit -1;
}

# call appropriate sub for each command line arg
if ($ARGV[0] eq $lst_flag) { exit(&if_list);  }
if ($ARGV[0] eq $rm_flag)  { exit(&if_rm);    }

# no valid args given
&usage;
exit -1;

#--------------------------------------------------------------------#
# End main                                                           #
#--------------------------------------------------------------------#


#--------------------------------------------------------------------#
#--------------------------------------------------------------------#
# Subroutines                                                        #
#--------------------------------------------------------------------#
#--------------------------------------------------------------------#


#--------------------------------------------------------------------#
# list adapters that have interface name field configured            #
#--------------------------------------------------------------------#
sub if_list
{
    my @ip_labels = ();        # array of adapter ip labels
    my $ip_label = "";         # individual ip label
    my $ip_label_out = "";     # ip label without extras
    my $rc = 0;                # return code from command

    # filter out adapters with no interface name
    @ip_labels = `odmget -q "interfacename!='' and identifier like '*.*.*.*'" HACMPadapter | grep ip_label`;

    $rc = $?;
    if ($rc) {
        # couldn't get adapter information
        system("dspmsg scripts.cat 7902 '$msg_xref{7902}'");
        exit -1;
    }

    # drop extraneous text
    foreach $ip_label (@ip_labels) {
        $ip_label =~ /.*\"(\S+)\"/;
        $ip_label_out = $1;
        print "$ip_label_out\n";
    }

    exit 0;
}


#--------------------------------------------------------------------#
# remove interface name from adapter configuration                   #
#--------------------------------------------------------------------#
sub if_rm
{

    my $tmp_name = "/tmp/clresetiface.";   # temp file name - ALWAYS
                                           # append timestamp
    my $timestamp = 0;         # timestamp - to be appended to file name
    my $tmp_file = "";         # massaged temporary file name

    # temp file text arrays
    my @ad_file    = ("HACMPadapter:\n",
                      "interfacename = \"\"\n");
    my @clstr_file = ("HACMPcluster:\n",
                      "handle = 0\n");

    my $file_line = "";        # one line in file
    my $rc = 0;                # return code from command

    # need an adapter name
    if (scalar(@ARGV) < 2) {
        # no adapter name specified
        system("dspmsg scripts.cat 7901 '$msg_xref{7901}'");
        exit -1;
    }

    # can't reset if cluster services are running
    # clcheck_server returns 1 if active, 0 otherwise
    # check for grpsvcs because clstrmgr is always active
    `/usr/es/sbin/cluster/utilities/clcheck_server grpsvcs`;

    $rc = $?;
    if($rc) {
        # services must be stopped before resetting interface
        system("dspmsg scripts.cat 7903 '$msg_xref{7903}'");
        exit -1;
    }

    my ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    $tmp_file = $tmp_name . $year . $mon . $mday . $hour . $min . $sec;

    # create and load adapter temp file
    unless (open (TMPFILE, ">$tmp_file")) {
        # unable to open temp file for writing
        system("dspmsg scripts.cat 7904 '$msg_xref{7904}'");
        exit -1;
    }

    foreach $file_line (@ad_file) {
        print TMPFILE $file_line;
    }

    close(TMPFILE);

    # nuke interface name for this adapter
    `odmchange -o HACMPadapter -q ip_label="$ARGV[1]" $tmp_file`;

    $rc = $?;

    # remove temp file
    `rm $tmp_file`;

    if ($rc) {
        # unable to change config
        system("dspmsg scripts.cat 7905 '$msg_xref{7905}'");
        exit -1;
    }

    # and repeat for cluster config change
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst) = localtime;
    $tmp_file = $tmp_name . $year . $mon . $mday . $hour . $min . $sec;

    # create and load adapter temp file
    unless (open (TMPFILE, ">$tmp_file")) {
        # unable to open temp file for writing (use $! in msg txt)
        system("dspmsg scripts.cat 7904 '$msg_xref{7904}'");
        exit -1;
    }

    foreach $file_line (@clstr_file) {
        print TMPFILE $file_line;
    }

    close(TMPFILE);

    # nuke interface name for this adapter
    `odmchange -o HACMPcluster $tmp_file`;

    $rc = $?;

    # remove temp file
    `rm $tmp_file`;

    if ($rc) {
        # unable to change config
        system("dspmsg scripts.cat 7905 '$msg_xref{7905}'");
        exit -1;
    }

    # tell user to resynch cluster then restart cluster services
    system("dspmsg scripts.cat 7906 '$msg_xref{7906}'");
    exit 0;
    
}


#--------------------------------------------------------------------#
# display usage message                                              #
#--------------------------------------------------------------------#
sub usage
{
    system("dspmsg scripts.cat 7900 '$msg_xref{7900}'");
    return 0;
}
