#!/bin/sh

# Copyright C 2002-2011 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,
# 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.

# This script should be run only from isi_post_ifs and all commands run
# in it must be idempotent, as a failure could occur midway through execution
# and this script will be re-run during the next boot.

# Pre-conditions: ifs is mounted, node has quorum, /ifs/.ifsvar is created

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

# Check if ifsvar is ready

/usr/bin/isi_ifsvar_ready
ifsvar_ready=$?

# If ifsvar isn't ready then wait 60 seconds before checking again for
# readiness. 60 seconds is no magical number but we expect to loop couple of
# times before ifsvar is ready. If ifsvar isn't ready in 10 minutes we
# exit out. Related to bug 44576.
count=0
while [ $ifsvar_ready -ne 0 ] && [ $count -lt 10 ]
do
    /usr/bin/logger -i -p ifs.err "Ifsvar ready yet: sleeping for 60
    seconds"
    /bin/sleep 60
    /usr/bin/isi_ifsvar_ready
    ifsvar_ready=$?
    count=$(($count + 1 ))
done

if [ $ifsvar_ready -ne 0 ]; then
    exit 1
fi

# ==== WARNING WARNING WARNING WARNING WARNING WARNING ====
# DEPRECATED LEGACY as of Riptide DO NOT put anything new here

# (A) For upgrades where the "Originating" Version is Riptide (8.0.0.0) or later
#     please use the upgrade hook UpgradePostRebootInitial and/or
#     RollbackPostRebootInitial instead

# (B) For upgrades where the "Originating" Version is prior to Riptide (8.0.0.0)
#     please place the logic in:
#     /usr/share/upgrade/event-actions/upgrade-post-reboot-initial/isi_legacy_upgrade_isi_first_post_ifs.sh
# ==== WARNING WARNING WARNING WARNING WARNING WARNING ====

if [ ! -f /etc/ifs/isi_upgrade_in_progress ]; then
    /usr/share/upgrade/event-actions/upgrade-post-reboot-initial/isi_legacy_upgrade_isi_first_post_ifs.sh
    touch /etc/ifs/isi_first_post_ifs_already_ran
    chmod 640 /etc/ifs/isi_first_post_ifs_already_ran
fi

exit 0
