#!/usr/local/bin/python

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

import sys
import os

import isi.sys.bootdisk as bootdisk

firmware_msg = \
"""Note that firmware has NOT been updated. Use the 'isi firmware update'
command after reboot to update firmware, assuming you've installed
a valid firmware package on the cluster. If you boot into a system
that doesn't have the 'isi firmware update' command, then see the
/usr/bin/isi_hwtools/versions.txt file for instructions on manually
updating your firmware."""

if __name__ == '__main__':
    revert = False
    if len(sys.argv) == 2 and sys.argv[1] == "--revert":
        revert = True

    bootdisk.init_providers()

    root = bootdisk.get_provider(bootdisk.PROVIDER_ID_ROOT)
    root_lkg = bootdisk.get_provider(bootdisk.PROVIDER_ID_ROOT_LKG)
    var = bootdisk.get_provider(bootdisk.PROVIDER_ID_VAR)
    var_lkg = bootdisk.get_provider(bootdisk.PROVIDER_ID_VAR_LKG)

    if revert:
        bootdisk.set_current_to_boot(root, var, root_lkg, var_lkg)
        root_to_boot = root
        print "The current OS is set to boot."
    else:
        bootdisk.set_lkg_to_boot(root, var, root_lkg, var_lkg)
        root_to_boot = root_lkg
        print "The last known good OS is set to boot."

    mounted = root_to_boot.mounted
    if not mounted:
        root_to_boot.mount()
    bootdisk.fix_boot(os.path.join(root_to_boot.mountpoint, 'boot'))
    if not mounted:
        root_to_boot.umount()

    print ""
    print firmware_msg

