#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2005,2006 
# 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 
# @(#)43 1.5.1.1 src/avs/fs/mmfs/ts/admin/mmdelfileset.sh, mmfs, avs_rgpfs24, rgpfs24s007a 10/18/06 12:02:58
#########################################################################
#
# Usage:
#   mmdelfileset Device FilesetName [-f]
#
# where:
#   Device        is the device name of the file system.
#   FilesetName   is the fileset name.
#   -f            deletes the contents of the filesets and unlinks
#                   any child filesets prior to deleting the fileset.
#
#########################################################################

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

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

# Local work files.  Names should be of the form:
#   fn=${tmpDir}fn.${mmcmd}.$$

LOCAL_FILES=" "


# Local variables
usageMsg=517


# Local functions



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


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

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

# The first argument is always the file system name.
device=$arg1
deviceName=${device##+(/)dev+(/)}  # name stripped of /dev/ prefix
fqDeviceName="/dev/$deviceName"    # fully-qualified name (with /dev/ prefix)

# The second argument is always the fileset name.
filesetName=$arg2
checkName filesetName 255 "$filesetName"
[[ $? -ne 0 ]] && cleanupAndExit

# Move past the positional parameters.
shift 2

# Process the rest of the parameters.
while getopts :f OPT
do
  case $OPT in
    f) # Delete the contents of the fileset.
       [[ -n $fflag ]] && syntaxError "multiple" $noUsageMsg "-$OPT"
       fflag="-$OPT"
       ;;
    +[f]) # Invalid option.
       syntaxError "invalidOption" $usageMsg $OPT
       ;;
    :) # Missing argument.
       syntaxError "missingValue" $usageMsg $OPTARG
       ;;
    *) # Invalid option.
       syntaxError "invalidOption" $usageMsg $OPTARG
       ;;
  esac
done  # end of while getopts :t OPT do
shift OPTIND-1

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


######################################################################
# Set up trap exception handling and ensure that the local copy of
# the mmsdrfs file is up-to-date.  There is no need to lock the 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 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
fi


########################################################################
# Invoke the tsdelfileset command on the local node.
# Display any error messages and exit if any of the following are true:
#   - the command completed successfully
#   - there is an unacceptable error
#       (anything other than daemon down or quorum wait)
#   - the fileset name contains path name expansion triggers
#   - this is a single node cluster
########################################################################
$tsdelfileset $fqDeviceName "$filesetName" $fflag 2>$errMsg
rc=$(remapRC $?)
if [[ $rc -ne $MM_DaemonDown && $rc -ne $MM_QuorumWait ||
      $MMMODE = single ]]
then
  if [[ $rc -eq $MM_DaemonDown ]]
  then
    # GPFS is down on this node.
    printErrorMsg 109 $mmcmd
  elif [[ $rc -eq $MM_QuorumWait ]]
  then
    # GPFS is not ready for commands.
    printErrorMsg 110 $mmcmd
  elif [[ $rc -eq $MM_ConnectionReset ]]
  then
    # An internode connection was reset.
    printErrorMsg 257 $mmcmd
  else
    # Either the command worked, or it is an unexpected error.
    [[ -s $errMsg ]] && $cat $errMsg 1>&2
#   [[ $rc -ne 0 ]] && printErrorMsg 113 "$mmcmd" "tsdelfileset" $rc
  fi  # end of if [[ $rc -eq $MM_FsNotFound ]]
  cleanupAndExit $rc
fi  # end of if [[ ($rc -ne $MM_DaemonDown && ... ]]
$rm -f $errMsg


#####################################################################
# We come here if the local GPFS daemon is not ready for commands.
# Find an active node and send the command there.
#####################################################################

# Create a file with the reliable names of the nodes in the cluster.
getNodeList $REL_HOSTNAME_Field $GLOBAL_ID $mmsdrfsFile > $nodefile
preferredNode=$ourNodeName

# Try the nodes one by one until you find a node that can execute the command.
$mmcommon onactive $preferredNode $nodefile     \
   $NO_FILE_COPY $NO_MOUNT_CHECK NULL $NO_LINK  \
   tsdelfileset "$fqDeviceName $filesetName $fflag"
rc=$?

# if [[ $rc -ne 0 ]]
# then
#   # tsdelfileset failed.
#   printErrorMsg 104 $mmcmd tsdelfileset
#   cleanupAndExit $rc
# fi

cleanupAndExit $rc

