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

PATH=/usr/bin:/bin:/usr/sbin:/sbin
PROG=`basename $0`
ETCOPT=/etc/opt/SUNWut
OPTLIB=/opt/SUNWut/lib
OPTSBIN=/opt/SUNWut/sbin

function usage {
	typeset fd=$1

	print  "Usage: $PROG [options]
	-n	non-interactive (abort if input is required)
	-a	use auto-response file where possible
	-d	use auto-defaults file where possible

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

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

function do_utconfig {
	LogIt "Performing basic configuration - utconfig"
	AICOPTS="$autoFlag $defaultFlag $nonInteractiveFlag"
	$OPTSBIN/utconfig $AICOPTS
}

function do_utpolicy {
	LogIt "Configuring access policy - utpolicy"
	AICOPTS="${autoFlag:+-A} ${defaultFlag:+-Z} $nonInteractiveFlag"
	$OPTSBIN/utpolicy -x $AICOPTS
}

function do_utadm {
	LogIt "Enabling LAN access - utadm"
	if [ $($UTDIALOG -H YesOrNo:N utsetup.LAN_EnableYN) = Y ]; then
		$OPTSBIN/utadm -L on
	fi
}

function do_utfwadm {
	LogIt "Configuring firmware downloads - utfwadm"
	if [ $($UTDIALOG -H YesOrNo:Y utsetup.FirmwareConfigYN) = Y ]; then
		$OPTSBIN/utfwadm -A -a -V
	fi
}

function do_utreplica {
	LogIt "Configuring replication - utreplica"
	AICOPTS="${autoFlag:+-A} ${defaultFlag:+-D} $nonInteractiveFlag"
	$OPTSBIN/utreplica $AICOPTS
}

function do_utacleanup {
	LogIt "Creating directory structure - utacleanup"
	/etc/init.d/utacleanup
}

function do_utstart {
	LogIt "Starting Sun Ray Software - utstart"
	$OPTSBIN/utstart -c
}

function isHA {
	typeset gmsigfile footprint fpcnt
	
	gmsigfile=$ETCOPT/gmSignature
	footprint="autogenerated"
	fpcnt="`print -n $footprint | wc -c | sed 's/ //g'`"
	[ ! `dd if=$gmsigfile bs=$fpcnt count=1 2>/dev/null` = $footprint ]
}

function isConfigured {
	[ -f $ETCOPT/utadmin.conf ]
}

function main {
	#
	# By default, the following programs will be executed in order:
	# utinstall *
	# utconfig * ***
	# utpolicy -x * ***
	# utreplica (if FOG was specified to utconfig) * ***
	# utfwadm -A -a -V ** ***
	# utadm -L on ** ***
	# utstart -c
	#
	# *  These programs have AIC-capable dialogs
	#
	# ** These programs will only be executed if requested (AIC-capable)
	#
	# *** These programs will only be executed for a fresh installation
	#

	do_utacleanup
	if ! isConfigured; then
		do_utconfig
		do_utpolicy
		if isHA; then
			do_utreplica
		fi
		do_utfwadm
		do_utadm
	fi
	do_utstart
}

# main
autoFlag=
defaultFlag=
nonInteractiveFlag=
chainedSetup=false
UTDIALOG_OPTS=

while getopts :aCdhn opt; do
	case $opt in
		a)
			autoFlag=-a
			UTDIALOG_OPTS="$UTDIALOG_OPTS -$opt"
			;;
		d)
			defaultFlag=-d
			UTDIALOG_OPTS="$UTDIALOG_OPTS -$opt"
			;;
		h)
			usage 1
			exit 0
			;;
		n)
			nonInteractiveFlag=-n
			UTDIALOG_OPTS="$UTDIALOG_OPTS -$opt"
			print -u2 "$PROG: The -n option is not implemented at this time"
			;;
		C)
			# Reserved for use by image utsetup so we
			# don't do redundant logging
			chainedSetup=true
			;;
		?)
			print -u2 "$PROG: Unknown option '$OPTARG'"
			usage 2
			exit 1
	esac
done

# XXX
if [ -n "$nonInteractiveFlag" ]; then
	exit 1
fi

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

UTDIALOG="$OPTLIB/utdialog $UTDIALOG_OPTS -c SRSS"

if ! $UTDIALOG -C $PROG; then
	exit 1
fi

if $chainedSetup; then
	main	# don't redirect output, already being logged
else
	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
fi
