#!/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: isi_patch_metadata 91270 2008-03-06 23:04:32Z eyoungblut $

import sys, os, os.path

VERSION_RC       = "boot/kernel.i386/version.rc"
REQUIRE          = "usr/libdata/patchmgr/upgrade/check_upgrade"
INSTALL          = "usr/libdata/patchmgr/upgrade/upgrade"
POST_INSTALL     = "usr/libdata/patchmgr/upgrade/post_upgrade"
kern_version     = "Isilon OneFS vHEAD"
kern_ostype      = "Isilon OneFS"
kern_osrelease   = "vHEAD"
kern_osbuild     = "B_HEAD"
isilon_copyright = "Copyright (c) 2001-2006 Isilon Systems, Inc.  " \
                   "All rights reserved."

def create_patch_metadata():
    
    f = open("+COMMENT", 'w')
    f.write("%s %s\n" % (kern_ostype, kern_osrelease))
    f.close()

    f = open("+DESC", 'w')
    f.write("%s\n\n%s\n" % (kern_version, isilon_copyright))
    f.close()

    f = open("+CONTENTS", 'w')
    f.write("@comment PKG_FORMAT_REVISION:1.1\n")
    f.write("@name install-%s\n" % kern_osbuild)
    f.write("@pkgver 1.0\n")
    f.write("@ignore\n")
    f.write("+COMMENT\n")
    f.write("@ignore\n")
    f.write("+DESC\n")
    f.write("@option no-extract\n")
    f.write("@ignore\n")
    f.write("+INSTALL\n")
    f.write("@ignore\n")
    f.write("+POST-INSTALL\n")
    f.write("@ignore\n")
    f.write("+REQUIRE\n")
    f.write("@cwd /\n")
    f.write("@timeout 900\n")
    f.write("@no_record all\n")
    f.write("@reboot all\n")
    f.close()

    os.system("/bin/cp -f %s +REQUIRE" % REQUIRE)
    os.system("/bin/cp -f %s +INSTALL" % INSTALL)
    os.system("/bin/cp -f %s +POST-INSTALL" % POST_INSTALL)

def create_from_dir():
    global kern_version
    global kern_ostype
    global kern_osrelease
    global kern_osbuild
    global isilon_copyright

    if os.path.exists(VERSION_RC):
        rc = open(VERSION_RC, 'r')    
        for line in rc.readlines():
            line = line.rstrip()
            line = line.split('=')
            if len(line) != 2:
                continue
            if line[0] == 'kern_version':
                kern_version = line[1]
            elif line[0] == 'kern_ostype':
                kern_ostype = line[1]
            elif line[0] == 'kern_osrelease':
                kern_osrelease = line[1]
            elif line[0] == 'kern_osbuild':
                kern_osbuild = line[1]
                kern_osbuild = kern_osbuild.rstrip(')')
                kern_osbuild = kern_osbuild.replace('(', '-')
                kern_osbuild = kern_osbuild.replace(')', '-')
            elif line[0] == 'isilon_copyright':
                isilon_copyright = line[1]
        rc.close()

    create_patch_metadata()

def create_from_file(fname):
    import isi.sys.update.util as util
    import isi.sys.update.config as config
    
    global kern_osrelease
    global kern_version
    global REQUIRE
    global INSTALL
    global POST_INSTALL
    
    try:
        v = util.Version(r".*Isilon OneFS v(\S+)")
        kern_osrelease = v._get_version_from_file(fname)
        if (not kern_osrelease == "HEAD" and
            config.Config.cmp(kern_osrelease, '4.6') >= 0):
            return
        kern_osrelease = "v%s" % kern_osrelease
        kern_version   = "%s %s" % (kern_ostype, kern_osrelease)
        REQUIRE          = "/usr/libdata/patchmgr/upgrade/check_upgrade"
        INSTALL          = "/usr/libdata/patchmgr/upgrade/upgrade"
        POST_INSTALL     = "/usr/libdata/patchmgr/upgrade/post_upgrade"
        create_patch_metadata()
    except:
        import traceback
        traceback.print_exc()
        pass
        
if __name__ == "__main__":
    if len(sys.argv) != 2:
        print "Usuage: isi_patch_metadata directory"
    elif os.path.isdir(sys.argv[1]):
        os.chdir(sys.argv[1])
        create_from_dir()
    elif os.path.isfile(sys.argv[1]):
        create_from_file(sys.argv[1])

