#!/usr/bin/env 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.
#
# $Id: post_upgrade 116469 2009-05-01 00:05:58Z etorretta $
import sys
import os, os.path

import isi.sys.update.engine as engine
import isi.sys.update.config as config
import isi.sys.update.util as util

import logging
import time
import isi.hw.imdd.devices as devices 

def exit_on_err(eng):
    if eng._status == config.State.FAILED:
        os._exit(1)

if __name__ == '__main__':
    if not len(sys.argv) >= 3:
        os._exit(1)

    try:
        don_check = True
        skip_firmware = False
        if len(sys.argv) > 3:
            for arg in sys.argv[3:]:
                if arg == 'don_check':
                    don_check = False
                elif arg == 'skip_firmware':
                    skip_firmware = True

        eng = engine.Engine()
        lkg = devices.imdds.lookup("root lkg")
        eng.root_lkg_mountpoint = lkg.mountpoint
        eng._fw_check(don_check, skip_firmware)
        exit_on_err(eng)

        if (not skip_firmware and
            (eng.fw_update_state & engine.FwCheckState.FOUND) and 
            (eng.fw_update_state & engine.FwCheckState.REQUIRED)):
            eng._fw_update(True)
            exit_on_err(eng)

        lkgboot = os.path.join(lkg.mountpoint, "/boot")
        if os.path.isdir(lkgboot):
            # if /usr/update/root/boot is mounted, take the steps engine.py
            # takes.
            try:
                # 5.0 and after have a bootdir parameter
                devices.fix_boot(lkgboot)
            except TypeError:
                # 4.7 and earlier do not
                devices.fix_boot()
            else:
                # make sure that the file /etc/mcp/sys/lni.xml is not
                # preserved. Since we may downgrade a node from Jamaican
                # release to pre-Jamaican releases, a node running
                # pre-Jamaican release will not re-generate the file
                # /etc/mcp/sys/lni.xml if it exists.
                lni_file = os.path.join(lkg.mountpoint, 'etc/mcp/sys/lni.xml')
                if os.path.exists(lni_file):
                    os.unlink(lni_file)
        else:
            devices.fix_boot()

        eng._lkg_swap()
        for h in logging._handlers.keys():
            h.flush()
        time.sleep(5)
        eng._cp_log_file()
        util.Util.pipe_open('/bin/sync')

        os._exit(0)
    except:
        #import traceback
        #traceback.print_exc()
        util.Logging.exception()
        os._exit(1)
        
