#!/bin/ksh
#
# ident "@(#)utctl.ksh	1.5	05/06/01 SMI"
#
# Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
# Use is subject to license terms.
#

#
# This script activates the Sun Ray features that used to be done in the
# install scripts and utinstall modules.  This script must be run on the
# very first boot immediately after the installation of the product so that
# we can activate the Sun Ray service on the system.
#

export PATH=/bin:/usr/bin:/sbin
PROG=${0##*/}

BASE=`/etc/opt/SUNWut/basedir/lib/utprodinfo -r SUNWuto 2>/dev/null`
BASE=${BASE:-/opt}
SUNWUT=$BASE/SUNWut
SUNWUTLIB=$SUNWUT/lib
ETCSUNWUT=/etc/opt/SUNWut
UTCTL_D=$SUNWUTLIB/utctl.d
UTCTL_FEATURES=$UTCTL_D/features
UTCTL_ORDER=$UTCTL_D/profiles/default
UTCTL_RUN=$ETCSUNWUT/${PROG}.run
SPEC_LIST=""
VERB=""

#
# get the execution order for the features.  The first argument specifies
# the type of ordering to be returned, "default" or "reverse".  For "default",
# the order will be the same as specified in the "default" order file.  For
# "reverse", it will be in the reverse order.  If the argument is not specified,
# the default order is returned.
#
get_order() {
	if [ -n "$SPEC_LIST" ]; then
		# user specified the feature list
		print "$SPEC_LIST"
		return
	fi
	LIST=""
	exec < $UTCTL_ORDER
	while read F
	do
		expr "$F" : '[ 	]*#' > /dev/null
		if [ $? -eq 0 ]; then
			# skip comments
			continue
		fi
		if [ "$1" = "reverse" ]; then
			LIST="$F $LIST"
		else
			LIST="$LIST $F"
		fi
	done
	echo "$LIST"
}

#
# prints the usage message.
# $1 - exit code. if this is not 0 (success), it will send the output to
#	stderr.
Usage() {
	typeset OUT=""

	if [[ $1 -ne 0 ]]; then
		OUT="-u2"
	fi
	print ${OUT} "
	${PROG} help
	${PROG} [ -v ] <keyword> [ <feature> ... ]

        OPTION:
        -v      # turns the verbose mode on

	KEYWORD:
                # if no keyword is specified, ${PROG} will print the description
                # for all the available features.
	enable	# enables the feature specified
	disable	# disables the feature specified
	param	# lists the configurable parameters for the feature specified
	help	# displays the usage message and the list of available features

	FEATURES:"

	ORDER_LIST=`get_order forward`
	for F in $ORDER_LIST
	do
		FEATURE=$UTCTL_FEATURES/ut${F}ctl
		if [ -x $FEATURE ]; then
			print "\t$F"
		fi
	done

        exit $1
}

while getopts ":v" c
do
	case $c in
	v)	VERB="-v";;
	*)	print -u2 "${PROG}: invalid option $OPTARG"
		Usage 1;;
	esac
done

shift `expr $OPTIND - 1`
if [ $# -eq 0 ]; then
	# print the description of all the available features
	ORDER_LIST=`get_order forward`
	for F in $ORDER_LIST
	do
		FEATURE=$UTCTL_FEATURES/ut${F}ctl
		if [ -x $FEATURE ]; then
			$FEATURE
		fi
	done
	exit 0
fi
OP=$1
shift 1

if [ $# -gt 0 ]; then
	# caller specified the feature list
	SPEC_LIST="$*"
fi

case "$OP" in

'enable')
	ORDER_LIST=`get_order forward`
	ERROR_LIST=""
	for F in $ORDER_LIST
	do
		FEATURE=$UTCTL_FEATURES/ut${F}ctl
		if [ -x $FEATURE ]; then
			$FEATURE $VERB enable
		else
			ERROR_LIST="${ERROR_LIST} ${F}"
		fi
	done
	if [ -n "$ERROR_LIST" ]; then
		print -u2 "${PROG}: feature(s) not available -${ERROR_LIST}"
	fi
	# Set the run flag so that we don't activate the features again.
	touch $UTCTL_RUN
	;;

'disable')
	ORDER_LIST=`get_order reverse`
	for F in $ORDER_LIST
	do
		FEATURE=$UTCTL_FEATURES/ut${F}ctl
		if [ -x $FEATURE ]; then
			$FEATURE $VERB disable
		else
			ERROR_LIST="${ERROR_LIST} ${F}"
		fi
	done
	if [ -n "$ERROR_LIST" ]; then
		print -u2 "${PROG}: feature(s) not available -${ERROR_LIST}"
	fi
	;;

'param')
	ORDER_LIST=`get_order forward`
	for F in $ORDER_LIST
	do
		FEATURE=$UTCTL_FEATURES/ut${F}ctl
		if [ -x $FEATURE ]; then
			$FEATURE $VERB param | sed "s/^/$F: /"
		else
			ERROR_LIST="${ERROR_LIST} ${F}"
		fi
	done
	if [ -n "$ERROR_LIST" ]; then
		print -u2 "${PROG}: feature(s) not available -${ERROR_LIST}"
	fi
	;;

'help')
	if [ -n "$SPEC_LIST" ]; then
		print -u2 "${PROG}: no option allowed with the help sub-command"
		Usage 1
	fi
	Usage 0
esac
