#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,2005 
# 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 
# @(#)71 1.27 src/avs/fs/mmfs/ts/admin/mmrefresh.sh, mmfs, avs_rgpfs24, rgpfs240610b 6/27/05 03:02:52
##############################################################################
#
#  Ensure that the GPFS system files on the specified nodes are at the most
#  recent level.  If the -f (force) option is specified, rebuild the files
#  even if they appear to be at the latest level.
#
#  Usage:
#
#    mmrefresh [-f] [-a | -N {Node[,Node...] | NodeFile | NodeClass}]
#
#  where:
#
#    -f            Rebuild the GPFS system files even if they appear
#                  to be at the most current level.
#
#    -a            Check all nodes in the GPFS cluster.
#
#    -N Node,Node,...  Specify the nodes whose state is desired.
#    -N NodeFile       NodeClass may be one of several possible node classes
#    -N NodeClass      (e.g., quorumnodes, managernodes, nsdnodes, etc.)
#
#
#  If not explicitly specified otherwise, only the local node is checked.
#
#
#  Obsolete options:
#
#   -C NodesetId  Check all nodes in the specified nodeset.
#                 Assumed to be the same as -a.
#
#   -W NodeFile   Check all nodes whose reliable hostnames
#                 are listed one per line in NodeFilename.
#                 Cannot be specified with -a, -N, -w, or -n.
#
#   -w nodenames  Check all nodes whose reliable hostnames
#                 are in the comma-separated nodenames list.
#                 Cannot be specified with -a, -N or -W.
#                 If both -w and -n are specified, the lists are combined.
#
#   -n nodenums   Check all nodes whose node numbers are
#                 in the comma-separated nodenums list.
#                 Cannot be specified with -a, -N, or -W.
#                 If both -w and -n are specified, the lists are combined.
#
##############################################################################

# Include global declarations and service routines.
. /usr/lpp/mmfs/bin/mmglobfuncs
. /usr/lpp/mmfs/bin/mmsdrfsdef
. /usr/lpp/mmfs/bin/mmfsfuncs

sourceFile="mmrefresh.sh"
[[ -n $DEBUG || -n $DEBUGmmrefresh ]] && set -x
$mmTRACE_ENTER "$*"


# Local variables

usageMsg=374
typeset -i nodes=0
rc=0
nodenames=""
nodenums=""
Wcoll=""
fflag=""
aflag=""
Cflag=""
nflag=""
Nflag=""
Wflag=""
wflag=""
integer nodeCount=0



#######################
# Mainline processing
#######################


##################################
# Process each of the arguments.
##################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] &&  \
  syntaxError "help" $usageMsg

