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

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

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

usageMsg=371
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 the 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 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


###########################################################
# 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


############################################################################
# We come here if this is a local filesystem.  Invoke the command directly.
############################################################################
${mmcmdDir}/${links}/mmcrsnapshot $fqDeviceName $argList
rc=$(remapRC $?)

# If acceptable error (daemon not running, waiting for quorum),
# try some other node.  Otherwise, get out; either the command worked
# or the errors are not acceptable.
if [[ $rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait ]]
then
  if [[ $rc -eq $MM_ConnectionReset ]]
  then
    # An internode connection was reset.
    printErrorMsg 257 $mmcmd
  fi
  cleanupAndExit $rc
fi


###################################################
# We come here if the daemon was not up.
# Find an active node and send the command there.
###################################################

# 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 mmcrsnapshot $fqDeviceName $argList
rc=$?

cleanupAndExit $rc

