import sys
import os
import warnings

from SCons.Util import \
        WhereIs
from SCons.Tool.ifort import \
        generate as old_generate

from numscons.tools.intel_common import get_abi
from numscons.tools.intel_common.win32 import find_fc_versions, product_dir_fc

def generate_linux(env):
    ifort = WhereIs('ifort')
    if not ifort:
        warnings.warn("ifort not found")
    return old_generate(env)

def generate_win32(env):
    # Import here to avoid importing msvc tool on every platform
    from SCons.Tool.MSCommon.common import get_output, parse_output

    abi = get_abi(env, lang='FORTRAN')

    # Get product dir
    versdict = find_fc_versions(abi)
    vers = sorted(versdict.keys())[::-1]
    pdir = product_dir_fc(versdict[vers[0]])
    batfile = os.path.join(pdir, "bin", "ifortvars.bat")

    out = get_output(batfile, args=abi)
    d = parse_output(out)
    for k, v in d.items():
        env.PrependENVPath(k, v, delete_existing=True)

    return old_generate(env)

def generate(env):
    if sys.platform.startswith('linux'):
        return generate_linux(env)
    elif sys.platform == 'win32':
        return generate_win32(env)
    else:
        raise RuntimeError('Intel fortran on %s not supported' % sys.platform)

def exists(env):
    pass

@@ -265,16 +265,68 @@
 dir_fc%0A%0A
+_ABI2BATABI = %7B%22amd64%22: %22intel64%22, %22ia32%22: %22ia32%22%7D%0A%0A
 def gene
@@ -897,19 +897,32 @@
 e, args=
+_ABI2BATABI%5B
 abi
+%5D
 )%0A    d 

Translate between abi and batfile abi argument.