#!/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: check_upgrade 65844 2007-03-14 21:06:27Z qlu $
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

def yes_no(utype, default):
    try:
        prompt = "Are you sure you wish to %s (yes/no [%s])? " % \
                 (utype, default)
        
        while True:
            confirm = raw_input(prompt)
            if (confirm == ''):
                confirm = default 
                
            if (confirm == 'no' or confirm == 'n'):
                #import signal
                #os.kill(os.getppid(), signal.SIGKILL)
                os._exit(1)
            elif (confirm == 'yes' or confirm == 'y'):
                os._exit(0)
    except:
        # import traceback
        # traceback.print_exc()
        os._exit(1)

if __name__ == '__main__':
    if not len(sys.argv) >= 3:
        os._exit(1)
        
    try:
        tarball = os.path.join(sys.argv[1], sys.argv[2])
        if not os.path.isfile(tarball):
            os._exit(1)

        version = util.Version()
        rv = version.get_running_version()
        nv = version.get_version(tarball)

        print "Node version : %s" % rv
        print "Image version: %s" % nv

        utype = config.Config.cmp(rv, nv) > 0 and 'downgrade' or 'upgrade'
        default = 'yes'        
        if len(sys.argv) > 3:
            for arg in sys.argv[3:]:
                if arg == 'force':
                    os._exit(0)
                elif arg == 'reimage':
                    utype = 'reimage'
                    default = 'no'

        yes_no(utype, default)                
    except:
        # import traceback
        # traceback.print_exc()
        os._exit(1)