while getopts :aC:fn:N:w:W: OPT
do
  case $OPT in

    a) [[ -n $aflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       aflag="-$OPT"
       all="all"
       [[ -n $Cflag || -n $Wflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $aflag $Cflag $Wflag $wflag $nflag $Nflag
       ;;

    C) # syntaxError "obsoleteOption" $usageMsg "-$OPT"
       [[ -n $Cflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Cflag="-$OPT"
       nodesetId="$OPTARG"
       [[ -n $aflag || -n $Wflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Cflag $aflag $Wflag $wflag $nflag $Nflag
       ;;

    f) [[ -n $fflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       fflag="-$OPT"
       ;;

    n) [[ -n $nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       nflag="-$OPT"
       nodenums="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $nflag $Cflag $Wflag $aflag $Nflag
       ;;

    N) [[ -n $Nflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Nflag="-$OPT"
       nodenames="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $nflag || -n $wflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Nflag $Cflag $Wflag $aflag $nflag $wflag
       ;;

    w) [[ -n $wflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       wflag="-$OPT"
       nodenames="$OPTARG"
       [[ -n $Cflag || -n $Wflag || -n $aflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $wflag $Cflag $Wflag $aflag $Nflag
       ;;

    W) [[ -n $Wflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       Wflag="-$OPT"
       Wcoll="$OPTARG"
       [[ -n $Cflag || -n $aflag || -n $wflag || -n $nflag || -n $Nflag ]] &&  \
         syntaxError "invalidCombination"  \
           $usageMsg $Wflag $Cflag $aflag $wflag $nflag $Nflag
       ;;

    :) syntaxError "missingValue" $usageMsg $OPTARG
       ;;

    +[aCfnNwW])
       syntaxError "invalidOption" $usageMsg "$OPT"
       ;;

    *) syntaxError "invalidOption" $usageMsg $OPTARG
       ;;

  esac
done  # end of while getopts :aC:fn:N:w:W: OPT do

shift OPTIND-1
[[ $# != 0 ]] && syntaxError "extraArg" $usageMsg $1

# Rather than fail, convert the obsolete -C option to -a.
[[ -n $Cflag ]] && aflag="-a"


########################################################################
# Set up trap exception handling and call the gpfsInit function.
# It will ensure that the local copy of the mmsdrfs and the rest of the
# GPFS system files are up-to-date.  There is no need to lock the sdr.
########################################################################
trap pretrap2 HUP INT QUIT KILL
gpfsInitOutput=$(gpfsInit nolock)
setGlobalVar $? $gpfsInitOutput


#######################################################
# Create a file containing all of the specified nodes.
#######################################################
if [[ -n $aflag ]]
then
  # Get a list of the nodes.
  getNodeList $REL_HOSTNAME_Field $GLOBAL_ID $mmsdrfsFile > $nodefile

elif [[ -n $Nflag ]]
then
  # Convert the passed data into a file containing admin node names.
  createVerifiedNodefile $nodenames $REL_HOSTNAME_Field no $nodefile
  [[ $? -ne 0 ]] && cleanupAndExit

elif [[ -n $Wflag ]]
then
  # Verify the input file is readable.
  if [[ ! -f $Wcoll || ! -r $Wcoll ]]
  then
    printErrorMsg 43 $mmcmd $Wcoll
    cleanupAndExit
  fi

  # Filter out comment lines and localhost entries.
  $grep -v -e "localhost" -e "^#" "$Wcoll" > $tmpfile

  # Convert any entries in the node file into admin node names.
  if [[ -s $tmpfile ]]
  then
    createVerifiedNodefile $tmpfile $REL_HOSTNAME_Field no $nodefile
    [[ $? -ne 0 ]] && cleanupAndExit
  else
    # No node names were specified.
    printErrorMsg 328 $mmcmd $Wcoll
    cleanupAndExit
  fi

else
  # Either no option was specified, or we have some combination of -w and -n.

  # Convert the node names list (if any) into a file.
  $rm -f $tmpfile
  if [[ -n $nodenames ]]
  then
    for i in $(print $nodenames | $tr "," " ")
    do
      print -- "$i" >> $tmpfile
    done
  fi

  # Append the node number list (if any) to the node file.
  if [[ -n $nodenums ]]
  then
    for i in $(print $nodenums | $tr "," " ")
    do
      print -- "$i" >> $tmpfile
    done
  fi

  # Convert the entries in the node file into admin node names.
  if [[ -s $tmpfile ]]
  then
    createVerifiedNodefile $tmpfile $REL_HOSTNAME_Field no $nodefile
    [[ $? -ne 0 ]] && cleanupAndExit
  fi

fi  # end of if [[ -n $aflag ]]


# Ensure we have the proper credentials.
[[ $getCredCalled = no ]] && getCred


##########################
# Refresh the GPFS nodes.
##########################
if [[ ! -s $nodefile ]]
then
  # This is a request to refresh only the local node.
  # Unless -f is specified, this is already done by gpfsInit above.
  if [[ -n $fflag ]]
  then
    $mmremote cfg $fflag
    rc=$?
  else
    : # There is nothing else to do.  If there is a need for
      # additional processing that is not handled by gpfsInint,
      # invoke the appropriate function.
  fi
else
  # This is a request to refresh other nodes as well.
  $mmcommon onall $nodefile $unreachedNodes cfg $fflag
  rc=$?
fi  # end of if [[ ! -s $nodefile ]]

# If any nodes could not be reached, tell the user which ones.
if [[ -s $unreachedNodes ]]
then
  # The following nodes could not be reached: . . .
  printErrorMsg 270 $mmcmd
  $cat $unreachedNodes 1>&2
fi

cleanupAndExit $rc

