#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2001,2004 
# 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 
# @(#)73 1.13 src/avs/fs/mmfs/ts/admin/mmrestorefs.sh, mmfs, avs_rgpfs24, rgpfs240610b 11/15/04 02:35:50
#######################################################################
#
#  Usage:  mmrestorefs Device Directory
#
#######################################################################

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

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

usageMsg=419
integer rc=0
integer nodeCount=0


#####################################################################
# Process the command arguments.
# The detailed syntax checking is left for the ts command.
# Here we make sure that we have the minimum needed to continue.
#####################################################################
[[ $arg1 = '-?' || $arg1 = '-h' || $arg1 = '--help' || $arg1 = '--' ]] &&  \
  syntaxError "help" $usageMsg

[[ $argc -lt 1  ]] &&  \
  syntaxError "missingArgs" $usageMsg

device=$arg1     # Save stripe group device (always the first parameter).
shift 1          # Drop the device name from the parameter list.
argList=$@       # Save the remainder of the argument list.


#####################################################################
# Set up trap exception handling and ensure that the local copy of
# the mmsdrfs is up-to-date.  There is no need to lock mmsdrfs file.
#####################################################################
trap pretrap2 HUP INT QUIT KILL
gpfsInitOutput=$(gpfsInit nolock)
setGlobalVar $? $gpfsInitOutput


###########################################################
# Make sure the specified file system exists and is local.
###########################################################
findFSoutput=$(findFS "$device" $mmsdrfsFile)
[[ -z $findFSoutput ]] && cleanupAndExit

# Parse the output from the findFS function.
set -f ; set -- $findFSoutput ; set +f
fqDeviceName=$1
deviceName=$2
fsHomeCluster=$3

# Exit with a message if the command was invoked for a remote file system.
if [[ $fsHomeCluster != $HOME_CLUSTER ]]
then
  # Command is not allowed for remote file systems.
  printErrorMsg 106 $mmcmd $device $fsHomeCluster
  cleanupAndExit 1
fi


##########################################################
# If the cluster to which this node belongs is the same
# as the cluster in which the file system resides,
# invoke the command directly.
##########################################################
if [[ $nsId = $fsHomeCluster ]]
then
  ${mmcmdDir}/${links}/mmrestorefs $fqDeviceName $argList 2>$errMsg
  rc=$(remapRC $?)

  # If the command completed successfully, or if there is an
  # unacceptable error, display any error messages and get out.
  if [[ $rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait ]]
  then
    [[ -s $errMsg ]] && $cat $errMsg 1>&2
    if [[ $rc -eq $MM_ConnectionReset ]]
    then
      # An internode connection was reset.
      printErrorMsg 257 $mmcmd
    fi
    cleanupAndExit $rc
  fi
fi
$rm -f $errMsg


############################################################################
# If the local daemon is not available, send the command to an active node.
############################################################################

# Create a file with the reliable names that form the
# cluster to which the file system belongs.
nodeCount=$(getNodeFile $REL_HOSTNAME_Field $fsHomeCluster $mmsdrfsFile $nodefile)
if [[ $nodeCount -eq 0 ]]
then
  # The cluster is empty; there is nobody to run the command.
  printErrorMsg 171 $mmcmd "getNodeFile (nodeCount=0)" 1
  cleanupAndExit
fi

# Try the nodes one by one until you find a node that can execute the command.
preferredNode=0     # We have no idea where to go first; let mmcommon decide.
$mmcommon linkCommand $preferredNode $nodefile mmrestorefs $fqDeviceName $argList
rc=$?

cleanupAndExit $rc

