#!/bin/ksh -p
#
# ident "@(#)utctl.ksh	1.9	11/01/26 Oracle"
#
# Copyright (c) 2005, 2011, Oracle and/or its affiliates. All rights reserved.
#

#
# 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##*/}

SUNWUTLIB=/opt/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.
#
function 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.
function 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
}

function execute {
	case "$1" in

		'enable')
			# Set the run flag so that we don't activate
			# the features again.  Do not set the run flag
			# if individual feature is activated.  RFE
			# [6471030] is filed to provide a way to store
			# the activation status of individual feature
			# in UTCTL_RUN file and that can be used
			# during activation of features instead of
			# using global activation switch.
			if [ -z "$SPEC_LIST" ]; then
				touch $UTCTL_RUN
			fi
			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
			/etc/init.d/utacleanup
			;;

		'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
}

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

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

TIME_STAMP="`date '+%Y_%m_%d_%H:%M:%S'`"
if [ "`uname -s`" = "SunOS" ]; then
	ADM_LOG_DIR="/var/adm/log"
else
	LINUX_LOG_DIR="/var/log/SUNWut"
	if [[ -d ${LINUX_LOG_DIR} ]]; then
		ADM_LOG_DIR=${LINUX_LOG_DIR}
	else
		ADM_LOG_DIR="/var/log"
	fi

fi
LOGFILE="${ADM_LOG_DIR}/utctl.${TIME_STAMP}.log"

PRVERB=$OP
if [ "$FEATURES" = "Product" -a "$PRVERB" = "enable" ]; then
	PRVERB=activation
fi

print "$FEATURES $PRVERB output being logged at $LOGFILE"
execute $OP > $LOGFILE 2>&1
