#!/bin/sh

# Copyright C 2002-2011 by Isilon Systems, Inc. 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,
# Inc.
# 
# 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. 
#  
# Isilon and Isilon Systems are registered trademarks of Isilon Systems,
# Inc.  Legion is a trademark of Isilon Systems, Inc.  All other
# trademarks and registered trademarks are the property of their
# respective holders.

# PROVIDE: isi_first_boot
# REQUIRE: isi_hw_init isi_create_lni_xml ldconfig

if [ -f /etc/ifs/isi_first_boot_already_ran ]; then
	exit
fi	    

if [ -f /etc/rc.isi_first_boot_already_ran ]; then
	touch /etc/ifs/isi_first_boot_already_ran
	chmod 640 /etc/ifs/isi_first_boot_already_ran
	/bin/rm -f /etc/rc.isi_first_boot_already_ran
	exit
fi	    

# Variables to hold shorthand versions of the syslog priorities that we use
NOTE=user.notice
ERROR=user.err

#
# Log a message to the system console and to /var/log/messages
#
# $1 = syslog priority expressed as facility.level tuple
#      (refer to NOTE, ERROR above)
# $2 = message
#
log () {
	echo $2
	logger -p $1 $2
}

#
# With the advent of the "3/4 chassis", manufacturing will
# no longer be shipping chassis (currently only 4U) configured
# with product SKUs (851 SKUs), but instead, they will ship
# chassis configured as 3/4 chassis (812 SKUs). Therefore, these
# chassis will need to be transformed into their appropriate
# product SKUs during initial installation.
#
log ${NOTE} "Checking if chassis requires SKU transformation..."
if [ -c /dev/da0 ]; then
	log ${NOTE} "Booted from USB flash, skipping SKU transformation."
elif [ ! -f /etc/ifs/isi_factory_fresh_drives ]; then
	log ${NOTE} "Drives are not factory fresh, skipping SKU transformation."
else
	log ${NOTE} "Drives are factory fresh."
	/bin/rm -f /etc/ifs/isi_factory_fresh_drives
	/usr/bin/isi_transform_tool --need
	if [ $? -eq 0 ]; then
		log ${NOTE} "No SKU transformation needed, skipping."
	else
		log ${NOTE} "Performing SKU transformation..."
		/usr/bin/isi_transform_tool --install
		if [ $? -eq 0 ]; then
			log ${NOTE} "SKU transformation completed successfully."
		else
			log ${ERROR} "ERROR: SKU transformation failed."
		fi
	fi
fi

/usr/bin/isi_hwtools/isi_hal_predicate supportsSelfEncryptingDrives > /dev/null 2>&1
supports_seds=$?

if [ ${supports_seds} -eq 1 ]; then
	touch /etc/ifs/isi_newfs_wait
	journal_dev=`python -c "import isi.hw.hal as hal; print hal.getNvramDeviceName()"`
	/usr/sbin/isi_newjournal -f /dev/$journal_dev --force
fi

# LCD is stopped and restarted to pick-up HW files.
/usr/bin/isi_lcd_d stop
/usr/bin/isi_lcd_d start

if [ -f /etc/newfs_run ] && [ ! -f /etc/ifs/isi_newfs_wait ]; then
	log ${NOTE} "Performing newfs_efs..."
	/bin/rm -f /etc/newfs_run
	/bin/rm -f /etc/ifs/drives.xml
	/sbin/newfs_efs -f
elif [ ! -f /etc/ifs/drives.xml ] && [ ! -f /etc/ifs/isi_newfs_wait ]; then
	log ${NOTE} "Performing isi_initial_newfs..."
	/usr/sbin/isi_initial_newfs
fi

if [ -f /usr/sbin/isi_mcp ]; then
    /usr/sbin/isi_mcp -u
fi

touch /etc/ifs/isi_first_boot_already_ran
chmod 640 /etc/ifs/isi_first_boot_already_ran

if [ -r /etc/defaults/rc.conf ]; then
	. /etc/defaults/rc.conf
	source_rc_confs
elif [ -r /etc/rc.conf ]; then
	. /etc/rc.conf
fi

/etc/rc.d/dumpon start
/etc/rc.d/savecore start
