# Copyright (c) 2010 - 2012, 2014 AllSeen Alliance. All rights reserved.
#
#    Permission to use, copy, modify, and/or distribute this software for any
#    purpose with or without fee is hereby granted, provided that the above
#    copyright notice and this permission notice appear in all copies.
#
#    THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
#    WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
#    MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
#    ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
#    WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
#    ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
#    OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
# 
import os, zipfile
Import('ajenv')

vsix_env = ajenv.Clone();

# find the newest .NetFramework so we can use the 32 bit msbbuild.exe
def FindNewestDotNet( ):
    import re, winreg

    # check the environment for COMSPEC and if it contains SysWOW64, look for the 32bit version of MSBuild.exe
    if re.match('SysWOW64', os.environ['COMSPEC'] ):
        frameworkKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Wow6432Node\\Microsoft\\.NETFramework',0, winreg.KEY_READ)
    else:
        frameworkKey = winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\.NETFramework',0, winreg.KEY_READ)
    installRoot = winreg.QueryValueEx(frameworkKey, 'InstallRoot')

    # there can be multiple .Net framework versions installed, enumerate the registry keys to find the latest
    Version = ''
    try:
        index = 0
        while True:
            subkeyName = winreg.EnumKey(frameworkKey, index)
            if re.match(r'^v', subkeyName):
                if os.path.exists(installRoot[0] + subkeyName):
                    if (subkeyName > Version):
                        Version = subkeyName  #take the latest Framework.
            index += 1
    except:
        pass
    winreg.CloseKey(frameworkKey)
    return installRoot[0] + Version


# check the registry to see if the VS SDK is installed
def is_vsip_installed( ):
    import re, winreg

    vsip_installed = False
    try:
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Wow6432Node\\Microsoft\\VisualStudio\\VSIP\\11.0',0, winreg.KEY_READ) as key:
            vsip_installed = True
        with winreg.OpenKey(winreg.HKEY_LOCAL_MACHINE, 'SOFTWARE\\Microsoft\\VisualStudio\\VSIP\\11.0',0, winreg.KEY_READ) as key2:
            vsip_installed = True
    except:
        if not vsip_installed :
            print 'Could not find the expected version of the Visual Studio 2012 SDK'
        pass
    return vsip_installed
