#!/bin/ksh -p
#
# ident "@(#)utsetup.ksh	1.4	11/02/25 Oracle"
#
# Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
#

PATH=/usr/bin:/bin:/usr/sbin:/sbin
PROG=`basename $0`
OPTSBIN=/opt/SUNWut/sbin

function usage {
	typeset fd=$1

	print  "Usage: $PROG [options]
	-a		use auto-response file where possible
	-d		use auto-defaults file where possible
	-D dialog_dir	use dialog files in specified directory
	-n		non-interactive (abort if input is required)
	-p		automate all steps except for personalization and startup

	If -a and -d are both specified, auto-response entries take precedence.
" 1>&$fd
}

function LogIt {
	print ""
	print "=================================================================="
	print "=== $1 ==="
}

function do_utinstall {
	LogIt "Installing Sun Ray Software - utinstall"
	AICOPTS="${autoFlag:+-A} ${defaultFlag:+-c} $nonInteractiveFlag ${dialogDir:+-D $dialogDir}"
	`dirname $0`/utinstall $AICOPTS
}

function main {
	# By default, the following programs will be executed in order:
	# utinstall *
	# utctl enable
	# utconfig *
	# (personalization steps follow only if -p option is not specified)
	# utadm -L on ***
	# utfwadm -A -a -V ***
	# utpolicy -x *
	# utpw * **
	# utgroupsig * **
	# utreplica (if FOG was specified to utconfig) *
	# utrestart -c
	#
	# *  These programs have AIC-capable dialogs
	#
	# ** these programs are only run if a post-factor-prep/personalization
	#    phase is being performed since it is assumed that a configuration
	#    is being cloned, otherwise the necessary data has already been
	#    configured by utconfig
	#
	# *** These programs will only be executed if requested (AIC-capable)
	#

	do_utinstall
	# The SUNWuti utsetup completes the rest of the required tasks
	if [ -x $OPTSBIN/utsetup ]; then
		exec $OPTSBIN/utsetup -C $autoFlag $defaultFlag $nonInteractiveFlag $shouldSkipPersonalizingFlag
	fi
}

# main
autoFlag=
defaultFlag=
nonInteractiveFlag=
dialogDir=
shouldSkipPersonalizingFlag=

while getopts :adD:hnp opt; do
	case $opt in
		a)
			autoFlag=-a
			;;
		d)
			defaultFlag=-d
			;;
		D)
			dialogDir=$OPTARG
			;;
		h)
			usage 1
			exit 0
			;;
		n)
			nonInteractiveFlag=-n
			;;
		p)
			shouldSkipPersonalizingFlag=-p
			;;
		?)
			print -u2 "$PROG: Unknown option '$OPTARG'"
			usage 2
			exit 1
	esac
done

if [ $# -ne $(( $OPTIND - 1 )) ]; then
	print -u2 "$PROG: Too many arguments"
	usage 2
	exit 1
fi

case $(uname -s) in
	SunOS) ADM_LOG_DIR="/var/adm/log";;
	Linux) ADM_LOG_DIR="/var/log";;
esac
PROGRAM_ID="$(basename $0)"
TIME_STAMP="$(date '+%Y_%m_%d_%H:%M:%S')"
LOGFILE="${ADM_LOG_DIR}/${PROGRAM_ID}.${TIME_STAMP}.log"
main 2>&1 | tee $LOGFILE
