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


PATH="/bin"

# flags that are set while parsing command line
ARG_MNTDEV=""
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 -m partition_name [ -p 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 " -m	# mount partition_name on default mount_point"
	print " -p	# (with -m) mount partition_name on given 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 :hm:p: vopts
do
	case $vopts in
	h)	set_op "h"
		;;
	m)	set_op "m"
		ARG_MNTDEV="$OPTARG"
		;;
	p)	if [[ -z $ARG_MNTPATH ]] then
			ARG_MNTPATH="$OPTARG"
		else
			print -u2 "only one instance of -p is allowed"
			showusage $PROGNAME
			exit 1
		fi
		;;
	\?) 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

# check for illegal option combinations
case $OPERATION in
"")	print -u2 "$PROGNAME: option required"
	showusage $PROGNAME
	exit 1
	;;
h)	# optional -p not allowed
	if [[ -n $ARG_MNTPATH ]] then
		print -u2 "$PROGNAME: -p not valid with -$OPERATION"
		showusage $PROGNAME
		exit
	fi
	;;
esac

# execute command
case $OPERATION in
h)	showhelp $PROGNAME
	;;
m)	if [[ -n $ARG_MNTPATH ]] then
		$UTDISKADMCMD -m $ARG_MNTDEV -p $ARG_MNTPATH
	else
		$UTDISKADMCMD -m $ARG_MNTDEV
	fi
	;;
esac

exit $?
