#!/bin/bsh
# IBM_PROLOG_BEGIN_TAG 
# This is an automatically generated prolog. 
#  
# onc720 src/oncplus/usr/sbin/sysmgt/lsnfsmnt.sh 1.3.1.10 
#  
#                  
#  
# OBJECT CODE ONLY SOURCE MATERIALS 
#  
# COPYRIGHT International Business Machines Corp. 1996,2009 
# All Rights Reserved 
#  
# The source code for this program is not published or otherwise 
# divested of its trade secrets, irrespective of what has been 
# deposited with the U.S. Copyright Office. 
#  
# IBM_PROLOG_END_TAG 
#
# @(#)79        1.3.1.10  src/oncplus/usr/sbin/sysmgt/lsnfsmnt.sh, onccmds, onc720 3/9/09 09:44:44
#
# COMPONENT_NAME: (CMDNFS) Network File System Commands
#
# FUNCTIONS: local_lsfs()
#
# ORIGINS: 27
#
# US Government Users Restricted Rights - Use, duplication or
# disclosure restricted by GSA ADP Schedule Contract with IBM Corp.
#
#
set -f			# this is so that the case statement will work

PATH=/usr/bin:/usr/sbin:$PATH
export PATH

PG=`basename $0`
TMPDIR=${TMPDIR:-$HOME/tmp}
[ ! -d $TMPDIR ] && TMPDIR=/tmp
TMPDIR=$TMPDIR/$PG.$$

mkdir $TMPDIR || {
	echo "$PG: Could not create temporary files"
	exit 1
}

# Variables used just within this script
TMP_FILE=$TMPDIR/lsnfsmnt.$$
TMP_FILE_2=$TMPDIR/lsnfsmnt_2.$$
COMMAND_NAME=`basename $0`
TR=/usr/bin/tr
LSFS=/usr/sbin/lsfs
EGREP=/bin/egrep
AWK=/bin/awk

# Flag to enable/disable extended output
# Extended output is used by other commands and may change
EXTENDED=0

# To pick YESSTR in the language as defined by the current locale
YESSTR=`/usr/bin/locale yesexpr`
YESSTR=${YESSTR:-"yes"}

USAGE="usage: ${COMMAND_NAME}  [-c | -l | -p ] [directory]\n"

set -- `getopt plce $*  2>/dev/null`

if [ $? != 0 ] ; then
	dspmsg cmdnfs.cat -s 37 61 "${USAGE}" $COMMAND_NAME
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
fi

# Function that will take an "lsfs -c" output and convert colons to
# pipes except where a dos like pathname exists (ie: Device=c:\games\)
local_lsfs()
{
	if [ -n "${DIRECTORY}" ]; then
	  ${LSFS} -c "${DIRECTORY}" | ${AWK} 'BEGIN {FS=":"}
	  {
	    if (NF == 10)
	      {printf "%s|%s:%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}
	    else
	      {printf "%s|%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,$5,$6,$7,$8}
	  }'
	else
	  ${LSFS} -c | ${AWK} 'BEGIN {FS=":"}
	  {
	    if (NF == 10)
	      {printf "%s|%s:%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,$5,$6,$7,$8,$9}
	    else
	      {printf "%s|%s|%s|%s|%s|%s|%s|%s\n",$1,$2,$3,$4,$5,$6,$7,$8}
	  }'
	fi
}

# Find out how we are supposed to print the information to the user.

while [ $1 != -- ]
do
    case "$1" in
	"-p")			#
	    if [ -n "${FORMAT}" ] ; then
		dspmsg cmdnfs.cat -s 37 61 "$USAGE" $COMMAND_NAME;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    FORMAT="pipe"
	    FSEP="|"
	    shift
	    ;;
	"-c")			#
	    if [ -n "${FORMAT}" ] ; then
		dspmsg cmdnfs.cat -s 37 61 "$USAGE" $COMMAND_NAME;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    FORMAT="colon"
	    FSEP=":"
	    shift
	    ;;
	"-l")			#
	    if [ -n "${FORMAT}" ] ; then
		dspmsg cmdnfs.cat -s 37 61 "$USAGE" $COMMAND_NAME;
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 1 ;
	    fi
	    FORMAT="list"
	    shift
	    ;;
	"-e")			# enable extended output for -c and -p output
	    EXTENDED=1
	    shift
	    ;;
	*)
	    dspmsg cmdnfs.cat -s 37 61 "${USAGE}" $COMMAND_NAME
	    trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	    exit 1
	    shift
	    ;;
    esac
