#!/bin/ksh
#
# ident "@(#)utumount.ksh	1.8	04/06/25 SMI"
#
# Copyright 2004 Sun Microsystems, Inc.  All Rights Reserved.
# Use is subject to license terms.
#
# utumount:
# Sun Ray disk unmount utility
# This is just a wrapper for utdiskadm -u
#


PATH="/bin"

# flags that are set while parsing command line
ARG_MNTPATH=""

# only one operation allowed (see set_opt)
OPERATION=""

# print usage message on stderr
# $1 = program name
# $2 = if set, send output to stdout (default is stderr)
#
function showusage
{
	if [[ -n $2 ]] then
		# if $2 is set, send output to stdout
		OUT=""
	else
		# else send output to stderr
		OUT="-u2"
	fi
	print $OUT "usage:"
	print $OUT "\t $1 -h"
	print $OUT "\t $1 -u mount_path"
}


# print help message
# $1 = program name
#
function showhelp
{
	# show usage message on stdout
	showusage $1 "1"

	# explain options
	print "Options:"
	print " -h	# print this help message"
	print " -u	# unmount mount_path"
}


# only one operation allowed as command line option
# check whether caller has issued multiple options
# $1 = option
#
function set_op
{
	if [[ -z $OPERATION ]] then
		OPERATION=$1
	elif [[ $OPERATION != $1 ]] then
		print -u2 "conflicting options  -${OPERATION} and -${1}"
		showusage $PROGNAME
		exit 1
	fi
}


# main
#

PROGNAME="`basename $0`"

# setup global variables

# Sun Ray paths
UTDISKADMCMD="/etc/opt/SUNWut/basedir/bin/utdiskadm"

# parse options
#
while getopts :hu: vopts
do
	case $vopts in
	h)	set_op "h"
		;;
	u)	set_op "u"
		ARG_MNTPATH="$OPTARG"
		;;
	\?) print -u2 "$PROGNAME: Invalid option -$OPTARG"
		showusage $PROGNAME
		exit 1
		;;
	:) print -u2 "$PROGNAME: Required argument missing"
		showusage $PROGNAME
		exit 1
		;;
	esac
done

# reset param numbering to catch junk args
shift `expr $OPTIND - 1`

if [[ $# -gt 0 ]] then
	print -u2 "$PROGNAME: extra arguments found"
	print -u2 "\tArguments: $*"
	showusage $PROGNAME
	exit 1
fi

# execute command
case $OPERATION in
"")	print -u2 "$PROGNAME: option required"
	showusage $PROGNAME
	exit 1
	;;
h)	showhelp $PROGNAME
	;;
u)	$UTDISKADMCMD -u $ARG_MNTPATH
	;;
esac

exit $?

