#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
#  
#  
# Licensed Materials - Property of IBM 
#  
# (C) COPYRIGHT International Business Machines Corp. 2000,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 
# @(#)10 1.28 src/avs/fs/mmfs/ts/admin/mmfsenv.sh, mmfs, avs_rgpfs24, rgpfs240610b 10/26/05 19:20:30
#######################################################################
#
# mmfsenv is used to establish the environment and to load
# the kernel extensions for GPFS and its support programs.
#
#  Usage:
#    mmfsenv [-r | -u]  [other_options]
#
#  where
#    -r             unload the current extensions first.
#    -u             unload the kernel extensions and exit.
#    other_options  all other options are passed through to
#                   the load and unload routines.
#
#######################################################################
#
# This script assumes that the GPFS daemon is running in the context
# of the mm command environment and performs a series of checks to
# assure that this environment is correct and that all other needed
# subsystems are present and operational.  If this behavior is not
# desired, because GPFS is running in a single node or controlled
# test environment, set the environment variable MMFSBYPASSCFG.
#
#######################################################################

# Save the command line options before doing anything else.
opts=$@

#######################################################################
# If this has not been done yet by the caller, determine the pathname
# of the mm commands directory and include the global declarations.
#######################################################################
if [[ $commandPath != set ]]
then
  # Decide where to look for the commands.  The normal install directory
  # is /usr/lpp/mmfs/bin.  This can be changed by setting the MMFSDIR
  # environment variable to something other than /usr/lpp/mmfs.
  # If MMFSDIR is not used, before assuming the default install directory,
  # we will check if the mmfs binaries are in the same place where this
  # script resides.  This is a development aid for running out of a build tree.
  if [[ -n $MMFSDIR ]]
  then
    mmcmdDir="${MMFSDIR}/bin"
  else
    if [[ -z ${0%%/*} ]]
    then
      fullname="$0"
    else
      fullname="${PWD%/}/$0"
    fi
    progDir=${fullname%/*}

    if [[ -f ${progDir}/mmfs ]]
    then
      mmcmdDir="$progDir"
    else
      mmcmdDir="/usr/lpp/mmfs/bin"
    fi
  fi

  # Determine the values of mmcmdSubdir and mmcmdSuffix.
  set -f ; set -- $(/bin/uname -a) ; set +f
  osName=$1
  osVersion=$4
  set --
  if [[ -f ${mmcmdDir}/mmfs || $osName != AIX || $osVersion < 5 ]]
  then
    mmcmdSubdir=""
    mmcmdSuffix=""

  else
    # Must be running on top of AIX 5.
    # Determine whether the current kernel is 64-bit.
    kernelMode=$(${mmcmdDir}/mmkerninfo)
    if [[ $kernelMode = 64 ]]
    then
      mmcmdSubdir="aix64"
      mmcmdSuffix="64"
    else
      mmcmdSubdir="aix32"
      mmcmdSuffix=""
    fi
  fi

  # Include global declarations and service routines.
  . ${mmcmdDir}/mmglobfuncs
  . ${mmcmdDir}/mmsdrfsdef
  . ${mmcmdDir}/mmfsfuncs

  commandPath=set

fi  # end of if [[ $commandPath != set ]]

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

# Set up trap exception handling.
trap pretrap2 HUP INT QUIT KILL

# Separate -r and -u from the rest of the options.
myopt=""
extopt=""
for opt in $opts
do
  if [[ $opt = "-r" || $opt = "-u" ]]
  then
    myopt=$opt
  else
    extopt="$extopt $opt"
  fi
done

# If Linux, make sure the extension is reloaded.
[[ $osName = Linux && -z $myopt ]] && myopt="-r"

# Add the GPFS command directory to the path.
PATH="$PATH:$mmcmdDir:"

# If requested, unload the kernel extensions.
if [[ $myopt = "-r" || $myopt = "-u" ]]
then
  # If Linux, first try to unmount left over file systems.
  [[ $osName = Linux ]] && $umount -a -t gpfs

  # Unload the extensions.
  unloadKernelExt $myopt
  rc=$?

  # If Linux, try to bring the daemon up even if the unloadKernExt failed.
  if [[ $osName = Linux && $rc -ne 0 ]]
  then
    if [[ $myopt = "-u" ]]
    then
      rc=$MM_KExtFailure
    else
      rc=0
    fi
  fi

  # If nothing else to do, or if an unrecoverable error, get out.
  if [[ $myopt = "-u" || $rc -ne 0 ]]
  then
    $mmTRACE_EXIT "rc=$rc after unloadKernelExt "
    return $rc
  fi
fi

# Load the mmfs extension.
loadKernelExt $extopt
rc=$?
[[ $osName = Linux && $rc -ne 0 ]] && rc=$MM_KExtFailure
if [[ $rc -ne 0 ]]
then
  $mmTRACE_EXIT "rc=$rc loadKernelExt failure"
  return $rc
fi

# If MMFSBYPASSCFG is set, do not do configuration initialization.
# This is used when running without the mm commands and repository services.
if [[ -n $MMFSBYPASSCFG ]]
then
  $mmTRACE_EXIT "rc=0 mmchecksubsys bypassed"
  return 0
fi

# Verify and update the mmfs environment.
. $mmchecksubsys
rc=$?
sourceFile="mmfsenv.sh"  # Restore value after in-line call to mmchecksubsys.

$rm -f $GLOBAL_FILES $LOCAL_FILES
$mmTRACE_EXIT "rc=$rc"
return $rc

