#!/bin/ksh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# bos720 src/bos/usr/lib/nim/methods/c_fs.sh 1.3.1.4 
#  
# Licensed Materials - Property of IBM 
#  
# Restricted Materials of IBM 
#  
# COPYRIGHT International Business Machines Corp. 1995,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 
# @(#)68	1.3.1.4  src/bos/usr/lib/nim/methods/c_fs.sh, cmdnim, bos720 9/15/06 15:49:06
#
#   COMPONENT_NAME: cmdnim
#
#   FUNCTIONS: create_fs
#		get_vg_size
#		list_vg
#		make_dir
#		remove_fs
#
#   ORIGINS: 27
#
#
#   (C) COPYRIGHT International Business Machines Corp. 1995
#   All Rights Reserved
#   Licensed Materials - Property of IBM
#   US Government Users Restricted Rights - Use, duplication or
#   disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#

# include common NIM shell defines/functions
NIMPATH=${0%/*}
NIMPATH=${NIMPATH%/*}
[[ ${NIMPATH} = ${0} ]] && NIMPATH=/usr/lpp/bos.sysmgt/nim
NIM_METHODS="${NIMPATH}/methods"
export NIMPATH NIM_METHODS
. ${NIM_METHODS}/c_sh_lib

# used to identify the filesystem type
NIMVFS=/etc/vfs

#############################################################################
#
# Name:        list_vg
#
# Function:    Lists the volume groups defined on a machine.
#
# Parameters:  None.
#
# Returns:     Returns the return code from the call to lsvg.
#
#############################################################################
list_vg ()
{
/usr/sbin/lsvg

return $?
} #end list_vg

#############################################################################
#
# Name:       get_vg_size
#
# Function:   Prints the amount of space available in a volume group.
#
# Parmeters:  None.  Uses the global VG_NAME.
#
# Returns:    Nothing.
#
#############################################################################
get_vg_size ()
{

# Save the current language.  We need to set it temporarily to C so that
# the grep will work.
ORIGLANG=$LANG
export LANG=C

FREESPACE=`/usr/sbin/lsvg $VG_NAME | ${GREP} FREE | ${AWK} '{print $7}' | \
           ${AWK} 'BEGIN {FS="("} {print $2}'`

print $FREESPACE

# Change the language back to what it was previously.
export LANG=$ORIGLANG
return
}

#############################################################################
#
# Name:       create_fs
#
# Function:   Creates and mounts a filesystem.
#
# Parmeters:  None.  Uses the globals FS_NAME, VG_NAME, FS_SIZE
#
# Returns:    Nothing.
#
#############################################################################
create_fs ()
{

${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_FS} 'Creating the %s filesystem.\n' ${FS_NAME}

print

#
#  If the filesystem already exists, then return an error.
#
${LSFS} $FS_NAME > /dev/null 2>&1
if [[ $? -eq 0 ]]
then
  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_FS_EXISTS} 'The %s filesystem already exists.  It must be removed\nbefore a new filesystem can be created with the same\nmount point.\n' ${FS_NAME}

  print

  return -1
fi

#
#  Create the filesystem.
#
# obtain the file system type
FS_TYPE=`${AWK} '$1 ~ /\%defaultvfs/ {print $2}' $NIMVFS`

if [[ ${FS_TYPE} = "jfs2" ]]
then
   /usr/sbin/crfs -v $FS_TYPE -g $VG_NAME -a size=$((2048*$FS_SIZE)) -m $FS_NAME  \
     -A yes -p rw -t no -a agblksize=4096 2>&1
else
   /usr/sbin/crfs -v $FS_TYPE -a bf=${BF_FLAG:-false} -g $VG_NAME -a size=$((2048*$FS_SIZE)) -m $FS_NAME  \
     -A yes -p rw -t no -a frag=4096 -a nbpi=4096 -a compress=no 2>&1
fi

if [[ $? -ne 0 ]]
then
  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_FS_CREATE_FAILED} 'Creation of the %s filesystem failed.\n' ${FS_NAME}

  print
  return -1
fi

print

${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_MOUNTING_FS} 'Mounting the %s filesystem.\n' ${FS_NAME}

print

#
#  Mount the filesystem that was just created.
#
${MOUNT} $FS_NAME 2>&1
if [[ $? -ne 0 ]]
then
  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_MOUNT_FS_FAILED} 'Unable to mount the %s filesystem.\n' ${FS_NAME}

  print
  return -1
fi

return 0
} #end create_fs

#############################################################################
#
# Name:       remove_fs
#
# Function:   Removes a filesystem.
#
# Parmeters:  None.  Uses the global FS_NAME.
#
# Returns:    Returns the return code from the rmfs command.
#
#############################################################################
remove_fs ()
{

${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_UNMOUNTING_FS} 'Unmounting the %s filesystem.\n' ${FS_NAME}
${UNMOUNT} $FS_NAME 2>&1

print

${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_C_REMOVING_FS} 'Removing the %s filesystem and mount point.\n' ${FS_NAME}

print

/usr/sbin/rmfs -r $FS_NAME 2>&1
RM_FS_RC=$?

if [[ $RM_FS_RC -ne 0 ]]
then
  ${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_REMOVE_FS_FAILED} 'Removal of the %s filesystem failed.\n' ${FS_NAME}
  print
fi

return $RM_FS_RC
} #end remove_fs

#############################################################################
#
# Name:       make_dir
#
# Function:   Creates a directory.
#
# Parmeters:  None.  Uses the global DIR_NAME.
#
# Returns:    Returns the result of the mkdir command.
#
#############################################################################
make_dir ()
{
${DSPMSG} -s ${MSG_SET} cmdnim.cat ${MSG_C_CREATING_DIR} 'Creating the %s directory.\n' ${DIR_NAME}

print

${MKDIR} -p $DIR_NAME

return $?
}

############################# Begin Main Program ############################

#
#  No syntax checking is performed since this command should never be
#  called by a customer directly.  Verification of the parameters will
#  occur on the calling side.
#
while getopts crlzbn:v:s:d: option
do
  case $option in
    c) FUNC="create_fs";;
    r) FUNC="remove_fs";;
    l) FUNC="list_vg";;
    z) FUNC="get_vg_size";;
	b) BF_FLAG="true";;
    n) FS_NAME=$OPTARG;;
    v) VG_NAME=$OPTARG;;
    s) FS_SIZE=$OPTARG;;
    d) DIR_NAME=$OPTARG
       FUNC="make_dir";;
  esac
done

$FUNC

exit $?