done

shift 		# get rid of the -- parameter
if [ $# -gt 1 ] ; then
	dspmsg cmdnfs.cat -s 37 61 "${USAGE}" $COMMAND_NAME
	trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
	exit 1
elif [ $# -eq 1 ] ; then
	DIRECTORY=$1
fi


# Find the information that we are supposed to print to the user.

if [ -n "$DIRECTORY" ] ; then
	if [ "${FORMAT}" = "colon" -o "${FORMAT}" = "pipe" ] ; then
		local_lsfs | ${AWK} 'BEGIN{ FS="|" } ; { if ($3 == "nfs" ) print ($0)}' | sed s/'\\'/\\\\\\\\/g > ${TMP_FILE} 2>/dev/null
                if [ ! -s ${TMP_FILE} ] ; then
		    trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		    exit 1
		fi
	else
		# There is nothing to do if the request is for list format
		
		${LSFS} -v nfs ${DIRECTORY} > ${TMP_FILE} 2> /dev/null
		if [ $? -eq 0 ] ; then
			cat -q ${TMP_FILE} | ${AWK} -v VAR=${DIRECTORY} '{ if ($3 == VAR) { print $0 }}' > ${TMP_FILE_2}
                	if [ ! -s ${TMP_FILE_2} ] ; then
				dspmsg cmdnfs.cat -s 52 4 "%s: %s not found.\n" $COMMAND_NAME ${DIRECTORY}
		    		RC=1
			else
				cat -q ${TMP_FILE} | ${AWK} '{ if (NR == 1) { print $0 }}'
				cat -q ${TMP_FILE_2}
				RC=0
			fi
		else
			dspmsg cmdnfs.cat -s 52 4 "%s: %s not found.\n" $COMMAND_NAME ${DIRECTORY}
			RC=1
		fi

		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit ${RC}

	fi
else
	if [ "${FORMAT}" = "colon" -o "${FORMAT}" = "pipe" ] ; then
		local_lsfs | ${AWK} 'BEGIN {FS="|"};{ if (NR != 1 && $3 == "nfs"q) {print $0}}' | sed s/'\\'/\\\\\\\\/g > ${TMP_FILE} 2>/dev/null
	else
		# There is nothing to do if the request is for list format
		${LSFS} | ${AWK} '{ if ((NR == 1) || ($4 == "nfs")) { print $0}}'
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit $?
	fi
fi
# We have to do a little of our own formatting here.
# The rest of the script expects something for each of the options
# listed by the lsfs command.  This awk script fills in any null options.

${AWK} 'BEGIN {FS="|"} ; {\
	if ($1 == "") { printf "_null " }else{printf "%s ", $1} ;\
	if ($2 == "") { printf "_null " }else{printf "%s ", $2} ;\
	if ($3 == "") { printf "_null " }else{printf "%s ", $3} ;\
	if ($4 == "") { printf "_null " }else{printf "%s ", $4} ;\
	if ($5 == "") { printf "_null " }else{printf "%s ", $5} ;\
	if ($6 == "") { printf "_null " }else{printf "%s ", $6} ;\
	if ($7 == "") { printf "_null " }else{printf "%s ", $7} ;\
	if ($8 == "") { printf "_null " }else{printf "%s\n", $8} }'\
	${TMP_FILE} | sed s/'\\'/\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\/g > ${TMP_FILE_2}

# Process the listed mount information so that we
# can correctly print the options for the user.
echo "#mountpoint${FSEP}remotepath${FSEP}nodename${FSEP}type${FSEP}readtype${FSEP}when${FSEP}softhard${FSEP}secure${FSEP}intr${FSEP}retry${FSEP}rsize${FSEP}wsize${FSEP}timeo${FSEP}port${FSEP}acregmin${FSEP}acregmax${FSEP}acdirmin${FSEP}acdirmax${FSEP}actimeo${FSEP}biods${FSEP}suid${FSEP}dev${FSEP}shortdev${FSEP}auto${FSEP}vers${FSEP}proto${FSEP}llock${FSEP}acl${FSEP}grpid${FSEP}posix${FSEP}retrans\c"
if [ $EXTENDED -eq 1 ]; then
	echo "${FSEP}maxgroups"
else
	echo
fi

while [ 1 ]
do

	# Set up the defaults for the export
	mountpoint=
 	device=
	vfs=
	nodename=
	type=
	size=
	options=
	automount=

	# Get the export line that is in the temporary file.
	read inputline
	if [ $? != 0 ] ; then
		trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15
		exit 0
	fi

	# set the arguments so that we can treat them like command parms
	set -- $inputline  2>/dev/null

	mountpoint=$1
 	device=$2
	vfs=$3
	nodename=$4
	if [ "$5" = "_null" ] ; then  type="" ; else type=$5 ; fi
	size=$6
	options=$7
	automount=$8
	`echo $automount | ${EGREP} "$YESSTR" >/dev/null 2>&1`
	if [ $? -eq 0 ]; then
           automount="-A"
        else
           automount="-a"
        fi
	# Defaults for all of the options that may be specified.
	read_only_read_write="rw"
	foreground_background="fg"
	soft_hard="-H"
	secure="sys"
	interrupts="-e"
	llock="-l"
	acl="-j"
	grpid="-g"
	posix="-q"
	retry=
	retrans=
	rsize=
	wsize=
	timeo=
	port=
	acregmin=
	acregmax=
	acdirmin=
	acdirmax=
	actimeo=
	biods=
	suid="-Y"
	dev="-Z"
	shortdev="-X"
	vers=
	proto=
	maxgroups=

	# Parse the options list so that they may be printed correctly
	for option in `echo $options | ${TR} ',' ' '`
	do
	    case "$option" in
		rw)
			read_only_read_write=$option
			;;
		ro)
			read_only_read_write=$option
			;;
		fg)
			foreground_background=$option
			;;
		bg)
			foreground_background=$option
			;;
		soft)
			soft_hard="-S"
			;;
		hard)
			soft_hard="-H"
			;;
		secure)
			secure="dh"
			;;
		sec=*)
			secure=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}' | ${TR} '+' ','`
			;;
		intr)
			interrupts="-E"
			;;
		llock)
			llock="-L"
			;;
		acl)
			acl="-J"
			;;
		grpid)
			grpid="-G"
			;;
		posix)
			posix="-Q"
			;;
		retry*)
			retry=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		retrans*)
			retrans=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		rsize*)
			rsize=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		wsize*)
			wsize=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		timeo*)
			timeo=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		vers*)
			vers=`echo $option | ${TR} '=' ' ' | ${AWK} '{print $2}'`
			;;

		proto*)
			proto=`echo $option | ${TR} '=' ' ' | ${AWK} '{print $2}'`
			;;

		port*)
			port=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		acregmin*)
			acregmin=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		acregmax*)
			acregmax=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		acdirmin*)
			acdirmin=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		acdirmax*)
			acdirmax=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		actimeo*)
			actimeo=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		biods*)
			biods=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		nosuid)
			suid="-y"
			;;
		nodev)
			dev="-z"
			;;
		shortdev)
			shortdev="-x"
			;;
		maxgroups*)
			maxgroups=`echo $option | ${TR} '=' ' ' |${AWK} '{print $2}'`
			;;
		*)
			;;
	    esac
	done

	# SMIT-ESCape a colon if it exists in remote device name
	device=`echo $device | sed s/:/#!:/g` 

if [ "x$vers" = "x" ] ; then
    vers="any"
fi

if [ "x$proto" = "x" ] ; then
    proto="any"
fi


	echo "$mountpoint$FSEP$device$FSEP$nodename$FSEP$type$FSEP$read_only_read_write$FSEP$foreground_background$FSEP$soft_hard$FSEP$secure$FSEP$interrupts$FSEP$retry$FSEP$rsize$FSEP$wsize$FSEP$timeo$FSEP$port$FSEP$acregmin$FSEP$acregmax$FSEP$acdirmin$FSEP$acdirmax$FSEP$actimeo$FSEP$biods$FSEP$suid$FSEP$dev$FSEP$shortdev$FSEP$automount$FSEP$vers$FSEP$proto$FSEP$llock$FSEP$acl$FSEP$grpid$FSEP$posix$FSEP$retrans\c"
if [ $EXTENDED -eq 1 ]; then
	echo "$FSEP$maxgroups"
else
	echo
fi

done < 	${TMP_FILE_2}

# remove temporary dir
trap '/bin/rm -rf $TMPDIR 2>/dev/null' 0 1 2 3 15

exit 0