#!/bin/sh
#
# Copyright C 2002-2013 by Isilon Systems, LLC. All rights not expressly
# granted herein are reserved.
# Created in the United States of America.
# 
# This digital media is protected by U.S. and international copyright and
# intellectual property laws. Unless otherwise specified, all information
# and screens appearing as part of this digital medium, including
# software, services, documents, text, images, icons, and logos design;
# the selection, assembly, arrangement, and design thereof; and the code
# that enables its presentation, are the sole property of Isilon Systems,
# LLC.
# 
# Except as otherwise required by applicable law, no part of any
# copyrighted material may be reproduced, modified, translated,
# distributed, published, or transmitted by any means, electronic or
# mechanical, for any purpose, without the prior written consent of Isilon
# Systems, LLC. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# LLC.  Legion is a trademark of Isilon Systems, LLC.  All other
# trademarks and registered trademarks are the property of their
# respective holders.
#

# PROVIDE: isi_hal_init
# REQUIRE: isi_module_init isi_bootdisk_init

. /etc/rc.subr

name="isi_hal_init"
start_cmd="isi_hal_startup"


isi_hal_startup()
{
	umask 002
	#query isi_radish about info on boot drives and save to a log file
	/usr/bin/isi_bootdisk_info >> /var/log/isi_bootdisk_info.log

	# We should try to pull the VPD from the EEPROM before we attempt
	# to run the inventory tool.  If this node has a legacy SKU, we 
	# should be able to get it and avoid going down the CTO path
	# erroneously.  Re-direct the output to /dev/null to avoid spewage
	# that may must be confusing to the casual observer. Also, regardless
	# of the success of the command, we're still going to attempt to 
	# determine if the node supports CTO using what we have at our 
	# disposal.
	#
	# NOTE: HAL should have enough logic to provide what is needed
	# 	for this tool to run.
	echo "Attempt sync of VPD EEPROM data to node..."

	/usr/bin/isi_vpd_pull

	/usr/bin/isi_hwtools/isi_hal_predicate supportsCto > /dev/null 2>&1
	supports_cto=$?

	/usr/bin/isi_hwtools/isi_hal_predicate isCtoInitted > /dev/null 2>&1
	cto_initted=$?

	do_init=1
	isi_reboot_filename="/tmp/isi_rebooting"
	if [ ${cto_initted} -eq 1 ] && [ -f ${isi_reboot_filename} ]; then
		ts_file=`cat ${isi_reboot_filename}`
		ts_now=`date +"%s"`
		ts_diff=$(( ${ts_now} - ${ts_file} ))

		# A no-op unless this is a CTO node.  If it has been 5 minutes
		# since we logged the last reboot, we should run init anyway
		# to be safe.
		if [ ${supports_cto} -eq 1 ] && [ ${ts_diff} -lt 300 ]; then
			echo "Skipping HAL init..."
			do_init=0
		fi

		# Remove skip file - this should be reset each time by
		# a clean reboot.
		/bin/rm -f $isi_reboot_filename > /dev/null 2>&1
	fi

	# A no-op unless this is a CTO node.
	if [ ${do_init} -eq 1 ] && [ ${supports_cto} -eq 1 ]; then
		echo "Executing HAL init..."
		/usr/bin/isi_inventory_tool --startUp
	fi
}

load_rc_config $name
run_rc_command "$1"
